ueHandler = new UEHandler(this); // 设置异常处理实例 Thread.setDefaultUncaughtExceptionHandler(ueHandler);
// fetch Excpetion Info
String info = null;
ByteArrayOutputStream baos = null;
PrintStream printStream = null;
try {
baos = new ByteArrayOutputStream();
printStream = new PrintStream(baos);
ex.printStackTrace(printStream);
byte[] data = baos.toByteArray();
info = new String(data);
data = null;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (printStream != null) {
printStream.close();
}
if (baos != null) {
baos.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
package lab.sodino.errorreport;
import java.io.File;
import android.app.Application;
/**
* @author Sodino E-mail:sodinoopen@hotmail.com
* @version Time:2011-6-9 下午11:49:56
*/
public class SoftApplication extends Application {
/** "/data/data/<app_package>/files/error.log" */
public static final String PATH_ERROR_LOG = File.separator + "data" + File.separator + "data"
+ File.separator + "lab.sodino.errorreport" + File.separator + "files" + File.separator
+ "error.log";
/** 标识是否需要退出。为true时表示当前的Activity要执行finish()。 */
private boolean need2Exit;
/** 异常处理类。 */
private UEHandler ueHandler;
public void onCreate() {
need2Exit = false;
ueHandler = new UEHandler(this);
// 设置异常处理实例
Thread.setDefaultUncaughtExceptionHandler(ueHandler);
}
public void setNeed2Exit(boolean bool) {
need2Exit = bool;
}
public boolean need2Exit() {
return need2Exit;
}
}
package lab.sodino.errorreport;
import java.io.File;
import java.io.FileInputStream;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class ActOccurError extends Activity {
private SoftApplication softApplication;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
softApplication = (SoftApplication) getApplication();
// 一开始进入程序恢复为"need2Exit=false"。
softApplication.setNeed2Exit(false);
Log.d("ANDROID_LAB", "ActOccurError.onCreate()");
Button btnMain = (Button) findViewById(R.id.btnThrowMain);
btnMain.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Log.d("ANDROID_LAB", "Thread.main.run()");
int i = 0;
i = 100 / i;
}
});
Button btnChild = (Button) findViewById(R.id.btnThrowChild);
btnChild.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
new Thread() {
public void run() {
Log.d("ANDROID_LAB", "Thread.child.run()");
int i = 0;
i = 100 / i;
}
}.start();
}
});
// 处理记录于error.log中的异常
String errorContent = getErrorLog();
if (errorContent != null) {
Intent intent = new Intent(this, ActErrorReport.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("error", errorContent);
intent.putExtra("by", "error.log");
startActivity(intent);
}
}
public void onStart() {
super.onStart();
if (softApplication.need2Exit()) {
Log.d("ANDROID_LAB", "ActOccurError.finish()");
ActOccurError.this.finish();
} else {
// do normal things
}
}
/**
* 读取是否有未处理的报错信息。<br/>
* 每次读取后都会将error.log清空。<br/>
*
* @return 返回未处理的报错信息或null。
*/
private String getErrorLog() {
File fileErrorLog = new File(SoftApplication.PATH_ERROR_LOG);
String content = null;
FileInputStream fis = null;
try {
if (fileErrorLog.exists()) {
byte[] data = new byte[(int) fileErrorLog.length()];
fis = new FileInputStream(fileErrorLog);
fis.read(data);
content = new String(data);
data = null;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
if (fileErrorLog.exists()) {
fileErrorLog.delete();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return content;
}
}
package lab.sodino.errorreport;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
/**
* @author Sodino E-mail:sodinoopen@hotmail.com
* @version Time:2011-6-12 下午01:34:17
*/
public class ActErrorReport extends Activity {
private SoftApplication softApplication;
private String info;
/** 标识来处。 */
private String by;
private Button btnReport;
private Button btnCancel;
private BtnListener btnListener;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.report);
softApplication = (SoftApplication) getApplication();
by = getIntent().getStringExtra("by");
info = getIntent().getStringExtra("error");
TextView txtHint = (TextView) findViewById(R.id.txtErrorHint);
txtHint.setText(getErrorHint(by));
EditText editError = (EditText) findViewById(R.id.editErrorContent);
editError.setText(info);
btnListener = new BtnListener();
btnReport = (Button) findViewById(R.id.btnREPORT);
btnCancel = (Button) findViewById(R.id.btnCANCEL);
btnReport.setOnClickListener(btnListener);
btnCancel.setOnClickListener(btnListener);
}
private String getErrorHint(String by) {
String hint = "";
String append = "";
if ("uehandler".equals(by)) {
append = " when the app running";
} else if ("error.log".equals(by)) {
append = " when last time the app running";
}
hint = String.format(getResources().getString(R.string.errorHint), append, 1);
return hint;
}
public void onStart() {
super.onStart();
if (softApplication.need2Exit()) {
// 上一个退栈的Activity有执行“退出”的操作。
Log.d("ANDROID_LAB", "ActErrorReport.finish()");
ActErrorReport.this.finish();
} else {
// go ahead normally
}
}
class BtnListener implements Button.OnClickListener {
@Override
public void onClick(View v) {
if (v == btnReport) {
// 需要 android.permission.SEND权限
Intent mailIntent = new Intent(Intent.ACTION_SEND);
mailIntent.setType("plain/text");
String[] arrReceiver = { "sodinoopen@hotmail.com" };
String mailSubject = "App Error Info[" + getPackageName() + "]";
String mailBody = info;
mailIntent.putExtra(Intent.EXTRA_EMAIL, arrReceiver);
mailIntent.putExtra(Intent.EXTRA_SUBJECT, mailSubject);
mailIntent.putExtra(Intent.EXTRA_TEXT, mailBody);
startActivity(Intent.createChooser(mailIntent, "Mail Sending..."));
ActErrorReport.this.finish();
} else if (v == btnCancel) {
ActErrorReport.this.finish();
}
}
}
public void finish() {
super.finish();
if ("error.log".equals(by)) {
// do nothing
} else if ("uehandler".equals(by)) {
// 1.
// android.os.Process.killProcess(android.os.Process.myPid());
// 2.
// ActivityManager am = (ActivityManager)
// getSystemService(ACTIVITY_SERVICE);
// am.restartPackage("lab.sodino.errorreport");
// 3.
// System.exit(0);
// 1.2.3.都失效了,Google你让悲催的程序员情何以堪啊。
softApplication.setNeed2Exit(true);
// ////////////////////////////////////////////////////
// // 另一个替换方案是直接返回“HOME”
// Intent i = new Intent(Intent.ACTION_MAIN);
// // 如果是服务里调用,必须加入newtask标识
// i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// i.addCategory(Intent.CATEGORY_HOME);
// startActivity(i);
// ////////////////////////////////////////////////////
}
}
}
package lab.sodino.uncaughtexception;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import android.content.Intent;
import android.util.Log;
/**
* @author Sodino E-mail:sodinoopen@hotmail.com
* @version Time:2011-6-9 下午11:50:43
*/
public class UEHandler implements Thread.UncaughtExceptionHandler {
private SoftApplication softApp;
private File fileErrorLog;
public UEHandler(SoftApplication app) {
softApp = app;
fileErrorLog = new File(SoftApplication.PATH_ERROR_LOG);
}
@Override
public void uncaughtException(Thread thread, Throwable ex) {
// fetch Excpetion Info
String info = null;
ByteArrayOutputStream baos = null;
PrintStream printStream = null;
try {
baos = new ByteArrayOutputStream();
printStream = new PrintStream(baos);
ex.printStackTrace(printStream);
byte[] data = baos.toByteArray();
info = new String(data);
data = null;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (printStream != null) {
printStream.close();
}
if (baos != null) {
baos.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
// print
long threadId = thread.getId();
Log.d("ANDROID_LAB", "Thread.getName()=" + thread.getName() + " id=" + threadId + " state=" + thread.getState());
Log.d("ANDROID_LAB", "Error[" + info + "]");
if (threadId != 1) {
// 此处示例跳转到汇报异常界面。
Intent intent = new Intent(softApp, ActErrorReport.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("error", info);
intent.putExtra("by", "uehandler");
softApp.startActivity(intent);
} else {
// 此处示例发生异常后,重新启动应用
Intent intent = new Intent(softApp, ActOccurError.class);
// 如果<span style="background-color: rgb(255, 255, 255); ">没有NEW_TASK标识且</span>是UI线程抛的异常则界面卡死直到ANR
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
softApp.startActivity(intent);
// write 2 /data/data/<app_package>/files/error.log
write2ErrorLog(fileErrorLog, info);
// kill App Progress
android.os.Process.killProcess(android.os.Process.myPid());
}
}
private void write2ErrorLog(File file, String content) {
FileOutputStream fos = null;
try {
if (file.exists()) {
// 清空之前的记录
file.delete();
} else {
file.getParentFile().mkdirs();
}
file.createNewFile();
fos = new FileOutputStream(file);
fos.write(content.getBytes());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Throws Exception By Main Thread"
android:id="@+id/btnThrowMain"
></Button>
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Throws Exception By Child Thread"
android:id="@+id/btnThrowChild"
></Button>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/errorHint"
android:id="@+id/txtErrorHint" />
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/editErrorContent"
android:editable="false" android:layout_weight="1"></EditText>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:background="#96cdcd"
android:gravity="center" android:orientation="horizontal">
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Report"
android:id="@+id/btnREPORT" android:layout_weight="1"></Button>
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Cancel"
android:id="@+id/btnCANCEL" android:layout_weight="1"></Button>
</LinearLayout>
</LinearLayout>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有