├── .gitignore ├── .idea ├── .name ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── gradle.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── AvoidForceCloseDemo.iml ├── app ├── .gitignore ├── app.iml ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── droidyue │ │ └── avoidforceclosedemo │ │ └── ApplicationTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── droidyue │ │ │ └── avoidforceclosedemo │ │ │ ├── DroidApplication.java │ │ │ ├── DroidService.java │ │ │ ├── DroidUncaughtExceptionHandler.java │ │ │ ├── MainActivity.java │ │ │ └── SimpleUncaughtExceptionHandler.java │ └── res │ │ ├── layout │ │ ├── activity_main.xml │ │ └── content_main.xml │ │ ├── menu │ │ └── menu_main.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-v21 │ │ └── styles.xml │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── droidyue │ └── avoidforceclosedemo │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | 15 | # Gradle files 16 | .gradle/ 17 | build/ 18 | 19 | # Local configuration file (sdk path, etc) 20 | local.properties 21 | 22 | # Proguard folder generated by Eclipse 23 | proguard/ 24 | 25 | # Log Files 26 | *.log 27 | 28 | # Android Studio Navigation editor temp files 29 | .navigation/ 30 | 31 | # Android Studio captures folder 32 | captures/ 33 | 34 | ##Android Studio Ignore files 35 | /.idea/workspace.xml 36 | /.idea/libraries 37 | 38 | ##Mac Ignore files 39 | .DS_Store 40 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | AvoidForceCloseDemo -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | 47 | 48 | 49 | 50 | 1.8 51 | 52 | 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /AvoidForceCloseDemo.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/app.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.2" 6 | 7 | defaultConfig { 8 | applicationId "com.droidyue.avoidforceclosedemo" 9 | minSdkVersion 8 10 | targetSdkVersion 23 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | testCompile 'junit:junit:4.12' 25 | compile 'com.android.support:appcompat-v7:23.1.0' 26 | compile 'com.android.support:design:23.1.0' 27 | } 28 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/androidyue/common_tools/adt-bundle-mac-x86_64-20140702/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/droidyue/avoidforceclosedemo/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.droidyue.avoidforceclosedemo; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /app/src/main/java/com/droidyue/avoidforceclosedemo/DroidApplication.java: -------------------------------------------------------------------------------- 1 | package com.droidyue.avoidforceclosedemo; 2 | 3 | import android.app.Application; 4 | import android.util.Log; 5 | 6 | /** 7 | * Created by androidyue on 11/23/15. 8 | */ 9 | public class DroidApplication extends Application { 10 | private static final String LOGTAG = "DroidApplication"; 11 | 12 | @Override 13 | public void onCreate() { 14 | super.onCreate(); 15 | Log.i(LOGTAG, "onCreate"); 16 | Thread.setDefaultUncaughtExceptionHandler(new DroidUncaughtExceptionHandler(this)); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /app/src/main/java/com/droidyue/avoidforceclosedemo/DroidService.java: -------------------------------------------------------------------------------- 1 | package com.droidyue.avoidforceclosedemo; 2 | 3 | import android.app.Service; 4 | import android.content.Intent; 5 | import android.os.IBinder; 6 | import android.support.annotation.Nullable; 7 | import android.util.Log; 8 | 9 | /** 10 | * Created by androidyue on 11/23/15. 11 | */ 12 | public class DroidService extends Service { 13 | 14 | private static final String LOGTAG = "DroidService"; 15 | 16 | @Nullable 17 | @Override 18 | public IBinder onBind(Intent intent) { 19 | return null; 20 | } 21 | 22 | @Override 23 | public void onCreate() { 24 | Log.i(LOGTAG, "onCreate "); 25 | super.onCreate(); 26 | 27 | } 28 | 29 | @Override 30 | public int onStartCommand(Intent intent, int flags, int startId) { 31 | int result = super.onStartCommand(intent, flags, startId); 32 | new Thread() { 33 | @Override 34 | public void run() { 35 | super.run(); 36 | try { 37 | Thread.sleep(5000); 38 | String s = null; 39 | s.length(); 40 | } catch (InterruptedException e) { 41 | e.printStackTrace(); 42 | } 43 | } 44 | }.start(); 45 | return result; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /app/src/main/java/com/droidyue/avoidforceclosedemo/DroidUncaughtExceptionHandler.java: -------------------------------------------------------------------------------- 1 | package com.droidyue.avoidforceclosedemo; 2 | 3 | import android.app.ActivityManager; 4 | import android.content.Context; 5 | import android.util.Log; 6 | 7 | /** 8 | * Created by androidyue on 11/23/15. 9 | */ 10 | public class DroidUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler{ 11 | private static final String LOGTAG = "DroidUncaughtExceptionHandler"; 12 | private Thread.UncaughtExceptionHandler mDefaultExceptionHandler; 13 | private Context mAppContext; 14 | 15 | public DroidUncaughtExceptionHandler(Context context) { 16 | mAppContext = context.getApplicationContext(); 17 | mDefaultExceptionHandler = Thread.getDefaultUncaughtExceptionHandler(); 18 | } 19 | 20 | public static String getProcessName(Context appContext) { 21 | String currentProcessName = ""; 22 | int pid = android.os.Process.myPid(); 23 | ActivityManager manager = (ActivityManager) appContext.getSystemService(Context.ACTIVITY_SERVICE); 24 | for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) { 25 | if (processInfo.pid == pid) { 26 | currentProcessName = processInfo.processName; 27 | break; 28 | } 29 | } 30 | return currentProcessName; 31 | } 32 | 33 | @Override 34 | public void uncaughtException(Thread thread, Throwable ex) { 35 | String processName = getProcessName(mAppContext); 36 | if (mAppContext.getPackageName().equals(processName)) { 37 | Log.i(LOGTAG, "uncaughtException main process"); 38 | mDefaultExceptionHandler.uncaughtException(thread, ex); 39 | } else { 40 | Log.i(LOGTAG, "uncaughtException process name=" + processName); 41 | android.os.Process.killProcess(android.os.Process.myPid()); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /app/src/main/java/com/droidyue/avoidforceclosedemo/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.droidyue.avoidforceclosedemo; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.view.View; 7 | 8 | public class MainActivity extends AppCompatActivity implements View.OnClickListener { 9 | 10 | @Override 11 | protected void onCreate(Bundle savedInstanceState) { 12 | super.onCreate(savedInstanceState); 13 | setContentView(R.layout.activity_main); 14 | findViewById(R.id.causeNPE).setOnClickListener(this); 15 | findViewById(R.id.startSickService).setOnClickListener(this); 16 | } 17 | 18 | 19 | 20 | private void causeNPE() { 21 | String s = null; 22 | s.length(); 23 | } 24 | 25 | @Override 26 | public void onClick(View v) { 27 | int viewId = v.getId(); 28 | if (viewId == R.id.causeNPE) { 29 | causeNPE(); 30 | } else if (viewId == R.id.startSickService) { 31 | startService(new Intent(MainActivity.this, DroidService.class)); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /app/src/main/java/com/droidyue/avoidforceclosedemo/SimpleUncaughtExceptionHandler.java: -------------------------------------------------------------------------------- 1 | package com.droidyue.avoidforceclosedemo; 2 | 3 | import android.util.Log; 4 | 5 | import java.io.PrintWriter; 6 | import java.io.StringWriter; 7 | import java.io.Writer; 8 | 9 | /** 10 | * Created by androidyue on 11/29/15. 11 | */ 12 | public class SimpleUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler { 13 | private static final String LOGTAG = "SimpleUncaughtExceptionHandler"; 14 | 15 | @Override 16 | public void uncaughtException(Thread thread, Throwable ex) { 17 | final Writer result = new StringWriter(); 18 | final PrintWriter printWriter = new PrintWriter(result); 19 | ex.printStackTrace(printWriter); 20 | String errorReport = result.toString(); 21 | Log.i(LOGTAG, "uncaughtException errorReport=" + errorReport); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 15 | 16 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /app/src/main/res/layout/content_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 15 | 16 |