取消程序崩溃后报警 [英] cancelling an alarm after program crash

查看:106
本文介绍了取消程序崩溃后报警的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个后台服务,设置重复报警,执行其任务,并停止本身。然后,当警报被唤醒它再次开始服务。如果程序崩溃的警报仍然存在并唤醒报警广播接收器。有什么办法取消报警的崩溃? - 我想我也许可以消除来自任何异常有关其他原因的报警,但什么

I have a background service which sets a repeating alarm, does its task and stops itself. Then when the alarm wakes up it starts the service again. If the program crashes the alarm is still around and wakes up the alarm broadcastreceiver. Is there any way to cancel the alarm on a crash - I suppose I might be able to cancel the alarm from any caught exceptions but what about other causes?

或者当警报广播接收器被触发,有没有办法告诉如果设置它崩溃的计划?

Or when the alarm Broadcast receiver is triggered is there any way to tell if the program that set it has crashed?

推荐答案

活动的生命周期可以通过在<一个使用的 isFinishing()的功能从意外终止检测干净的完成HREF =htt​​p://developer.android.com/reference/android/app/Activity.html#onDestroy%28%29相对=nofollow>的onDestroy()回调。你可以在你的应用程序将它添加到每个活动:

The activity lifecycle can detect a clean finish from an "unexpected" termination through use of the isFinishing() function in the onDestroy() callback. You could add this to each activity in your app:

protected void onDestroy () {
    boolean crashedOnLastClose = false;

    if (!isFinishing()) {
        // The application is being destroyed by the O/S
        crashedOnLastClose = true;

        // Store the result somewhere for the BroadcastReceiver to check later
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean("crashedOnLastClose", crashedOnLastClose);
        editor.commit();
    }
}

然后在你的广播接收器,从共享preferences框架拉回来的结果(或任何你决定来存储它),并取消行动,如果该值为true:

Then in your BroadcastReceiver, pull the result back from the SharedPreferences framework (or wherever you decide to store it), and cancel the action if the value is true:

public class MyAlarmReceiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        boolean crashedOnLastClose = settings.getBoolean("crashedOnLastClose", false);

        if (!crashedOnLastClose) {
            // No crash on last run, handle the alarm
            // ...
        }
    }
}

请记住,虽然重置回false在下次启动!像这样的东西应该做的:

Remember to reset it back to false on next launch though! Something like this should do:

protected void onCreate (Bundle savedInstanceState) {
    // Make sure this always resets to FALSE on launch
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putBoolean("crashedOnLastClose", false);
    editor.commit();
}

这应该捕获你的崩溃事件;但请记住它还将检测其他时候,您的应用程序是强制关闭的 - 比如,如果内存不足的设备上。对于这些类型的事件区分开,你需要检查系统状态以及 isFinishing()

This should capture your crash events; but remember it will also detect other times when your app is force-closed - such as if the memory is low on the device. To distinguish between these kinds of events you would need to inspect the system status as well as isFinishing().

编辑 - 服务

您服务拥有独立的生命周期,你必须把它几乎像一个单独的应用程序。正如你已经注意到,有没有isFinishing()在这里,但服务实际上是相当简单的,因为它们只能由您的code,你可以捕获pretty轻松干净终止。

Your Service has a separate lifecycle, you have to treat it almost like a separate app. As you've noticed, there is no "isFinishing()" here, but Services are actually quite simple since they are only terminated cleanly by your code, which you can trap pretty easily.

在您的服务中,添加一个新的布尔(或许叫isFinishing),当您的服务与完成其设置为true。然后覆盖的onDestroy(),以及并添加类似于我为你的活动描述的检查:

In your Service, add a new boolean (perhaps called "isFinishing") and set it to true when your service is finished with. Then override onDestroy() as well and add a check similar to the one I described for your activities:

protected void onDestroy () {       
    // Store the result somewhere for the BroadcastReceiver to check later
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putBoolean("crashedOnLastClose", !isFinishing);
    editor.commit();
}

广播接收器将然后用同样的code刚才添加检测如果任一服务或活动崩溃。

The broadcast receiver will then use the same code we added earlier to detect if either the service or an activity crashed.

这篇关于取消程序崩溃后报警的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆