1

我的通知安排有问题,我没有收到任何东西,这里是我:

类警报集

public void AlarmStart() {
     Calendar cal = Calendar.getInstance();
     cal.setTimeZone(TimeZone.getDefault());
     cal.set(Calendar.HOUR_OF_DAY, 20);
     cal.set(Calendar.MINUTE, 15);
     Intent intent = new Intent(context, AlarmReceiver.class);
     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
     AlarmManager am = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
     am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),AlarmManager.INTERVAL_FIFTEEN_MINUTES, sender);
     Log.d("MyActivity", "Set alarmManager.setRepeating to: " + cal.getTime().toLocaleString());
}

类警报接收器

public void onReceive(Context context, Intent objIntent) {
    Log.d("AlarmReceiver", "Called context.startService from AlarmReceiver.onReceive");
}

并在清单中: ... ...

接收器 android:name="AlarmReceiver">
/应用>

我收到第一条日志消息,所以它似乎设置了活动,然后我没有收到任何其他内容。上下文是从另一个活动类传递的,因为它们是简单的类。

知道我错了什么吗?我看过其他用户的代码,它和我的差不多。

4

1 回答 1

1

您没有说为什么要尝试使用广播而不是使用服务来设置它。您可以做的(如果您的意图只是安排通知)是尝试更改 PendingIntent 以启动 IntentService 而不是触发广播:

Intent myIntent = new Intent(context, YourService.class);
PendingIntent sender = PendingIntent.getService(context, 0, myIntent, 0);
 AlarmManager am = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis),AlarmManager.INTERVAL_FIFTEEN_MINUTES, sender                      );

然后,将处理实际通知发送等的代码放入 YourService(扩展 IntentService)的 onHandleIntent() 方法中。

于 2013-05-23T19:21:39.637 回答