【问题标题】:How to stop the complete app when in any state it crushes.如何在应用崩溃的任何状态下停止整个应用。
【发布时间】:2015-12-28 08:41:10
【问题描述】:

我正在创建一个应用程序。它有许多活动和片段,例如它具有登录系统,然后将其他功能放入应用程序中。但是当应用程序崩溃时,它会保持登录状态。但参数保持为空。这就是应用程序的原因向 API 发送空值。但是现在我想在发生任何类型的问题(应用程序崩溃)时停止整个应用程序。任何人都可以建议我!!!!

【问题讨论】:

    标签: android


    【解决方案1】:

    你可以杀死整个应用程序并将其从正在运行的应用程序列表中通过 pid 使用以下行删除:

     int pid = android.os.Process.myPid();
    android.os.Process.killProcess(pid);
    

    【讨论】:

    • 但我想在应用程序不幸停止时停止它。而且我有很多页面和活动。该应用程序可以从任何活动中粉碎。那么,我在哪里可以编写此代码!
    【解决方案2】:
    Create a background service or Broadcast receiver and check with logged in parameter value ,if parameter value is null than call
    
    Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            finish();`enter code here`
            System.exit(0);
    

    【讨论】:

      【解决方案3】:

      由于在任何活动中的任何时间任何崩溃都可能发生这种情况,因此一种方法是在 defaultExceptionHandler 中捕获所有异常并在那里提供您的救助机制,即在任何此类异常上,终止应用程序,重置参数,等等

      链接:how to implement uncaughtException android

      http://developer.android.com/reference/java/lang/Thread.UncaughtExceptionHandler.html

      实现的要点

      • 设置默认异常处理程序来处理任何地方发生的任何崩溃。

        //Application.java

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
        }
        
      • 编写一个异常处理程序来处理任何崩溃。

        // ExceptionHandler.java

        公共类 ExceptionHandler 实现 java.lang.Thread.UncaughtExceptionHandler { 私人最终活动 ctx;

        public ExceptionHandler(Activity ctx) {
            this.ctx= ctx;
        }
        
        public void uncaughtException(Thread thread, Throwable exception) {
            // Send mail or upload exception details if required.
            // kill app.
            android.os.Process.killProcess(android.os.Process.myPid());
            System.exit(10);
        }
        

        }

      【讨论】:

        猜你喜欢
        • 2020-01-21
        • 2012-06-04
        • 2016-06-13
        • 2020-01-29
        • 2011-10-13
        • 1970-01-01
        • 2022-12-11
        • 2018-10-08
        • 2023-03-03
        相关资源
        最近更新 更多