AlarmManager有时不会开火

时间:2016-02-15 08:29:41

标签: android alarmmanager android-notifications

奇怪的问题,我的应用程序有成千上万的用户,对于他们中的大多数用户来说,这非常有效。有些人,比如说5%,会遇到导致提醒不被解雇的问题。

我已经记录了调度机制,包括预定的时间戳,所以我知道提醒已经安排,但是没有调用AlarmReceiver。

为什么AlarmManager有时不会开火?

ReminderLogic.java -

public class ReminderLogic {

    public static void schedule() {
        AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

        Intent intent = new Intent(context, AlarmReceiver.class);
        PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

        int timeMillis = ... // some calculation

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) 
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, timeMillis, alarmIntent);
        else 
            alarmManager.set(AlarmManager.RTC_WAKEUP, timeMillis, alarmIntent);
    }
}

AlarmReceiver.java -

public class AlarmReceiver extends WakefulBroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent service = new Intent(context, AlarmService.class);
        startWakefulService(context, service);
    }   
}

AlarmService.java -

public class AlarmService extends IntentService {
    private final static String TAG = "AlarmService";

    public AlarmService() {
        super(TAG);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        ReminderLogic.schedule();

        AlarmReceiver.completeWakefulIntent(intent);
    }
}

BootReceiver.java -

public class BootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        ReminderLogic.schedule();
    }

}

PackageReplaceReceiver.java -

public class PackageReplaceReceiver extends BroadcastReceiver {

    private static final String TAG = "BootReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        ReminderLogic.schedule();
    }

}

AndroidManifest.xml -

    <service android:name=".reminders.AlarmService" />
    <receiver android:name=".reminders.AlarmReceiver" />

    <receiver
        android:name=".reminders.BootReceiver"
        android:exported="true" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
    <receiver
        android:name=".reminders.PackageReplaceReceiver"
        android:exported="true" >
        <intent-filter>
            <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
        </intent-filter>
    </receiver>

0 个答案:

没有答案