我试图设置一个将向广播接收器发送意图的警报。然后接收者应该在 logcat 中打印一行,说明它已经工作了。但是我没有得到任何迹象表明接收者正在接收任何东西。我已经检查了所有我能想到的东西。有人可以给我一些指导,因为我不知所措吗?
报警方式
public void scheduleNextUpdate()
{
Intent intent = new Intent(MainActivity.this,AlarmReceiver.class);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
long currentTimeMillis = System.currentTimeMillis();
long nextUpdateTimeMillis = currentTimeMillis * DateUtils.MINUTE_IN_MILLIS;
Time nextUpdateTime = new Time();
nextUpdateTime.set(nextUpdateTimeMillis);
if (nextUpdateTime.hour < 8 || nextUpdateTime.hour >= 22)
{
nextUpdateTime.hour = 8;
nextUpdateTime.minute = 0;
nextUpdateTime.second = 0;
nextUpdateTimeMillis = nextUpdateTime.toMillis(false) + DateUtils.DAY_IN_MILLIS;
}
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, nextUpdateTimeMillis, pendingIntent);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, nextUpdateTimeMillis,nextUpdateTimeMillis,pendingIntent);
boolean alarmUp = (PendingIntent.getBroadcast(this, 0,
new Intent(MainActivity.this,AlarmReceiver.class),
PendingIntent.FLAG_NO_CREATE) != null);
if (alarmUp)
{
Log.d("myTag", "Alarm is already active");
}
}
接收者等级
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("Alarm Received");
intent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
System.out.println("Alarm Received");
}
清单
<receiver android:name=".AlarmReceiver"></receiver>