在我的应用程序中,我有一个BroadcastReceiver
,它由一个AlarmManager
. 这BroadcastReceiver
称为 CommonsWare 的WakefulIntentservice
.
我在手机上对此进行了测试,有时似乎BroadcastReceiver
根本没有打电话给我。我真的很困惑它可能是什么。我的 BroadcastReceiver 和 WakefulIntentservice 已在清单中注册。
这是我的代码: 在 AlarmActivity 中:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 2);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), savedIntervalAutomaticMilisInt, pendingIntent);
Toast.makeText(this, "Saved", Toast.LENGTH_LONG).show();
finish();
报警接收器:
package com.something.app;
import com.commonsware.cwac.wakeful.WakefulIntentService;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, AlarmService.class);
WakefulIntentService.sendWakefulWork(context, i);
}
}
和警报服务:
package com.something.app;
import android.app.PendingIntent;
import android.content.Intent;
import com.commonsware.cwac.wakeful.WakefulIntentService;
public class AlarmService extends WakefulIntentService {
public AlarmService() {
super("AlarmService");
}
@Override
protected void doWakefulWork(Intent arg0) {
//A looooooooot of stuff
}
有谁知道为什么BroadcastReceiver
有时不调用?
编辑:我听说设置 a BroadcastReceiver
which receive onBootCompleted
. 这是必需的吗?