0

我知道这是一个基本问题,但它仍然让我发疯。我正在设置重复警报,但从未呼叫过接收器。

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" />

我正在手机上测试。日历日志显示准确时间。我从来没有在接收器类中得到吐司。

4

2 回答 2

0

参考:带有广播接收器的 Android 警报管理器

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>
于 2012-10-10T19:29:35.203 回答
0

实际上,事实证明我的代码很好。不知何故,警报启动并运行,因此由于某种未知原因(至少对我而言)无法呼叫接收器。当我创建一个新项目并测试该接收器工作正常时,我想通了。我也不得不停止那个警报。然后我回到我原来的项目并在不改变任何行的情况下启动了相同的警报,它工作正常。有没有人经历过这个?

于 2012-10-10T19:40:13.827 回答