現在使用Service
代替AlarmManager
是個好主意。改爲使用WakefulBroadcastReceiver
。你的設備睡着瞭然後拔掉。
public class BRMine extends WakefulBroadcastReceiver {
public static final String INTENT_FILTER = "com.example.BRMine";
@Override
public void onReceive(Context ctx, Intent intent) {
// TODO Auto-generated method stub
OWakeLocker.acquire(ctx, _.indexNOTS);
ComponentName comp = new ComponentName(ctx.getPackageName(),
SMine.class.getName());
startWakefulService(ctx, intent.setComponent(comp));
}
}
其中:
公共類OWakeLocker { 私有靜態PowerManager.WakeLock [] wakeLocks =新PowerManager.WakeLock [_ indexNOTS_MAX]; //服務計數
@SuppressWarnings("deprecation")
public static void acquire(Context ctx, int index) {
WakeLock wakeLock = wakeLocks[index];
if (wakeLock != null) wakeLock.release();
PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP |
PowerManager.ON_AFTER_RELEASE, _.APPNAME + Integer.toString(index));
if (wakeLock != null && wakeLock.isHeld()){
wakeLock.acquire();
}
}
public static void release(int index) {
WakeLock wakeLock = wakeLocks[index];
if (wakeLock != null)
wakeLock.release();
wakeLock = null;
}}
啓動:
Intent intent = new Intent(BRMine.INTENT_FILTER);
PendingIntent pi = PendingIntent.getBroadcast(ctx, myintentalarm, intent, PendingIntent.FLAG_CANCEL_CURRENT):
am.setExact(AlarmManager.RTC_WAKEUP, nexttime, pi);
我將嘗試與此WakefulBroadcastReceiver – rcorbellini
同樣的問題,我認爲問題是setExact當設備處於空閒狀態。 – rcorbellini