Android:如何在应用程序被“强制关闭”后自动重启?

zzzyeukh  于 5个月前  发布在  Android
关注(0)|答案(8)|浏览(143)

在Android应用程序中,如果我们没有正确处理异常,我们通常会得到“强制关闭”错误。
如果我的应用程序被强制关闭,我如何自动重新启动它?
是否有特定的权限用于此?

vfwfrxfs

vfwfrxfs1#

要做到这一点,你必须做两件事:
1.避免“强制关闭”-应用程序崩溃的标准方式。
1.设置一个重新启动机制,当崩溃发生时,无论如何。
下面看看如何做到这一点:
1.调用Thread.setDefaultUncaughtExceptionHandler()以捕获所有未捕获的异常,在这种情况下,将调用uncaughtException()方法。“强制关闭”不会出现,应用程序将没有响应,这不是一件好事。为了在应用程序崩溃时重新启动应用程序,您应该执行以下操作:
1.在onCreate方法中,在主Activity中初始化PendingIntent成员:

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秒后重新启动。
最后,你可以在Intent中设置一些标志,表明应用程序崩溃了,在onCreate()方法中,你可以显示一个对话框“I'm sorry,the application crashed,hope never again:)"。

e7arh2l6

e7arh2l62#

诀窍是首先确保它不会强制关闭。
如果使用Thread.setDefaultUncaughtExceptionHandler()方法,则可以捕获导致应用程序强制关闭的错误。
查看at this question,了解使用UncaughtExceptionHandler记录应用程序引发的错误的示例。

mu0hgdu0

mu0hgdu03#

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

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);
            }
});

字符串

ut6juiuv

ut6juiuv4#

只需将此类添加到包中即可

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);
}}

字符串
在您的应用程序或每个Activity类中,在onCreate()方法中只需调用:

Thread.setDefaultUncaughtExceptionHandler(new MyExceptionHandler(this,
            SplashScreenActivity.class));

nsc4cvqm

nsc4cvqm5#

public class ForceCloseExceptionHandalingActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setContentView(MyLayout());
        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread paramThread, Throwable paramThrowable) {
                myHandaling(paramThread, paramThrowable);
            }
        });
    }

    private ViewGroup MyLayout(){
        LinearLayout mainLayout = new LinearLayout(this);
        mainLayout.setOrientation(LinearLayout.VERTICAL);  
        Button btnHello =new Button(this);
        btnHello.setText("Show all button");
        btnHello.setOnClickListener(new OnClickListener() {         
            @Override
            public void onClick(View v) {                   
                setContentView(MyLayout2());            
            }
        });             
        mainLayout.addView(btnHello);       
        return mainLayout;
    }

    private ViewGroup MyLayout2(){
        LinearLayout mainLayout = new LinearLayout(this);
        mainLayout.setOrientation(LinearLayout.VERTICAL);  
        Button btnHello =new Button(this);
        btnHello.setText("I am a EEROR uncaughtException");
        btnHello.setOnClickListener(new OnClickListener() {         
            @Override
            public void onClick(View v) {                   
                Log.e("Alert","btn  uncaughtException::");
                Toast.makeText(ForceCloseExceptionHandalingActivity.this, "Alert uncaughtException222",Toast.LENGTH_LONG).show();
                View buttone = null;
                setContentView(buttone);            
            }
        });     
        Button btnHello2 =new Button(this);
        btnHello2.setText("I am a EEROR Try n catch");
        btnHello2.setOnClickListener(new OnClickListener() {            
            @Override
            public void onClick(View v) {   

                try{
                    View buttone = null;
                    setContentView(buttone);
                }
                catch (Exception e) {
                    Log.e("Alert","Try n catch:::");
                    Toast.makeText(ForceCloseExceptionHandalingActivity.this, "Alert Try n catch",Toast.LENGTH_LONG).show();
                    setContentView(MyLayout());
                }

            }
        });     
        mainLayout.addView(btnHello);
        mainLayout.addView(btnHello2);
        return mainLayout;
    }
    public void myHandaling(Thread paramThread, Throwable paramThrowable){
        Log.e("Alert","Lets See if it Works !!!" +"paramThread:::" +paramThread +"paramThrowable:::" +paramThrowable);
        Toast.makeText(ForceCloseExceptionHandalingActivity.this, "Alert uncaughtException111",Toast.LENGTH_LONG).show();
        Intent in =new Intent(ForceCloseExceptionHandalingActivity.this,com.satya.ForceCloseExceptionHandaling.ForceCloseExceptionHandalingActivity.class);
        startActivity(in);
        finish();
        android.os.Process.killProcess(android.os.Process.myPid()); 
    }
    @Override
    protected void onDestroy() {
        Log.e("Alert","onDestroy:::");
        Toast.makeText(ForceCloseExceptionHandalingActivity.this, "Alert onDestroy",Toast.LENGTH_LONG).show();
        super.onDestroy();  
    }

字符串

vdgimpew

vdgimpew6#

下面是我的代码,你应该在MainActivityonCreate()方法中调用appInitialization()

/*
    * App Restart on crash logic
    * */

    public void triggerRestart(Activity context) {
        Intent intent = new Intent(context, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
        if (context instanceof Activity) {
            finish();
        }
        Runtime.getRuntime().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) {
            // You can code here to send crash analytics
            ex.printStackTrace();
            triggerRestart(currentActivity);
        }
    };

字符串

lo8azlld

lo8azlld7#

对于Kotlin用户,在MainActivity的onCreate()方法中调用这两个扩展扩展函数,就在超级调用之后,最好在通常在onCreate()中编写的任何其他代码之前。

fun Activity.handleUncaughtException() {
    Thread.setDefaultUncaughtExceptionHandler { _, throwable ->
       // here you can report the throwable exception to Sentry or Crashlytics or whatever crash reporting service you're using, otherwise you may set the throwable variable to _ if it'll remain unused
        val intent = Intent(this, MainActivity::class.java).apply {
            putExtra("isCrashed", true)
            addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
        }
        startActivity(intent)
        finish()
        Process.killProcess(Process.myPid())
        exitProcess(2)
    }
}

fun Activity.showUncaughtExceptionDialog() {
if (intent.getBooleanExtra("isCrashed", false)) {
    AlertDialog.Builder(this).apply {
        setTitle("Something went wrong.")
        setMessage("Something went wrong.\nWe'll work on fixing it.")
        setPositiveButton("OK") { _, _ -> }
    }.show()
}
}

字符串
我们称之为过程。(Process.myPid())和exitProcess(2),因为如果我们看看Android的开源代码,that's actually the default and proper handling that gets called by Thread. getDefaultUncaughtExceptionHandler(),它会导致我们的应用程序崩溃,并引发臭名昭著的ANR对话框,我们希望通过遵循原始实现而不是在uncaughtException()结束时做任何时髦的事情来成为好的Android公民,比如根本不崩溃或显示另一个活动(永远不要这样做)。
请注意,Kotlin的exitProcess()方法只是Java的System.exit()的 Package 器,无论您在构造函数中传递什么状态代码都无关紧要,我在代码中将其设置为2
请注意,在Intent中,我们将一个布尔标志“isCrashed”设置为true,我们将使用它来检测是否有意外异常导致应用重新启动,并相应地向用户显示一个对话框,通知他们崩溃。
showUncaughtExceptionDialog()扩展函数是可选的,但是如果由于崩溃而要重新启动应用程序,最好通知用户。

lvjbypge

lvjbypge8#

您可以使用app_watchdogd.sh观看并重新启动您的应用程序与root Android设备

相关问题