如何崩溃后自动重启的活动? [英] How to restart an Activity automatically after it crashes?

查看:148
本文介绍了如何崩溃后自动重启的活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法对我来说,创建一个服务来跟踪我的活动类和崩溃后重新启动?请注意,我不能使用uncaughthandlers线程的方法重新启动我的应用程序。我的应用程序应该崩溃,不用担心那部分。我的应用程序是简单的东西,像这样

Is there a way for me to create a service to track my activity class and restart it after a crash? Note that i CANNOT use uncaughthandlers thread method to restart my app. My app is supposed to crash, don't worry about that part. My app is something simple, like this

private class AudioRenderer extends Activity {

private MediaPlayer AudioRenderer(String filePath) {
File location = new File(filePath);
        Uri path = Uri.fromFile(location);
  mp= MediaPlayer.create(this, path); 

}
return mp


}

在这个崩溃,服务听在后台会自动重新启动我的应用程序。任何人都知道这怎么可能?谢谢!

Once this crashes, the service listening in the background will restart my app automatically. Anybody knows how this is possible? Thanks!

推荐答案

您可以做到这一点,是的,如下所述。但是,如果这样的技术可能是有意义的实验,它们是绝对不适合制作。这将是非常丑陋,效率不高。

You can do that, yes, as explained below. But if such techniques may make sense for experiments, they are definitely not suitable for production. That would be awfully ugly and unefficient.

本说,这里是很长的路要走:

This said, here is a way to go:

  • 请您服务交还以确保它总是会已启动一次,而不是明确地停止后运行。

  • Make your Service sticky or redelivered to ensure it will always be running after having been started once and not explicitely stopped.

活动类,静态的WeakReference 的指点下存储的所有正在运行的实例,并提供一个的方式静态地检查是否它们中的至少一个当前分配:

in your Activity class, statically store WeakReferences pointing to all its running instances and provide a way to statically check whether at least one of them is currently allocated:

public class MyActivity extends Activity {
    private static ArrayList<WeakReference<MyActivity >> sMyInstances = new ArrayList<WeakReference<MyActivity >>();

    public MyActivity() {
        sMyInstances.add(new WeakReference<MyActivity >(this));            
    }

    private static int nbInstances() {
        int ret = 0;
        final int size = sMyInstances.size();

        for (int ctr = 0; ctr < size; ++ctr) {
            if (sMyInstances.get(ctr).get() != null) {
                ret++; 
            }
        }

        return ret;
    }
}

的WeakReference 是引用不这样做prevent这些对象被垃圾收集的对象,更多详细信息的这里

(WeakReference are references to objects that do not prevent these objects to be garbage-collected, more details here)

  • 然后,从你的服务,呼叫 MyActivity.nbInstances()不时。它会返回一个0(通常是短的,但在理论上未predictable),而最后一个运行的大跌后 MyActivity 实例。 警告:将这样做,除非你有一个有关本活动内存泄漏 或它的基本上下文,因为这会泄露prevent垃圾收集崩溃的实例。

  • Then, from your Service, call MyActivity.nbInstances() from time to time. It will return 0 a (usually short but theoretically unpredictable) while after the crash of the last running MyActivity instance. Warning: it will do so unless you have a memory leak concerning this Activity or its underlying Context as this leak would prevent the garbage collection of the instance that crashed.

然后你只需使用开始你的活动服务的新实例 startActivity(意向)

Then you just have to start a new instance of your Activity from your Service, using startActivity(Intent)

这篇关于如何崩溃后自动重启的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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