Android小技巧(一):实现捕获应用的运行时异常
2014-01-06 10:40
609 查看
转载请注明出处!本博客地址:http://blog.csdn.net/mylzc
由于Android设备各异,第三方定制的Android系统也非常多,我们不可能对所有的设备场景都进行测试,因而开发一款完全无bug的应用几乎是不可能的任务,那么当应用在用户的设备上Force Close时,我们是不是可以捕获这个错误,记录用户的设备信息,然后让用户选择是否反馈这些堆栈信息,通过这种bug反馈方式,我们可以有针对性地对bug进行修复。
当我们的的应用由于运行时异常导致Force Close的时候,可以设置主线程的UncaughtExceptionHandler,实现捕获运行时异常的堆栈信息。同时用户可以把堆栈信息通过发送邮件的方式反馈给我们。下面是实现的代码:
代码下载请按此
例子:点击按钮后,会触发一个NullPointerException的运行时异常,这个例子实现了捕获运行时异常,发送邮件反馈设备和堆栈信息的功能。
界面1(触发运行时异常)
界面2(发送堆栈信息)
TestActivity.java
[java] view
plaincopy
package com.zhuozhuo;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.Thread.UncaughtExceptionHandler;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
public class TestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {//给主线程设置一个处理运行时异常的handler
@Override
public void uncaughtException(Thread thread, final Throwable ex) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
ex.printStackTrace(pw);
StringBuilder sb = new StringBuilder();
sb.append("Version code is ");
sb.append(Build.VERSION.SDK_INT + "\n");//设备的Android版本号
sb.append("Model is ");
sb.append(Build.MODEL+"\n");//设备型号
sb.append(sw.toString());
Intent sendIntent = new Intent(Intent.ACTION_SENDTO);
sendIntent.setData(Uri.parse("mailto:csdn@csdn.com"));//发送邮件异常到csdn@csdn.com邮箱
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "bug report");//邮件主题
sendIntent.putExtra(Intent.EXTRA_TEXT, sb.toString());//堆栈信息
startActivity(sendIntent);
finish();
}
});
findViewById(R.id.button).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Integer a = null;
a.toString();//触发nullpointer运行时错误
}
});
}
}
main.xml
[html] view
plaincopy
<?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" />
<EditText android:id="@+id/editText1" android:layout_width="match_parent"
android:text="点击按钮触发运行时异常" android:layout_height="wrap_content"
android:layout_weight="1" android:gravity="top"></EditText>
<Button android:text="按钮" android:id="@+id/button"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"></Button>
</LinearLayout>
转载请注明出处!本博客地址:http://blog.csdn.net/mylzc
由于Android设备各异,第三方定制的Android系统也非常多,我们不可能对所有的设备场景都进行测试,因而开发一款完全无bug的应用几乎是不可能的任务,那么当应用在用户的设备上Force Close时,我们是不是可以捕获这个错误,记录用户的设备信息,然后让用户选择是否反馈这些堆栈信息,通过这种bug反馈方式,我们可以有针对性地对bug进行修复。
当我们的的应用由于运行时异常导致Force Close的时候,可以设置主线程的UncaughtExceptionHandler,实现捕获运行时异常的堆栈信息。同时用户可以把堆栈信息通过发送邮件的方式反馈给我们。下面是实现的代码:
代码下载请按此
例子:点击按钮后,会触发一个NullPointerException的运行时异常,这个例子实现了捕获运行时异常,发送邮件反馈设备和堆栈信息的功能。
界面1(触发运行时异常)
界面2(发送堆栈信息)
TestActivity.java
[java] view
plaincopy
package com.zhuozhuo;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.Thread.UncaughtExceptionHandler;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
public class TestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {//给主线程设置一个处理运行时异常的handler
@Override
public void uncaughtException(Thread thread, final Throwable ex) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
ex.printStackTrace(pw);
StringBuilder sb = new StringBuilder();
sb.append("Version code is ");
sb.append(Build.VERSION.SDK_INT + "\n");//设备的Android版本号
sb.append("Model is ");
sb.append(Build.MODEL+"\n");//设备型号
sb.append(sw.toString());
Intent sendIntent = new Intent(Intent.ACTION_SENDTO);
sendIntent.setData(Uri.parse("mailto:csdn@csdn.com"));//发送邮件异常到csdn@csdn.com邮箱
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "bug report");//邮件主题
sendIntent.putExtra(Intent.EXTRA_TEXT, sb.toString());//堆栈信息
startActivity(sendIntent);
finish();
}
});
findViewById(R.id.button).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Integer a = null;
a.toString();//触发nullpointer运行时错误
}
});
}
}
main.xml
[html] view
plaincopy
<?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" />
<EditText android:id="@+id/editText1" android:layout_width="match_parent"
android:text="点击按钮触发运行时异常" android:layout_height="wrap_content"
android:layout_weight="1" android:gravity="top"></EditText>
<Button android:text="按钮" android:id="@+id/button"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"></Button>
</LinearLayout>
转载请注明出处!本博客地址:http://blog.csdn.net/mylzc
相关文章推荐
- Android小技巧(一):实现捕获应用的运行时异常
- Android小技巧(一):实现捕获应用的运行时异常
- Android小技巧(一):实现捕获应用的运行时异常
- Android小技巧:实现捕获应用的运行时异常 分类: Android其他 2015-06-23 12:26 12人阅读 评论(0) 收藏
- Android小技巧:实现捕获应用的运行时异常
- android应用捕获运行异常发送反馈邮件的实现
- Android全局异常捕获,不退出应用,让应用正常运行下去!
- 安卓实现分业务模块异常捕获,全局异常不崩溃,应用继续运行!
- 实现捕获应用的运行时异常
- Android异常捕获篇(下)---retrofit实现文件的上传
- Android 捕获异常并在应用崩溃后重启应用
- 怎样实现了捕获应用中的日志在android开发中
- Android处理未捕获的异常(应用全局异常)
- Android捕获异常的实现
- android app记录运行日志 捕获奔溃异常 ,存储日志到文件
- 安卓捕获应用的运行时异常并保存代码
- android 捕获运行时异常和运行时错误
- android 捕获应用异常并保存入日志文件中
- .NET 异常捕获机制的小技巧应用(实例:端口扫描程序)
- android 应用 捕获异常 重启应用