android记录崩溃日志上传后台,android 记录崩溃日志

每个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();

}

});

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值