Android程序意外Crash后自动重启
2015-04-09 16:48
232 查看
1、自定义UncaughtExceptionHandler
2、自定义Application
3、配置AndroidManifest.xml
public class UnCeHandler implements UncaughtExceptionHandler { private Thread.UncaughtExceptionHandler mDefaultHandler; public final String TAG = "CatchExcep"; CatchExcApplication application; public UnCeHandler(CatchExcApplication application) { // 获取系统默认的UncaughtException处理器 mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler(); this.application = application; } @Override public void uncaughtException(Thread thread, Throwable ex) { if (!handleException(ex) && mDefaultHandler != null) { // 如果用户没有处理则让系统默认的异常处理器来处理 mDefaultHandler.uncaughtException(thread, ex); } else { try { Thread.sleep(2000); } catch (InterruptedException e) { Log.e(TAG, "error : ", e); } Intent intent = new Intent(application.getApplicationContext(),MainActivity.class); PendingIntent restartIntent = PendingIntent.getActivity( application.getApplicationContext(), 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK); // 退出程序 AlarmManager mgr = (AlarmManager) application.getSystemService(Context.ALARM_SERVICE); mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000,restartIntent); // 1秒钟后重启应用 application.finishActivity(); } } /** * 自定义错误处理,收集错误信息 发送错误报告等操作均在此完成. * * @param ex * @return true:如果处理了该异常信息;否则返回false. */ private boolean handleException(Throwable ex) { if (ex == null) { return false; } // 使用Toast来显示异常信息 new Thread() { @Override public void run() { Looper.prepare(); Toast.makeText(application.getApplicationContext(),"很抱歉,程序出现异常,即将退出.", Toast.LENGTH_SHORT).show(); Looper.loop(); } }.start(); return true; }
2、自定义Application
public class CatchExcApplication extends Application { ArrayList<Activity> list = new ArrayList<Activity>(); public CatchExcApplication() { //设置该CrashHandler为程序的默认处理器 Thread.setDefaultUncaughtExceptionHandler(new UnCeHandler(this)); } /** * Activity关闭时,删除Activity列表中的Activity对象*/ public void removeActivity(Activity a){ list.remove(a); } /** * 向Activity列表中添加Activity对象*/ public void addActivity(Activity a){ list.add(a); } /** * 关闭Activity列表中的所有Activity*/ public void finishActivity(){ for (Activity activity : list) { if (null != activity) { activity.finish(); } } //杀死该应用进程 android.os.Process.killProcess(android.os.Process.myPid()); } }
3、配置AndroidManifest.xml
<application android:name="com.hht.restartdemo.CatchExcApplication" android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.hht.restartdemo.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
相关文章推荐
- 【Android项目】程序意外Crash后自动重启
- 【Android】App应用崩溃(Crash/Force Close)之后如何让它自动重启?
- 如何在Android程序Force Closed后自动重启
- Android程序:系统重启+开机自动运行+屏幕解锁
- 解决Android后台清理APP后,程序自动重启的问题
- Android换卡自动发短信小程序
- android.webkit.WebView 使用 SINGLE_COLUMN layout algorithm 导致程序意外退出的解决办法
- Android实现开机自动运行程序
- linux下通过脚本实现自动重启程序
- eclipse中开发android程序时,打开layout配置文件自动关闭的问题
- (转载整理)程序安装流程及如何实现文件在系统后自动重启(windows)
- Android实现开机自动运行程序
- 注册后自动关闭并重启程序
- Android开机自动启动程序设置
- android中程序更新下载完成后自动跳转安装界面
- Android开机自动运行程序
- android.webkit.WebView 使用 SINGLE_COLUMN layout algorithm 导致程序意外退出的解决办法
- 解决Android 自动关闭运行程序的方法
- WEB应用监控与自动重启的程序和脚本
- linux下通过脚本实现自动重启程序