android全局捕获异常,重启应用
2017-07-31 14:42
513 查看
1.捕获异常类
3.manifest里面内容
public class CrashHandler implements Thread.UncaughtExceptionHandler { private Context context; public CrashHandler(Context context){ this.context=context; } @Override public void uncaughtException(Thread t, Throwable e) { showToast(t); } /** * 操作 * @param thread */ private void showToast(Thread thread) { final Dialog dialog=new Dialog(context); dialog.setContentView(R.layout.dialog_layout); new Thread(new Runnable() { @Override public void run() { Looper.prepare(); Toast.makeText(context, "程序异常,重新启动", Toast.LENGTH_LONG).show(); Looper.loop(); } }).start(); try { thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } restartApp(); } /** * 重启应用 */ private void restartApp(){ Intent intent = new Intent(context,MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); android.os.Process.killProcess(android.os.Process.myPid());//再此之前可以做些退出等操作 } }2.自定义Application
public class MyApplication extends Application { private static Context mContext; @Override public void onCreate() { super.onCreate(); mContext=this; Thread.setDefaultUncaughtExceptionHandler(new CrashHandler(mContext)); } }
3.manifest里面内容
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="test.com.crashtest"> <!--name注意修改--> <application android:allowBackup="true" android:name=".MyApplication" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".ActivityTest"></activity> </application> </manifest>
相关文章推荐
- Android处理未捕获的异常(应用全局异常)
- Android应用崩溃后异常捕获并重启
- android 应用 捕获异常 重启应用
- Android应用崩溃后异常捕获并重启并写入日志
- Android 全局异常捕获 重启app
- android 异常全局捕获并重启
- android艺术开发探索之 CrashHandler(全局捕获异常)和应用方法数越界
- Android应用崩溃后异常捕获并重启
- Android应用崩溃后异常捕获并重启
- Android 捕获异常并在应用崩溃后重启应用
- Android应用捕获全局异常自定义处理
- Android应用崩溃后异常捕获并重启
- android应用开发-------------应用崩溃全局异常捕获处理(UncaughtExceptionHandler)
- Android 捕获异常并在应用崩溃后重启应用
- Android全局异常捕获,不退出应用,让应用正常运行下去!
- Android应用崩溃后异常捕获并重启
- Android应用开发中全局异常的捕获
- 全局捕获应用程序异常,并在出现异常状态重启应用,保持错误日志到手机
- [转]Android应用崩溃后异常捕获并重启
- Android捕获全局异常,并对异常做出处理