【问题标题】:Alarm is killed when OS kills app当操作系统杀死应用程序时,警报被杀死
【发布时间】:2016-08-09 14:34:42
【问题描述】:

当操作系统终止应用程序时,我的警报被终止。我认为这是警报的要点之一,即使操作系统杀死了应用程序,它也会继续运行?我使用“./adb shell dumpsys alarm”命令检查警报的生命周期,每次操作系统杀死我的应用程序时,警报也消失了。我如何启动闹钟:

public static void startLocationAlarm(Context context){
    if(ActivityLifecycleHandler.isApplicationInForeground()) {
        return;   // If App is in foreground do not start alarm!
    }

    String alarm = Context.ALARM_SERVICE;
    AlarmManager am = ( AlarmManager ) context.getSystemService( alarm );

    Intent intent = new Intent(locationBroadcastAction);
    PendingIntent pi = PendingIntent.getBroadcast( context.getApplicationContext(), 0, intent, 0 );

    int type = AlarmManager.ELAPSED_REALTIME_WAKEUP;
    long interval = ONE_MINUTE;
    long triggerTime = SystemClock.elapsedRealtime() + interval;

    am.setRepeating(type, triggerTime, ONE_MINUTE, pi );    
}

为了添加更多上下文,我尝试在后台的服务(不是 IntentService)中进行一些定位操作。这是我的接收器。使用 Wakeful 是因为我不想在服务完成之前将其终止。

public class LocationBroadcastReceiver extends WakefulBroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent ) {

    Intent myIntent = new Intent( context, LocationServiceAlarmOwnGoogleClient.class );
    //context.startW( myIntent );
    LocationBroadcastReceiver.startWakefulService(context, myIntent);
}
}

有关更多信息:我在 OnStart 方法中取消了几个活动的警报,用户可以在后台返回这些活动。我不知道这是否会导致这种奇怪的行为?这是我的取消方法:

public static void stopLocationAlarm(Context context){
    Intent intent = new Intent(locationBroadcastAction);
    PendingIntent sender = PendingIntent.getBroadcast(context.getApplicationContext(), 0, intent, 0);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
    alarmManager.cancel(sender);
}

【问题讨论】:

  • 这是默认行为
  • “每次操作系统杀死我的应用程序,警报也消失了”——请详细解释一下“操作系统杀死我的应用程序”是什么意思。
  • @Shaishav Aaah,好的。我认为即使应用程序被操作系统杀死,警报也应该继续存在。即使操作系统杀死它,我怎样才能让它继续存在?
  • 我要指出 Commonsware 的另一个答案:stackoverflow.com/questions/9101818/…
  • @CommonsWare 当我将我的应用程序放在后台一段时间系统会杀死它(或者这就是我认为它所做的,因为它突然在 Android Studio 中显示为 DEAD)。我想它这样做是为了节省资源或其他东西。发生这种情况时,警报会被取消。如果用户杀死它,我不介意警报被取消。

标签: android alarmmanager


【解决方案1】:

您可以添加监听手机开机回调的服务。 将此权限添加到清单中

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

并注册接收者

<receiver android:name=".util.notification.local.MyBootCompletedService">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
</receiver>

public class MyBootCompletedService extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        AlarmReceiver.startLocalNotificationService(context);
    }
}

【讨论】:

  • 嗨!感谢您的回答!但我不希望警报或服务在启动时运行。只有当用户已经打开我的应用程序,然后将其置于后台时,我才希望运行触发服务的警报。
【解决方案2】:

导致取消闹钟的错误实际上与代码无关,而是与华为设备上的特殊电池设置有关。如果您的应用没有在“受保护的应用”中设置为“受保护”,系统会在杀死该应用时取消您的警报。将您的应用程序添加到“受保护的应用程序”将解决此问题。小米设备也是如此。必须将它们添加到“受保护的应用程序”,然后警报将按预期工作。感谢@CommonsWare 引导我找到解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-27
    • 1970-01-01
    • 2013-09-19
    • 1970-01-01
    • 1970-01-01
    • 2018-10-01
    • 1970-01-01
    相关资源
    最近更新 更多