在android中安排通知

时间:2013-05-23 18:23:50

标签: android notifications

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

类AlarmSet

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());
}

类AlarmReceiver

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

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

receiver android:name="AlarmReceiver">
/application>

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

知道我错了什么?我见过其他用户的代码,它与我的代码非常相似。

1 个答案:

答案 0 :(得分: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的onHandleIntent()方法中处理实际的通知发送等(扩展了IntentService)。

相关问题