1

在我的应用程序中,我有一个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 BroadcastReceiverwhich receive onBootCompleted. 这是必需的吗?

4

1 回答 1

1

所以,这就是问题所在:如果设备重新启动,它有时会清除警报,因此您必须在BroadcastReceiver接收onBootCompleted

于 2012-11-06T18:47:10.423 回答