我正在开发一个Android应用程序,我想让我的用户选择设置通知时间,以及他们是否希望在指定的时间间隔后重新发送通知。我遇到了报警管理器的问题。不知何故,警报管理器不会触发广播接收器。
以下是在主要活动中为警报管理器中的通知设置时间的代码:
private void checkForCreatingNotification(Intent data){
if (Constants.hours!=-1 && Constants.minutes!=-1){
Calendar calendar=Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,Constants.hours);
calendar.set(Calendar.MINUTE,Constants.minutes);
calendar.set(Calendar.SECOND, 0);
Log.e("time", " " + calendar.getTime());
Log.e("time"," "+calendar.getTimeInMillis());
boolean setOnce=data.getBooleanExtra(Constants.TIME_SET_ONCE,false);
boolean setRepeat=data.getBooleanExtra(Constants.TIME_SET_REPEAT,false);
int repeatDuration=data.getIntExtra(Constants.REPEAT_DURATION,Constants.NO_DURATION_REPEAT);
SharedPreferences preferences=getSharedPreferences(Constants.SHARED_PREFERENCES, MODE_PRIVATE);
SharedPreferences.Editor editor=preferences.edit();
editor.putBoolean(Constants.TIME_SET_ONCE,setOnce);
editor.putBoolean(Constants.TIME_SET_REPEAT, setRepeat);
editor.putInt(Constants.REPEAT_DURATION, repeatDuration);
editor.apply();
Intent intent;
intent = new Intent(this,NotificationMessage.class);
PendingIntent pendingIntent = PendingIntent.getService(this,
Constants.NOTIFICATION_REQUEST , intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
if (setOnce){
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}else if (setRepeat){
if(repeatDuration==Constants.HOURS24_DURATION)
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
else if (repeatDuration==Constants.HOURS12_DURATION)
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_HALF_DAY, pendingIntent);
else if (repeatDuration==Constants.HOURS6_DURATION)
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_HALF_DAY/2, pendingIntent);
else if (repeatDuration==Constants.HOURS1_DURATION)
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_HOUR, pendingIntent);
else if (repeatDuration==Constants.MINUTES30_DURATION)
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_HALF_HOUR, pendingIntent);
else if (repeatDuration==Constants.MINUTES5_DURATION)
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_HALF_HOUR/6, pendingIntent);
else if (repeatDuration==Constants.MINUTES1_DURATION)
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_FIFTEEN_MINUTES/15, pendingIntent);
}
//sendBroadcast(intent);
}
}
BroadCastReceiver:
public class NotificationMessage extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(context.getResources().getString(R.string.notification_title))
.setContentText(context.getResources().getString(R.string.notification_message));
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
Intent resultIntent = new Intent(context, WardrobeHome.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(WardrobeHome.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(Constants.NOTIFICATION_REQUEST, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
}
我已在清单文件中声明了接收器。
答案 0 :(得分:2)
使用PendingIntent.getBroadcast而不是PendingIntent.getService,你很高兴