我知道周围有类似的问题,但没有人帮助过我......我正试图设置一个重复警报来发射广播接收器,这将反过来发射一个意图服务。我知道打瞌睡模式推迟警报,我不介意。起初警报工作正常,但过了一段时间它根本没有交付。这就是我设置闹钟的方法:
public static void startRepeated(Context context){
Intent intent=new Intent(context,MyService.class);
//starting it manually first because API 19+ doesn't deliver exact alarms, but we want a run now
context.startService(intent);
intent = new Intent(context,ServiceReceiver.class);
int minutes=PreferenceManager.getDefaultSharedPreferences(context).getInt("timer",R.integer.timer_def);
final PendingIntent pendingIntent= PendingIntent.getBroadcast(context.getApplicationContext(),1,intent,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager=(AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(),minutes*60000,pendingIntent);
}
接收者的(WakefulBroadcastReceiver)onreceive方法:
@Override
public void onReceive(Context context, Intent intent) {
Log.e("SERV-RECEIVER ","triggered");
startWakefulService(context,new Intent(context,MyService.class));
}
请注意,当发生这种情况时,我不会看到SERV-RECEIVER日志,所以即使触发器也不会被触发。我的意图服务打开一个SQLite数据库,做一些工作,关闭它然后释放唤醒锁。接收器在android清单中注册,没有意图:
<receiver android:name=".ServiceReceiver" android:enabled="true"/>
我尝试使用adb命令将设备设置为打盹模式并将应用程序设置为待机状态,并且在我唤醒设备后它仍能正常工作。只有在我离开一段时间后才会出现问题。使用adb shell dumpsys警报,我注意到它始终可见(即使在警报停止后
trigerring):
u0a229:com.isoc.android.monitor +2s384ms running, 37 wakeups:
+2s384ms 37 wakes 37 alarms, last -6m33s804ms:
*walarm*:com.isoc.android.monitor/.ServiceReceiver
但是当问题发生时(从批处理部分),这会消失:
ELAPSED_WAKEUP #0: Alarm{99ac864 tag *walarm*:com.isoc.android.monitor/.ServiceReceiver type 2 when 340221779 com.isoc.android.monitor}
tag=*walarm*:com.isoc.android.monitor/.ServiceReceiver
type=2 whenElapsed=-971ms when=-5s971ms
window=+3m45s0ms repeatInterval=300000 count=0 flags=0x0
operation=PendingIntent{14dfacd: PendingIntentRecord{82c6f82 com.isoc.android.monitor broadcastIntent}}
我注意到在我的Android API 15手机上这个问题不会发生。只在我的Android marshmallow手机上......任何帮助都会非常受欢迎:) 感谢
答案 0 :(得分:0)
这是我的WakeLocker类
public class WakeLocker {
private static PowerManager.WakeLock wakeLock;
public static void setWakeLock(Context ctx) {
PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);
if (wakeLock != null) wakeLock.release();
wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "Vocabulary_Alarm");
wakeLock.acquire();
}
public static void resetWakeLock() {
if (wakeLock != null) wakeLock.release();
wakeLock = null;
} }
和接收者onReceive
public class AlarmReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
WakeLocker.setWakeLock(context);
Intent intentone = new Intent(context.getApplicationContext(), YourActivity.class);
intentone.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intentone.putExtra("startTest",true);
context.startActivity(intentone);
但是如果你看到我开始了我所需要的活动而不是服务。如果你的问题在使用WakeLock后仍未解决,你也可以尝试启动相关的Activiy。