Activity崩溃后如何自动重启?

发布于 2024-12-02 12:25:24 字数 417 浏览 0 评论 0 原文

有没有办法让我创建一个服务来跟踪我的活动类并在崩溃后重新启动它?请注意,我不能使用未捕获的处理程序线程方法来重新启动我的应用程序。我的应用程序应该会崩溃,不用担心那部分。我的应用程序很简单,就像这样

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


}

一旦崩溃,后台监听的服务将自动重新启动我的应用程序。有人知道这怎么可能吗?谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笨笨の傻瓜 2024-12-09 12:25:24

是的,您可以这样做,如下所述。但是,如果此类技术对于实验可能有意义,那么它们绝对不适合生产。那将是非常丑陋和低效的。

也就是说,这里有一个方法:

  • Make your Service 粘性重新传送以确保它会启动一次后始终运行,并且不会明确停止。

  • 在您的Activity类中,静态存储指向其所有正在运行的实例的WeakReference,并提供一种方法来静态检查当前是否至少分配了其中一个实例:< /p>

    public class MyActivity 扩展 Activity {
        私有静态ArrayList> sMyInstances = new ArrayList>();
    
        公共我的活动(){
            sMyInstances.add(new WeakReference(this));            
        }
    
        私有静态 int nbInstances() {
            int ret = 0;
            最终 int 大小 = sMyInstances.size();
    
            for (int ctr = 0; ctr < 大小; ++ctr) {
                if (sMyInstances.get(ctr).get() != null) {
                    ret++; 
                }
            }
    
            返回ret;
        }
    }
    

WeakReference 是对不会阻止这些对象被垃圾收集的对象的引用,更多详细信息此处)

  • 然后,从您的 Service 中,不时调用 MyActivity.nbInstances()。在最后一个运行的 MyActivity 实例崩溃后,它将返回 0 a(通常很短,但理论上不可预测)。 警告:除非您有 ,否则它会这样做关于此 Activity 或其底层 Context 的内存泄漏,因为此泄漏会阻止崩溃实例的垃圾回收。

  • 然后,您只需使用 startActivity(Intent)Service 启动 Activity 的新实例,

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.

  • 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 are references to objects that do not prevent these objects to be garbage-collected, more details here)

  • 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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文