Android app 崩溃后重新打开

该代码示例展示了一个自定义的`MyApplication`类,继承自`Application`,用于在Androidapp崩溃时自动重启。通过设置`UncaughtExceptionHandler`,当出现未捕获的异常时,应用会在1秒后使用`AlarmManager`和`PendingIntent`重新启动。在`MainActivity`的配置中,需指定`MyApplication`作为应用的名字。
摘要由CSDN通过智能技术生成

Android app 崩溃后重新打开

  1. 在Java代码中添加Application的继承类

    import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
    
    import android.app.AlarmManager;
    import android.app.Application;
    import android.app.PendingIntent;
    import android.content.Context;
    import android.content.Intent;
    
    /**
     * @author joker
     * @version 1.0
     * @date 2023/5/29 9:48
     */
    public class MyApplication extends Application {
    
        private static MyApplication application;
    
        // 1秒钟后重启应用
        private int time = 1000;
    
        @Override
        public void onCreate() {
            super.onCreate();
            application = this;
            // 程序崩溃时触发线程  以下用来捕获程序崩溃异常
            Thread.setDefaultUncaughtExceptionHandler(handler);
        }
    
        private Thread.UncaughtExceptionHandler handler = new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread t, Throwable e) {
                restartApp(); //发生崩溃异常时,重启应用
            }
        };
    
    
        private void restartApp() {
            Intent intent = new Intent(this, MainActivity.class);
            PendingIntent restartIntent = PendingIntent.getActivity(
                    application.getApplicationContext(), 0, intent, FLAG_ACTIVITY_NEW_TASK);
            //退出程序
            AlarmManager mgr = (AlarmManager) application.getSystemService(Context.ALARM_SERVICE);
            mgr.set(AlarmManager.RTC, System.currentTimeMillis() + time,
                    restartIntent);
    
            //结束进程之前可以把你程序的注销或者退出代码放在这段代码之前
            android.os.Process.killProcess(android.os.Process.myPid());
        }
    
    }
    
    • MainActivity.class:你程序的启动类
  2. 在AndroidManifest.xml中的application中添加android:name=“.MyApplication”

    <application
                 android:name=".MyApplication"
                 android:allowBackup="true"
                 android:dataExtractionRules="@xml/data_extraction_rules"
                 android:fullBackupContent="@xml/backup_rules"
                 android:icon="@mipmap/ic_logo"
                 android:label="@string/app_name"
                 android:roundIcon="@mipmap/ic_logo
                 android:supportsRtl="true"
                 android:theme="@style/Theme.test"
                 tools:targetApi="31">
        <activity
                  android:name=".ui.activity.MainActivity"
                  android:exported="true"
                  android:launchMode="singleTask">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值