概述
每个android应用都是由一个Application和多个activity或者server构成.应用启动时,会首先启动Application.在Application的onCreate方法中调用
Thread.setDefaultUncaughtExceptionHandler(handler);
就可以捕获导致应用崩溃的错误信息了.
首先应用要有读写sd卡权限
自定义一个Application,并在AndroidManifest.xml中使用这个Application
android:name=".MyApplication">
...
public class MyApplication extends Application {
private static final String LOG_DIR = Environment
.getExternalStorageDirectory().getAbsolutePath() + "/oldfeel/log/";
private static final String LOG_NAME = getCurrentDateString() + ".txt";
private ArrayList list = new ArrayList();
@Override
public void onCreate() {
super.onCreate();
Thread.setDefaultUncaughtExceptionHandler(handler);
}
UncaughtExceptionHandler handler = new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
writeErrorLog(ex);
Intent intent = new Intent(getApplicationContext(),
CollapseActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
exit();
}
};
/**
* 打印错误日志
*
* @param ex
*/
protected void writeErrorLog(Throwable ex) {
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();
}
}
Log.d("example", "崩溃信息n" + info);
File dir = new File(LOG_DIR);
if (!dir.exists()) {
dir.mkdirs();
}
File file = new File(dir, LOG_NAME);
try {
FileOutputStream fileOutputStream = new FileOutputStream(file, true);
fileOutputStream.write(info.getBytes());
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取当前日期
*
* @return
*/
private static String getCurrentDateString() {
String result = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd",
Locale.getDefault());
Date nowDate = new Date();
result = sdf.format(nowDate);
return result;
}
/**
* 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 exit() {
for (Activity activity : list) {
if (null != activity) {
activity.finish();
}
}
// 杀死该应用进程
android.os.Process.killProcess(android.os.Process.myPid());
}
}
系统错误后要还是要提示用户系统错误.这个是崩溃activity,
android:name="com.example.test.CollapseActivity"
android:theme="@android:style/Theme.Holo.Dialog.MinWidth" >
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal" >
android:id="@+id/collapse_restart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:text="重启应用" />
android:id="@+id/collapse_exit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:text="退出应用" />
public class CollapseActivity extends Activity {
private Button btnRestart, btnExit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.collapse_activity);
setTitle("应用崩溃了");
btnRestart = (Button) findViewById(R.id.collapse_restart);
btnExit = (Button) findViewById(R.id.collapse_exit);
btnRestart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),
MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
}
});
btnExit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
最后
以上就是彩色小伙为你收集整理的android记录崩溃日志上传后台,android 记录崩溃日志的全部内容,希望文章能够帮你解决android记录崩溃日志上传后台,android 记录崩溃日志所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复