我知道这是一个基本问题,但它仍然让我发疯。我正在设置一个重复警报,但接收器从未被调用。
Intent intent = new Intent(NewSchedule.this, RepeatingAlarm.class);
PendingIntent sender = PendingIntent.getBroadcast(NewSchedule.this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, calendar.getTimeInMillis(), 5 * 1000, sender);
Log.i("calendar",calendar.getTimeInMillis() + "");
Toast.makeText(NewSchedule.this, "repeating_scheduled", Toast.LENGTH_SHORT).show();
public class RepeatingAlarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "repeating_received", Toast.LENGTH_LONG).show();
}
}
<receiver android:name=".RepeatingAlarm" android:process=":remote" />
我在手机上测试。日历日志显示确切的时间。我从未在接收器类中获得Toast。
答案 0 :(得分:0)
参考:Android Alarm Manager with broadcast receiver
Intent sender = new Intent("WhatEverYouWant");
PendingIntent senderPIntent = PendingIntent.getBroadcast(context, 0, sender, 0);
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, triggerTime, senderPIntent);
// In Manifest.xml file
<receiver android:name="com.package.YourOnReceiver">
<intent-filter>
<action android:name="WhatEverYouWant" />
</intent-filter>
</receiver>
答案 1 :(得分:0)
事实证明我的代码很好。不知怎的,警报已启动并运行,因此由于某些未知原因(至少对我来说),无法调用接收器。当我创建一个新项目并测试该接收器工作正常时,我想通了。我也不得不停止这种警报。然后我回到我原来的项目并开始相同的警报而不改变任何线条,它工作正常。有没有人经历过这个?