Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓:如何在应用程序启动后自动重启;强制关闭;?_Android - Fatal编程技术网

Android 安卓:如何在应用程序启动后自动重启;强制关闭;?

Android 安卓:如何在应用程序启动后自动重启;强制关闭;?,android,Android,在Android应用程序中,如果我们没有正确处理异常,通常会出现“强制关闭”错误 如果应用程序被强制关闭,如何自动重新启动 是否有任何特定的权限用于此操作?诀窍是首先确保它不会强制关闭 如果使用,则可以捕获导致应用程序强制关闭的异常 查看一个使用UncaughtExceptionHandler记录应用程序引发的异常的示例。要完成此任务,您必须做两件事: 避免“强制关闭”——应用程序崩溃的标准方式 设置崩溃发生时的重启机制 请参见下面的操作方法: 调用Thread.setDefaultUncaug

在Android应用程序中,如果我们没有正确处理异常,通常会出现“强制关闭”错误

如果应用程序被强制关闭,如何自动重新启动


是否有任何特定的权限用于此操作?

诀窍是首先确保它不会强制关闭

如果使用,则可以捕获导致应用程序强制关闭的异常


查看一个使用
UncaughtExceptionHandler
记录应用程序引发的异常的示例。

要完成此任务,您必须做两件事:

  • 避免“强制关闭”——应用程序崩溃的标准方式
  • 设置崩溃发生时的重启机制 请参见下面的操作方法:

  • 调用
    Thread.setDefaultUncaughtExceptionHandler()
    以捕获所有未捕获的异常,在这种情况下将调用
    uncaughtException()
    方法。“强制关闭”将不会出现,应用程序将没有响应,这不是一件好事。 要在应用程序崩溃时重新启动应用程序,应执行以下操作:

  • onCreate
    方法中,在主活动中初始化
    pendingent
    成员:

    Intent intent = PendingIntent.getActivity(
        YourApplication.getInstance().getBaseContext(),
        0,
        new Intent(getIntent()),
        getIntent().getFlags());
    
  • 然后将以下内容放入
    uncaughtException()
    方法中:

    AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 2000, intent);
    System.exit(2);
    
    您还必须调用
    System.exit()
    ,否则将无法工作。 这样,应用程序将在2秒后重新启动


    最终,您可以在应用程序崩溃的意图中设置一些标志,并在
    onCreate()
    方法中显示一个对话框“对不起,应用程序崩溃了,希望永远不要再出现:)”

    如果您使用Critercism或其他错误报告服务,则接受的答案几乎是正确的

    final UncaughtExceptionHandler defaultHandler = Thread.getDefaultUncaughtExceptionHandler();
    Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
                public void uncaughtException(Thread thread, Throwable ex) {
                  Intent launchIntent = new Intent(activity().getIntent());
                  PendingIntent pending = PendingIntent.getActivity(CSApplication.getContext(), 0,
                        launchIntent, activity().getIntent().getFlags());
                  getAlarmManager().set(AlarmManager.RTC, System.currentTimeMillis() + 2000, pending);
                  defaultHandler.uncaughtException(thread, ex);
                }
    });
    

    只需在包中添加这个类

    public class MyExceptionHandler implements
        java.lang.Thread.UncaughtExceptionHandler {
    private final Context myContext;
    private final Class<?> myActivityClass;
    
    public MyExceptionHandler(Context context, Class<?> c) {
        myContext = context;
        myActivityClass = c;
    }
    
    public void uncaughtException(Thread thread, Throwable exception) {
        StringWriter stackTrace = new StringWriter();
        exception.printStackTrace(new PrintWriter(stackTrace));
        System.err.println(stackTrace);// You can use LogCat too
        Intent intent = new Intent(myContext, myActivityClass);
        String s = stackTrace.toString();
        //you can use this String to know what caused the exception and in which Activity
        intent.putExtra("uncaughtException", "Exception is: " + stackTrace.toString());
        intent.putExtra("stacktrace", s);
        myContext.startActivity(intent);
        //for restarting the Activity
        Process.killProcess(Process.myPid());
        System.exit(0);
    }}
    

    下面是为我工作的代码。您应该在
    onCreate()中调用
    appInitialization()
    ,方法是
    MainActivity

    /*
    *基于崩溃逻辑的应用程序重启
    * */
    公共无效触发器重新启动(活动上下文){
    意向意向=新意向(上下文,MainActivity.class);
    intent.addFlags(intent.FLAG_活动_清除_顶部| intent.FLAG_活动_清除_任务| intent.FLAG_活动_新任务);
    背景。开始触觉(意图);
    if(活动的上下文实例){
    完成();
    }
    Runtime.getRuntime().exit(0);
    }
    私有void appInitialization(){
    defaultUEH=Thread.getDefaultUncaughtExceptionHandler();
    setDefaultUncaughtExceptionHandler(\u unCaughtExceptionHandler);
    }
    //在ex.stackreport上制作碰撞报告
    private Thread.UncaughtExceptionHandler defaultUEH;
    //处理程序侦听器
    private Thread.UncaughtExceptionHandler\u UncaughtExceptionHandler=new Thread.UncaughtExceptionHandler(){
    @凌驾
    public void uncaughtException(线程,可丢弃的ex){
    //您可以在此处编写代码以发送崩溃分析
    例如printStackTrace();
    触发重启(currentActivity);
    }
    };
    
    尝试正确处理异常。自动重新启动的应用程序可能会让用户感到恼火。如果应用程序崩溃,我只想重新启动它。我认为这比烦人更友好,尤其是当用户在我的应用程序中时。是的,我正在努力使每一个例外都正确无误@约翰尼:请分享你的问题的解决方案。如果出现任何异常,请检查以重新启动你的应用程序。谢谢你的提示。接下来的问题是何时调用UncaughtExceptionHandler.uncaughtException?如果用户没有单击“强制关闭”按钮,是否仍会调用UncaughtExceptionHandler.uncaughtException?@Johnny当应用程序引发未处理的异常时,您将获得强制关闭。如果使用UncaughtExceptionHandler,则应用程序可以处理其所有异常,用户将永远看不到强制关闭对话框。换句话说,在显示强制关闭对话框时调用UncaughtExceptionHandler。但是,您需要小心处理应用程序捕获的任何意外异常;继续并假装什么都没发生显然是有风险的。谢谢你的解释。它很好地解释了UncaughtExceptionHandler。但我见过在强制关闭后自动重启的应用程序。Launcher(也许不是一个很好的例子,但我见过第三方应用程序也是这样工作的)。如果我想让我的应用程序像这样工作呢?我必须使用某种系统权限吗?我想出来了。现在的问题是在哪里实现uncaughtException方法??请帮忙。谢谢。@mayumayoresan您可以扩展应用程序类或活动,并在onCreate()中执行以下操作:Thread.setDefaultUncaughtExceptionHandler(新UncaughtExceptionHandler(){…});如果您扩展活动,值得注意的是,您需要对每个可以作为应用程序入口点的活动(包括Android操作系统可能决定启动的活动,例如当窗口关闭时等)执行相同的操作在通过“管理应用程序”菜单强制停止进程后,它在ICS中似乎不起作用。你的应用程序是什么意思?我用了@Mahesh的建议,但没有用。请有人解释一下。此代码获取“Thread.setDefaultUncaughtExceptionHandler”,并在关闭后调用。感谢您提供答案。请编辑您的答案,包括对代码的解释,好吗?这将有助于未来的读者更好地理解正在发生的事情,尤其是那些对该语言不熟悉并努力理解这些概念的社区成员。与OP问题相关的关键要点是什么?为什么他们比现有五个答案中的建议更受欢迎?
    public class MyExceptionHandler implements
        java.lang.Thread.UncaughtExceptionHandler {
    private final Context myContext;
    private final Class<?> myActivityClass;
    
    public MyExceptionHandler(Context context, Class<?> c) {
        myContext = context;
        myActivityClass = c;
    }
    
    public void uncaughtException(Thread thread, Throwable exception) {
        StringWriter stackTrace = new StringWriter();
        exception.printStackTrace(new PrintWriter(stackTrace));
        System.err.println(stackTrace);// You can use LogCat too
        Intent intent = new Intent(myContext, myActivityClass);
        String s = stackTrace.toString();
        //you can use this String to know what caused the exception and in which Activity
        intent.putExtra("uncaughtException", "Exception is: " + stackTrace.toString());
        intent.putExtra("stacktrace", s);
        myContext.startActivity(intent);
        //for restarting the Activity
        Process.killProcess(Process.myPid());
        System.exit(0);
    }}
    
    Thread.setDefaultUncaughtExceptionHandler(new MyExceptionHandler(this,
                SplashScreenActivity.class));