我正在安排活动。根据事件类型,它会安排在特定日期(例如仅周五和周日)或一周的所有日期。
问题 我创建的通知没有被解雇。我在谷歌搜索但无法找到导致错误的原因。 我的代码如下:
Intent intent = new Intent(this, MyReceiver.class);
intent.putExtra("HABIT_NAME", habitName.getText().toString());
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), eventId, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
if(daysNo.size() == 7){ //set for whole week
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
reminderLifeTS.getTimeInMillis(), 1000*60*60*24*7, pendingIntent);
}
else{ //set for specific days
for(int i=0;i<daysNo.size();i++){
int day = getWeek(daysNo.get(i));
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_WEEK, day);
c.set(Calendar.HOUR_OF_DAY, reminderLifeTS.get(Calendar.HOUR_OF_DAY));
c.set(Calendar.MINUTE, reminderLifeTS.get(Calendar.MINUTE));
System.out.println("day of week is : " + day);
System.out.println("reminderLifeTS.get(Calendar.HOUR_OF_DAY) is : " + reminderLifeTS.get(Calendar.HOUR_OF_DAY));
System.out.println("reminderLifeTS.get(Calendar.MINUTE) is : " + reminderLifeTS.get(Calendar.MINUTE));
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
c.getTimeInMillis(), 1000*60*60*24, pendingIntent);
}
}
广播接收器:
public void onReceive(Context context, Intent intent)
{
Intent service1 = new Intent(context, MyAlarmService.class);
String habitName = intent.getStringExtra("HABIT_NAME");
service1.putExtra("HABIT_NAME", habitName);
context.startService(service1);
}
MyAlarmService
@Override
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
String habitName = intent.getStringExtra("HABIT_NAME");
mManager = (NotificationManager) this.getApplicationContext().
getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(),MainActivity.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
this.getApplicationContext(),0, intent1,PendingIntent.FLAG_ONE_SHOT);
// megha
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.app_icon)
.setContentTitle("DharmaLife")
.setContentText(habitName)
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingNotificationIntent);
Notification note = mBuilder.build();
note.defaults |= Notification.DEFAULT_VIBRATE;
note.defaults |= Notification.DEFAULT_LIGHTS;
note.flags|= Notification.FLAG_AUTO_CANCEL;
PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK |
PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "TAG");
wakeLock.acquire();
// end megha
mManager.notify(0, note);
}
修改 所有日子都很好:
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
reminderLifeTS.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
任何人都可以指导我特定的日子。
特定日期:
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
c.getTimeInMillis(),7*24*60*60*1000, pendingIntent);
正在为我工作。