异常

来源:互联网 发布:金融类电影 知乎 编辑:程序博客网 时间:2024/06/04 18:35

/%%%%%%%%%%%%%%%%%%%%%%/

apply plugin: 'com.android.application'


android {
    compileSdkVersion 26
    buildToolsVersion "26.0.0"
    defaultConfig {
        applicationId "com.example.buglytest"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"


        ndk {
            // 设置支持的SO库架构
            abiFilters 'armeabi' , 'x86' //, 'armeabi-v7a', 'x86_64', 'arm64-v8a'
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}


dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'


    compile 'com.tencent.bugly:crashreport:latest.release'
    compile 'com.tencent.bugly:nativecrashreport:latest.release'
}





%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%/activity_main

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.buglytest.MainActivity">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</android.support.constraint.ConstraintLayout>

/%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%/配置

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.buglytest">
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.READ_LOGS" />
    <application
        android:name=".App"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>


</manifest>

/%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%/App

package com.example.buglytest;


import android.app.Application;
import android.os.Build;


import com.tencent.bugly.crashreport.CrashReport;


/**
 * Created by hasee on 2017/9/13.
 */


public class App extends Application {


    @Override
    public void onCreate() {
        super.onCreate();


//        MyCaughtException.getInstance().init(this);


        //腾讯bugly初始化, 本质上腾讯的bugly核心类就是一个自定义的 MyCaughtException
        CrashReport.initCrashReport(getApplicationContext(), "6bfb60be17", BuildConfig.DEBUG);
    }
}

/%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%/MainActivity

package com.example.buglytest;


import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;


public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Dialog dialog=null;
        dialog.show();
    }
}

/$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$/MyCaughtException

package com.example.buglytest;


import android.content.Context;


/**
 * Created by hasee on 2017/9/13.
 *
 * 自定义一个全局异常捕获机制
 */


public class MyCaughtException implements Thread.UncaughtExceptionHandler {




    static MyCaughtException myCaughtException = new MyCaughtException();
    Context context;


    private MyCaughtException() {
    }


    public static MyCaughtException getInstance() {
        return myCaughtException;
    }




    //// TODO: 2017/9/13  需要再applicatiion初始化这个方法
    public void init(Context context) {
        this.context = context;


        //将系统默认处理异常的类设置成我们的当前类
        Thread.setDefaultUncaughtExceptionHandler(this);
    }




    //捕获到异常之后,会回调我们这个方法,我们在这,可以 获取设备信息,获取错误信息,
    //可以将信息存到本地,也可以存到服务器
    @Override
    public void uncaughtException(Thread thread, Throwable throwable) {


        //// TODO: 2017/9/13 拿信息


        //// TODO: 2017/9/13 将信息存到本地,或者上传到服务器
    }
}