Skip to content

Commit 85309e9

Browse files
author
wanjian
committed
demo
1 parent 0ea7cad commit 85309e9

37 files changed

+609
-102
lines changed

.idea/gradle.xml

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ android {
99
targetSdkVersion 25
1010
versionCode 1
1111
versionName "1.0"
12-
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1312
}
1413
buildTypes {
1514
release {
@@ -21,11 +20,6 @@ android {
2120

2221
dependencies {
2322
compile fileTree(dir: 'libs', include: ['*.jar'])
24-
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
25-
exclude group: 'com.android.support', module: 'support-annotations'
26-
})
27-
compile 'com.android.support:appcompat-v7:25.1.0'
28-
testCompile 'junit:junit:4.12'
2923
}
3024

3125

app/src/androidTest/java/com/wanjian/cockroach/ExampleInstrumentedTest.java

-26
This file was deleted.

app/src/main/AndroidManifest.xml

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,2 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
package="com.wanjian.cockroach">
4-
5-
6-
7-
</manifest>
2+
<manifest package="com.wanjian.cockroach"></manifest>

app/src/main/java/com/wanjian/cockroach/App.java

-46
This file was deleted.

app/src/test/java/com/wanjian/cockroach/ExampleUnitTest.java

-17
This file was deleted.

build.gradle

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
buildscript {
44
repositories {
55
jcenter()
6+
maven {
7+
url 'https://maven.google.com/'
8+
name 'Google'
9+
}
610
}
711
dependencies {
812
classpath 'com.android.tools.build:gradle:2.2.2'
@@ -15,6 +19,10 @@ buildscript {
1519
allprojects {
1620
repositories {
1721
jcenter()
22+
maven {
23+
url 'https://maven.google.com/'
24+
name 'Google'
25+
}
1826
}
1927
}
2028

demo/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

demo/build.gradle

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 26
5+
buildToolsVersion "26.0.2"
6+
7+
8+
defaultConfig {
9+
applicationId "com.wanjian.demo"
10+
minSdkVersion 14
11+
targetSdkVersion 26
12+
versionCode 1
13+
versionName "1.0"
14+
15+
16+
}
17+
18+
buildTypes {
19+
release {
20+
minifyEnabled false
21+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
22+
}
23+
}
24+
25+
}
26+
27+
dependencies {
28+
compile fileTree(include: ['*.jar'], dir: 'libs')
29+
compile 'com.android.support:appcompat-v7:26.1.0'
30+
compile 'com.android.support.constraint:constraint-layout:1.0.2'
31+
compile 'com.android.support:recyclerview-v7:26+'
32+
compile project(':app')
33+
}

demo/proguard-rules.pro

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile

demo/src/main/AndroidManifest.xml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.wanjian.demo">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:roundIcon="@mipmap/ic_launcher_round"
10+
android:supportsRtl="true"
11+
android:theme="@style/AppTheme">
12+
<activity android:name=".MainAct">
13+
<intent-filter>
14+
<action android:name="android.intent.action.MAIN" />
15+
16+
<category android:name="android.intent.category.LAUNCHER" />
17+
</intent-filter>
18+
</activity>
19+
<activity android:name=".SecondAct" />
20+
</application>
21+
22+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package com.wanjian.demo;
2+
3+
import android.app.Activity;
4+
import android.content.Intent;
5+
import android.os.Bundle;
6+
import android.os.Handler;
7+
import android.os.Looper;
8+
import android.support.annotation.Nullable;
9+
import android.util.Log;
10+
import android.view.View;
11+
import android.widget.Toast;
12+
13+
import com.wanjian.cockroach.Cockroach;
14+
15+
16+
/**
17+
* Created by wanjian on 2018/1/22.
18+
*/
19+
20+
public class MainAct extends Activity {
21+
22+
@Override
23+
protected void onCreate(@Nullable Bundle savedInstanceState) {
24+
super.onCreate(savedInstanceState);
25+
setContentView(R.layout.main);
26+
findViewById(R.id.install).setOnClickListener(new View.OnClickListener() {
27+
@Override
28+
public void onClick(View v) {
29+
install();
30+
}
31+
});
32+
findViewById(R.id.uninstall).setOnClickListener(new View.OnClickListener() {
33+
@Override
34+
public void onClick(View v) {
35+
Cockroach.uninstall();
36+
}
37+
});
38+
findViewById(R.id.click).setOnClickListener(new View.OnClickListener() {
39+
@Override
40+
public void onClick(View v) {
41+
throw new RuntimeException("点击异常");
42+
}
43+
});
44+
findViewById(R.id.thread).setOnClickListener(new View.OnClickListener() {
45+
@Override
46+
public void onClick(View v) {
47+
new Thread() {
48+
@Override
49+
public void run() {
50+
super.run();
51+
throw new RuntimeException("子线程异常");
52+
}
53+
}.start();
54+
}
55+
});
56+
findViewById(R.id.handler).setOnClickListener(new View.OnClickListener() {
57+
@Override
58+
public void onClick(View v) {
59+
60+
new Handler().post(new Runnable() {
61+
@Override
62+
public void run() {
63+
throw new RuntimeException("handler异常");
64+
}
65+
});
66+
}
67+
});
68+
69+
findViewById(R.id.act).setOnClickListener(new View.OnClickListener() {
70+
@Override
71+
public void onClick(View v) {
72+
startActivity(new Intent(MainAct.this, SecondAct.class));
73+
}
74+
});
75+
76+
findViewById(R.id.noact).setOnClickListener(new View.OnClickListener() {
77+
@Override
78+
public void onClick(View v) {
79+
startActivity(new Intent(MainAct.this, UnknowAct.class));
80+
}
81+
});
82+
83+
84+
}
85+
86+
private void install() {
87+
Cockroach.install(new Cockroach.ExceptionHandler() {
88+
89+
// handlerException内部建议手动try{ 你的异常处理逻辑 }catch(Throwable e){ } ,以防handlerException内部再次抛出异常,导致循环调用handlerException
90+
91+
@Override
92+
public void handlerException(final Thread thread, final Throwable throwable) {
93+
//开发时使用Cockroach可能不容易发现bug,所以建议开发阶段在handlerException中用Toast谈个提示框,
94+
//由于handlerException可能运行在非ui线程中,Toast又需要在主线程,所以new了一个new Handler(Looper.getMainLooper()),
95+
//所以千万不要在下面的run方法中执行耗时操作,因为run已经运行在了ui线程中。
96+
//new Handler(Looper.getMainLooper())只是为了能弹出个toast,并无其他用途
97+
new Handler(Looper.getMainLooper()).post(new Runnable() {
98+
@Override
99+
public void run() {
100+
try {
101+
//建议使用下面方式在控制台打印异常,这样就可以在Error级别看到红色log
102+
Log.e("AndroidRuntime", "--->CockroachException:" + thread + "<---", throwable);
103+
Toast.makeText(MainAct.this, "Exception Happend\n" + thread + "\n" + throwable.toString(), Toast.LENGTH_SHORT).show();
104+
// throw new RuntimeException("..."+(i++));
105+
} catch (Throwable e) {
106+
107+
}
108+
}
109+
});
110+
}
111+
});
112+
113+
}
114+
}

0 commit comments

Comments
 (0)