android 记录崩溃日志
2016-10-15 18:40
274 查看
摘要: 在使用自己开发的android应用时,偶尔会出现 系统已停止运行 错误.这时候如果能记录错误日志,是非常有帮助的.
源地址: https://my.oschina.net/oldfeel/blog/187862
每个android应用都是由一个Application和多个activity或者server构成.应用启动时,会首先启动Application.在Application的onCreate方法中调用
就可以捕获导致应用崩溃的错误信息了.
首先应用要有读写sd卡权限
自定义一个Application,并在AndroidManifest.xml中使用这个Application
系统错误后要还是要提示用户系统错误.这个是崩溃activity,
源地址: https://my.oschina.net/oldfeel/blog/187862
每个android应用都是由一个Application和多个activity或者server构成.应用启动时,会首先启动Application.在Application的onCreate方法中调用
Thread.setDefaultUncaughtExceptionHandler(handler);
就可以捕获导致应用崩溃的错误信息了.
首先应用要有读写sd卡权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
自定义一个Application,并在AndroidManifest.xml中使用这个Application
<application android:name=".MyApplication"> ... </application>
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<Activity> list = new ArrayList<Activity>(); @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,
<activity android:name="com.example.test.CollapseActivity" android:theme="@android:style/Theme.Holo.Dialog.MinWidth" > </activity>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="horizontal" > <Button android:id="@+id/collapse_restart" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1.0" android:text="重启应用" /> <Button android:id="@+id/collapse_exit" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1.0" android:text="退出应用" /> </LinearLayout>
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中处理崩溃异常和记录日志
- Android中处理崩溃异常和记录日志
- Android中处理崩溃异常和记录日志
- Android全局捕获崩溃异常记录日志保存至本地并定时删除
- Android-记录崩溃日志
- Android中处理崩溃异常和记录日志(转)
- Android崩溃日志记录
- Android全局捕获崩溃异常记录日志保存至本地并定时删除
- Android 全局崩溃日志(CrashHandle)记录
- Android 中处理崩溃异常和记录日志
- Android中处理崩溃异常和记录日志
- Android中处理崩溃异常和记录日志
- [置顶] android 底层日志 开发框架 崩溃后进行记录 LogReport
- android 记录崩溃日志
- Android全局捕获崩溃异常记录日志保存至本地并定时删除
- Android开发之处理崩溃异常和记录日志
- 捕获android程序崩溃日志