以编程方式崩溃后重新启动应用程序 - Android

发布于 2024-12-01 23:52:28 字数 540 浏览 1 评论 0原文

有没有办法让我的应用程序在崩溃时自动重新启动?我的应用程序只是一个简单的媒体渲染应用程序,但是它偶尔会崩溃(应该是这样)。这有可能吗?谢谢。我的代码看起来像这样

public void Play(){  if(mp != null) {
             mp.reset();
             mp.release();
             mp = null;
         }
AudioRenderer mr = new AudioRenderer(); 
mp = mr.AudioRenderer(filePath);
}

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 my to program my app to automatically restart itself whenever it crashes? My app is just a simple media rendering App, however it occasionally crashes ( it's supposed to ). Is this at all possible? Thanks. My code looks like this

public void Play(){  if(mp != null) {
             mp.reset();
             mp.release();
             mp = null;
         }
AudioRenderer mr = new AudioRenderer(); 
mp = mr.AudioRenderer(filePath);
}

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
}

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

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

发布评论

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

评论(2

我也只是我 2024-12-08 23:52:28

这将为你完成这项工作。

如何启动自动停止的android服务?

我仍然不明白为什么它会崩溃。

更新

为未捕获的异常创建一个处理程序,您为未捕获的异常注册一个处理程序

    private Thread.UncaughtExceptionHandler onRuntimeError= new Thread.UncaughtExceptionHandler() {
        public void uncaughtException(Thread thread, Throwable ex) {
            //Try starting the Activity again
    };

您在创建时

    @Override
    protected void onCreate() { 
        super.onCreate();
        Thread.setDefaultUncaughtExceptionHandler(onRuntimeError);  
    }

this will do the job for you.

How to start the automatically stopped android service?

still i don't understand why it is supposed to crash.

UPDATE

you create an handler for uncaught exception

    private Thread.UncaughtExceptionHandler onRuntimeError= new Thread.UncaughtExceptionHandler() {
        public void uncaughtException(Thread thread, Throwable ex) {
            //Try starting the Activity again
    };

in your on create, you register an handler for uncaught exception

    @Override
    protected void onCreate() { 
        super.onCreate();
        Thread.setDefaultUncaughtExceptionHandler(onRuntimeError);  
    }
悲凉≈ 2024-12-08 23:52:28

我可能会迟到很多次,但我找到了解决您问题的二合一解决方案。

public void doRestart() {
    Intent mStartActivity = new Intent(context, LoginActivity.class);
    int mPendingIntentId = 123456;
    PendingIntent mPendingIntent = PendingIntent.getActivity(context, mPendingIntentId, mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
    System.exit(0);
}

private void appInitialization() {
    defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
    Thread.setDefaultUncaughtExceptionHandler(_unCaughtExceptionHandler);
}

//make crash report on ex.stackreport
private Thread.UncaughtExceptionHandler defaultUEH;
// handler listener
private Thread.UncaughtExceptionHandler _unCaughtExceptionHandler = new Thread.UncaughtExceptionHandler() {
    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
        ex.printStackTrace();
        doRestart();
    }
};



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_index);
    appInitialization();

I might be late a lot, but I found 2 in 1 solution for your problem.

public void doRestart() {
    Intent mStartActivity = new Intent(context, LoginActivity.class);
    int mPendingIntentId = 123456;
    PendingIntent mPendingIntent = PendingIntent.getActivity(context, mPendingIntentId, mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
    System.exit(0);
}

private void appInitialization() {
    defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
    Thread.setDefaultUncaughtExceptionHandler(_unCaughtExceptionHandler);
}

//make crash report on ex.stackreport
private Thread.UncaughtExceptionHandler defaultUEH;
// handler listener
private Thread.UncaughtExceptionHandler _unCaughtExceptionHandler = new Thread.UncaughtExceptionHandler() {
    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
        ex.printStackTrace();
        doRestart();
    }
};



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_index);
    appInitialization();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文