日志崩溃重新启动
2015-10-09 09:31
274 查看
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="我要崩溃咯"
android:textSize="20sp"/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/show"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
android:padding="10dp"
android:text="点我崩溃" />
</RelativeLayout>
MainActivity:
[java] view
plaincopy
package com.lzy.gesture;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private Button mButton;
private TextView mTextView;
private MyApplication myApplication;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myApplication = (MyApplication) getApplication();
myApplication.init();
myApplication.addActivity(this);
mButton = (Button) findViewById(R.id.btn);
mTextView = (TextView) findViewById(R.id.show);
mButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
pressed();
}
});
}
//人工制造异常崩溃
private void pressed() {
new Thread(new Runnable() {
@Override
public void run() {
mTextView.setText("beng...");
}
}).start();
}
}
MyApplication:在启动APP的时候一直开启application用于监听异常
[java] view
plaincopy
package com.lzy.gesture;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.Application;
public class MyApplication extends Application {
List<Activity> list = new ArrayList<Activity>();
public void init(){
//设置该CrashHandler为程序的默认处理器
MyUncaughtExceptionHandler catchException = new MyUncaughtExceptionHandler(this);
Thread.setDefaultUncaughtExceptionHandler(catchException);
}
/**
* 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());
}
}
package com.lzy.gesture;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.Application;
public class MyApplication extends Application {
List<Activity> list = new ArrayList<Activity>();
public void init(){
//设置该CrashHandler为程序的默认处理器
MyUncaughtExceptionHandler catchException = new MyUncaughtExceptionHandler(this);
Thread.setDefaultUncaughtExceptionHandler(catchException);
}
/**
* 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());
}
}
MyUncaughtExceptionHandler:处理崩溃异常
[java] view
plaincopy
package com.lzy.gesture;
import java.lang.Thread.UncaughtExceptionHandler;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Looper;
import android.widget.Toast;
public class MyUncaughtExceptionHandler implements UncaughtExceptionHandler {
private MyApplication myApplication;
private Thread.UncaughtExceptionHandler mUncaughtExceptionHandler;
public MyUncaughtExceptionHandler(MyApplication myApplication) {
this.myApplication = myApplication;
mUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();// 获取系统默认的异常处理器
}
@Override
public void uncaughtException(Thread thread, Throwable ex) {
if (!handleException(ex) && mUncaughtExceptionHandler != null) {
//如果用户没有处理则让系统默认的异常处理器来处理
mUncaughtExceptionHandler.uncaughtException(thread, ex);
}else {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Intent intent = new Intent(myApplication.getApplicationContext(), MainActivity.class);
//重启应用,得使用PendingIntent
PendingIntent restartIntent = PendingIntent.getActivity(
myApplication.getApplicationContext(), 0, intent,
Intent.FLAG_ACTIVITY_NEW_TASK);
//退出程序
AlarmManager mAlarmManager = (AlarmManager) myApplication.getSystemService(Context.ALARM_SERVICE);
mAlarmManager.set(AlarmManager.RTC, System.currentTimeMillis() + 1000,
restartIntent); // 1秒钟后重启应用
myApplication.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(myApplication.getApplicationContext(), "很抱歉,程序出现异常,一秒钟后重启.",
Toast.LENGTH_SHORT).show();
Looper.loop();
}
}.start();
return true;
}
}
刚开始碰到了
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lzy.gesture/com.lzy.gesture.MainActivity}: java.lang.ClassCastException: android.app.Application
cannot be cast to com.lzy.gesture.MyApplication
的报错,原来是忘了在manifast中application节点添加MyApplication了。如下:
[html] view
plaincopy
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lzy.gesture"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:name="com.lzy.gesture.MyApplication">
<activity
android:name=".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>
</manifest>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="我要崩溃咯"
android:textSize="20sp"/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/show"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
android:padding="10dp"
android:text="点我崩溃" />
</RelativeLayout>
MainActivity:
[java] view
plaincopy
package com.lzy.gesture;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private Button mButton;
private TextView mTextView;
private MyApplication myApplication;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myApplication = (MyApplication) getApplication();
myApplication.init();
myApplication.addActivity(this);
mButton = (Button) findViewById(R.id.btn);
mTextView = (TextView) findViewById(R.id.show);
mButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
pressed();
}
});
}
//人工制造异常崩溃
private void pressed() {
new Thread(new Runnable() {
@Override
public void run() {
mTextView.setText("beng...");
}
}).start();
}
}
MyApplication:在启动APP的时候一直开启application用于监听异常
[java] view
plaincopy
package com.lzy.gesture;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.Application;
public class MyApplication extends Application {
List<Activity> list = new ArrayList<Activity>();
public void init(){
//设置该CrashHandler为程序的默认处理器
MyUncaughtExceptionHandler catchException = new MyUncaughtExceptionHandler(this);
Thread.setDefaultUncaughtExceptionHandler(catchException);
}
/**
* 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());
}
}
package com.lzy.gesture;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.Application;
public class MyApplication extends Application {
List<Activity> list = new ArrayList<Activity>();
public void init(){
//设置该CrashHandler为程序的默认处理器
MyUncaughtExceptionHandler catchException = new MyUncaughtExceptionHandler(this);
Thread.setDefaultUncaughtExceptionHandler(catchException);
}
/**
* 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());
}
}
MyUncaughtExceptionHandler:处理崩溃异常
[java] view
plaincopy
package com.lzy.gesture;
import java.lang.Thread.UncaughtExceptionHandler;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Looper;
import android.widget.Toast;
public class MyUncaughtExceptionHandler implements UncaughtExceptionHandler {
private MyApplication myApplication;
private Thread.UncaughtExceptionHandler mUncaughtExceptionHandler;
public MyUncaughtExceptionHandler(MyApplication myApplication) {
this.myApplication = myApplication;
mUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();// 获取系统默认的异常处理器
}
@Override
public void uncaughtException(Thread thread, Throwable ex) {
if (!handleException(ex) && mUncaughtExceptionHandler != null) {
//如果用户没有处理则让系统默认的异常处理器来处理
mUncaughtExceptionHandler.uncaughtException(thread, ex);
}else {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Intent intent = new Intent(myApplication.getApplicationContext(), MainActivity.class);
//重启应用,得使用PendingIntent
PendingIntent restartIntent = PendingIntent.getActivity(
myApplication.getApplicationContext(), 0, intent,
Intent.FLAG_ACTIVITY_NEW_TASK);
//退出程序
AlarmManager mAlarmManager = (AlarmManager) myApplication.getSystemService(Context.ALARM_SERVICE);
mAlarmManager.set(AlarmManager.RTC, System.currentTimeMillis() + 1000,
restartIntent); // 1秒钟后重启应用
myApplication.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(myApplication.getApplicationContext(), "很抱歉,程序出现异常,一秒钟后重启.",
Toast.LENGTH_SHORT).show();
Looper.loop();
}
}.start();
return true;
}
}
刚开始碰到了
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lzy.gesture/com.lzy.gesture.MainActivity}: java.lang.ClassCastException: android.app.Application
cannot be cast to com.lzy.gesture.MyApplication
的报错,原来是忘了在manifast中application节点添加MyApplication了。如下:
[html] view
plaincopy
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lzy.gesture"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:name="com.lzy.gesture.MyApplication">
<activity
android:name=".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>
</manifest>
相关文章推荐
- Ubuntu 14.04 文件服务器--samba的安装和配置
- 空间域增强
- mysql常用命令
- struts2.3.3配置以及所需的jar包
- /run/systemd/private: No such file or directory
- Maven搭建SpringMVC+Mybatis项目详解
- AppleWatch开发Notification通知界面开发指南
- 深入理解Java:注解(Annotation)基本概念
- leetcode 104--Maximum Depth of Binary Tree
- 数据存储Introduce
- 0917 纯代码、SB、XIB自定义Cell
- Android-onInterceptTouchEvent()和onTouchEvent()总结
- 程序员如何快速从“码畜”到“中级水平”需要的10个秘诀
- c语言程序执行顺序结构简述
- 站内seo优化如何做?
- EM2040D采集软件SIS中grid实时显示问题(二)
- 循环和文本输入
- 第6周项目4数制转换
- 第4周项目3单链表应用3
- JS实现样式清新的横排下拉菜单效果