• 一手遮天 Android


    项目地址 https://github.com/webabcd/AndroidDemo
    作者 webabcd

    一手遮天 Android - 优化: 捕获未处理异常

    示例如下:

    /optimize/Demo4.java

    /**
     * 本例用于演示如何捕获未处理异常
     *
     * 自定义的未处理异常处理器请参见 MyUncaughtExceptionHandler.java
     */
    
    package com.webabcd.androiddemo.optimize;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    
    import com.webabcd.androiddemo.R;
    
    public class Demo4 extends AppCompatActivity {
    
        private Button _button1;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_optimize_demo4);
    
            _button1 = findViewById(R.id.button1);
    
            sample();
        }
    
        private void sample() {
            // 触发一个异常
            _button1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int i = Integer.parseInt("abc");
                }
            });
        }
    }
    

    /optimize/MyUncaughtExceptionHandler.java

    /**
     * 用于捕获未处理异常的类(此类在 MyApplication.java 中初始化)
     */
    
    package com.webabcd.androiddemo.optimize;
    
    import java.lang.Thread.UncaughtExceptionHandler;
    import java.lang.reflect.Field;
    import java.util.Locale;
    
    import android.content.Context;
    import android.content.pm.PackageInfo;
    import android.content.pm.PackageManager;
    import android.os.Build;
    import android.util.Log;
    
    // 通过实现 UncaughtExceptionHandler 接口来实现自定义的未处理异常处理器
    public class MyUncaughtExceptionHandler implements UncaughtExceptionHandler {
    
        private static MyUncaughtExceptionHandler instance = null;
        public synchronized static MyUncaughtExceptionHandler getInstance() {
            if (instance == null) {
                instance = new MyUncaughtExceptionHandler();
            }
            return instance;
        }
    
        private Context mContext;
        private Thread.UncaughtExceptionHandler mDefaultHandler;
    
        private MyUncaughtExceptionHandler() {
    
        }
    
        public void init(Context context) {
            mContext = context;
    
            // 系统默认的 UncaughtException 处理器
            mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
    
            // 指定 UncaughtException 处理器为我们的自定义处理器
            Thread.setDefaultUncaughtExceptionHandler(this);
        }
    
        // 捕获未处理异常
        @Override
        public void uncaughtException(Thread thread, Throwable ex) {
    
            // 打印未处理异常的相关信息
            Log.e("UncaughtException", getDeviceInfo(mContext) + getTraceInfo(ex));
    
            // 使用系统默认的 UncaughtException 处理器处理这个未处理异常(调用这个就可以崩溃了)
            mDefaultHandler.uncaughtException(thread, ex);
        }
    
        // 获取设备信息
        public static String getDeviceInfo(Context context) {
            StringBuilder sb = new StringBuilder();
            try {
                PackageManager pm = context.getPackageManager();
                PackageInfo pi = pm.getPackageInfo(context.getPackageName(), PackageManager.GET_ACTIVITIES);
                if (pi != null) {
                    sb.append(String.format(Locale.ENGLISH, "versionName:%s, versionCode:%d
    ", pi.versionName, pi.versionCode));
                }
            } catch (Exception e) {
                sb.append("getPackageInfo error:" + e.toString());
            }
    
            Field[] fields = Build.class.getDeclaredFields();
            for (Field field : fields) {
                try {
                    field.setAccessible(true);
                    sb.append(String.format(Locale.ENGLISH, "%s:%s
    ", field.getName(), field.get(null).toString()));
                } catch (Exception e) {
                    sb.append("getDeclaredFields error:" + e.toString());
                }
            }
    
            return sb.toString();
        }
    
        // 获取异常信息
        public static String getTraceInfo(Throwable e) {
            StringBuilder sb = new StringBuilder();
            Throwable ex = e.getCause() == null ? e : e.getCause();
            StackTraceElement[] stacks = ex.getStackTrace();
            for (int i = 0; i < stacks.length; i++) {
                sb.append(String.format(Locale.ENGLISH, "class:%s, method:%s, line:%s, exception:%s
    ",
                        stacks[i].getClassName(), stacks[i].getMethodName(), stacks[i].getLineNumber(), ex.toString()));
            }
    
            return sb.toString();
        }
    }
    

    MyApplication.java

        @Override
        public void onCreate() {
            super.onCreate();
    
            // 初始化自定义的未处理异常处理器
            MyUncaughtExceptionHandler myUncaughtExceptionHandler = MyUncaughtExceptionHandler.getInstance();
            myUncaughtExceptionHandler.init(getApplicationContext());
        }
    

    /layout/activity_optimize_demo4.xml

    <?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:orientation="vertical">
    
        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="触发异常" />
    
    </LinearLayout>
    

    项目地址 https://github.com/webabcd/AndroidDemo
    作者 webabcd

  • 相关阅读:
    动态tab页
    SQL Server附加数据库拒绝访问
    SQL window身份登陆 SQL server不能登陆
    SQL UPDATE 经典
    Char Varchar Nvarchar区别
    【OpenSSL】创建证书
    makecert 制作数字证书
    cer, pfx 创建,并且读取公钥/密钥,加解密 (C#程序实现)
    CompareValidator ASP控件
    XMLHelper 类
  • 原文地址:https://www.cnblogs.com/webabcd/p/android_optimize_Demo4.html
Copyright © 2020-2023  润新知