Android Open Source - demo-app-android Default Exception Handler






From Project

Back to project page demo-app-android.

License

The source code is released under:

MIT License

If you think the Android project demo-app-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package io.rong.imkit.demo;
//from w w w  .ja  v a 2s  .c om
import android.app.ActivityManager;
import android.content.Context;
import android.os.SystemClock;
import android.util.Log;

import com.umeng.analytics.MobclickAgent;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.Thread.UncaughtExceptionHandler;

public class DefaultExceptionHandler implements UncaughtExceptionHandler {
    Context mContext;

    public DefaultExceptionHandler(Context act) {
        mContext = act;
    }

    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
        MobclickAgent.reportError(mContext, ex);
        sendCrashReport(ex);

        Log.e("Rong Crash",ex.getMessage(), ex);

        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {

        }

        handleException();

    }

    private void sendCrashReport(Throwable ex) {

        File file = mContext.getExternalFilesDir("Crash_Rong");

        if (!file.exists()) {
            file.mkdirs();
        }

        File outFile = new File(file, getCurProcessName(mContext) + SystemClock.elapsedRealtime());

        try {
            outFile.createNewFile();
            saveCrashInfo2File(outFile, ex);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void handleException() {
        android.os.Process.killProcess(android.os.Process.myPid());

    }


    String getCurProcessName(Context context) {
        int pid = android.os.Process.myPid();
        ActivityManager mActivityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo appProcess : mActivityManager
                .getRunningAppProcesses()) {
            if (appProcess.pid == pid) {

                return appProcess.processName;
            }
        }
        return null;
    }


    private void saveCrashInfo2File(File file, Throwable ex) throws IOException {

        StringBuffer sb = new StringBuffer();


        Writer writer = new StringWriter();
        PrintWriter printWriter = new PrintWriter(writer);
        ex.printStackTrace(printWriter);
        Throwable cause = ex.getCause();
        while (cause != null) {
            cause.printStackTrace(printWriter);
            cause = cause.getCause();
        }
        printWriter.close();
        String result = writer.toString();
        sb.append(result);

        FileOutputStream fos = new FileOutputStream(file);
        fos.write(sb.toString().getBytes());
        fos.close();
    }

}




Java Source Code List

io.rong.imkit.demo.BaseActivity.java
io.rong.imkit.demo.BaseApiActivity.java
io.rong.imkit.demo.BlackListActivity.java
io.rong.imkit.demo.BlackListAdapter.java
io.rong.imkit.demo.DefaultExceptionHandler.java
io.rong.imkit.demo.DemoActivity.java
io.rong.imkit.demo.DemoApplication.java
io.rong.imkit.demo.DemoContext.java
io.rong.imkit.demo.FunctionListAdapter.java
io.rong.imkit.demo.GroupListActivity.java
io.rong.imkit.demo.GroupListAdapter.java
io.rong.imkit.demo.LocationActivity.java
io.rong.imkit.demo.LoginActivity.java
io.rong.imkit.demo.MainActivity.java
io.rong.imkit.demo.ParcelUtils.java
io.rong.imkit.demo.RegisterActivity.java
io.rong.imkit.demo.TestFragment2Activity.java
io.rong.imkit.demo.TestFragmentActivity.java
io.rong.imkit.demo.UserInfoActivity.java
io.rong.imkit.demo.common.DemoApi.java
io.rong.imkit.demo.model.CustomerService.java
io.rong.imkit.demo.model.Status.java
io.rong.imkit.demo.model.User.java
io.rong.imkit.demo.parser.GsonArrayParser.java
io.rong.imkit.demo.parser.GsonParser.java
io.rong.imkit.demo.parser.JsonObjectParser.java
io.rong.imkit.demo.parser.RegisterParser.java
io.rong.imkit.demo.ui.BaseFrameLayout.java
io.rong.imkit.demo.ui.LoadingDialog.java
io.rong.imkit.demo.ui.WinToast.java