我需要每 2 小时向用户显示一次完成注册的通知,我已将其设置为几分钟以进行测试,但我的通知只会出现一次,它再也不会出现。请建议。
我的重复任务代码:
public void createNotification() {
Intent notificationIntent = new Intent(context, ShowNotification.class);
PendingIntent contentIntent = PendingIntent.getService(context, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
// am.cancel(contentIntent);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 60); // first time
long frequency= 60 * 1000; // in ms
am.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), frequency, contentIntent);
}
我的服务代码:
public class ShowNotification extends Service {
private final static String TAG = "ShowNotification";
@Override
public void onCreate() {
super.onCreate();
Intent mainIntent = new Intent(this, DashBoardActivity.class);
NotificationManager notificationManager
= (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
Notification noti = new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setContentIntent(PendingIntent.getActivity(this, 0, mainIntent,
PendingIntent.FLAG_UPDATE_CURRENT))
.setContentTitle("HELLO " + System.currentTimeMillis())
.setContentText("Please complete all the steps for registration")
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(R.drawable.ic_launcher)
.setTicker("ticker message")
.setWhen(System.currentTimeMillis())
.build();
notificationManager.notify((int) System.currentTimeMillis(), noti);
// ToastUtil.displayToast(this , "my msg");
Log.i(TAG, "Notification created");
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}