├── .gitignore ├── .idea ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── gradle.xml ├── misc.xml ├── modules.xml └── runConfigurations.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── example │ │ └── maat │ │ └── hello_daemon │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── maat │ │ │ └── hello_daemon │ │ │ ├── DaemonBroadcastRecieve.java │ │ │ ├── DaemonService.java │ │ │ ├── MainActivity.java │ │ │ ├── RemoteBroadcastRecieve.java │ │ │ └── RemoteService.java │ └── res │ │ ├── layout │ │ └── activity_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-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── example │ └── maat │ └── hello_daemon │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── screenshots └── daemon.gif └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 12 | 13 | 14 | 26 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | Android Lint 46 | 47 | 48 | General 49 | 50 | 51 | Maven 52 | 53 | 54 | 55 | 56 | AndroidLintUseCompoundDrawables 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 78 | 79 | $USER_HOME$/.subversion 80 | 81 | 82 | 83 | 84 | 85 | 1.8 86 | 87 | 92 | 93 | 94 | 95 | 96 | 97 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #进程保活 2 | 在原生系统中,进程保活很容易可以做到,但是在小米这样的深度定制系统下,很难做到,这个项目运行在小米真机上,只是短时间系统杀不死。 3 | /* 最终发现在深度定制机上,很难做到进程不死 */ 4 | 5 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 24 5 | buildToolsVersion "24.0.3" 6 | defaultConfig { 7 | applicationId "com.example.maat.hello_daemon" 8 | minSdkVersion 16 9 | targetSdkVersion 24 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 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 | 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:24.2.1' 28 | testCompile 'junit:junit:4.12' 29 | } 30 | -------------------------------------------------------------------------------- /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/xinghongfei/Downloads/android-sdk--mac-r24-updated/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/example/maat/hello_daemon/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.example.maat.hello_daemon; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.example.maat.hello_daemon", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 22 | 23 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/maat/hello_daemon/DaemonBroadcastRecieve.java: -------------------------------------------------------------------------------- 1 | package com.example.maat.hello_daemon; 2 | 3 | import android.content.BroadcastReceiver; 4 | import android.content.Context; 5 | import android.content.Intent; 6 | import android.util.Log; 7 | 8 | /** 9 | * Created by xinghongfei on 16/10/8. 10 | */ 11 | 12 | public class DaemonBroadcastRecieve extends BroadcastReceiver { 13 | private boolean isFirst = true; 14 | 15 | @Override 16 | public void onReceive(final Context context, Intent intent) { 17 | while (isFirst) { 18 | isFirst = false; 19 | while (true){ 20 | try { 21 | Thread.sleep(2000); 22 | Log.i("hehe", "DaemonBroadRecieve is onReceive"); 23 | context.startService(new Intent(context, RemoteService.class)); 24 | 25 | } catch (InterruptedException e) { 26 | e.printStackTrace(); 27 | } 28 | } 29 | } 30 | 31 | 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/maat/hello_daemon/DaemonService.java: -------------------------------------------------------------------------------- 1 | package com.example.maat.hello_daemon; 2 | 3 | import android.app.Notification; 4 | import android.app.NotificationManager; 5 | import android.app.Service; 6 | import android.content.Intent; 7 | import android.os.IBinder; 8 | import android.os.SystemClock; 9 | import android.support.annotation.Nullable; 10 | import android.util.Log; 11 | 12 | /** 13 | * Created by xinghongfei on 16/10/8. 14 | */ 15 | 16 | public class DaemonService extends Service { 17 | private boolean isFirst = true; 18 | 19 | @Nullable 20 | @Override 21 | public IBinder onBind(Intent intent) { 22 | return null; 23 | } 24 | 25 | @Override 26 | public int onStartCommand(Intent intent, int flags, int startId) { 27 | Log.i("hehe", "DaemoService is onStartCommend"); 28 | // if (isFirst) { 29 | Notification.Builder builder = new Notification.Builder(this); 30 | builder.setSmallIcon(R.mipmap.ic_launcher); 31 | startForeground(111, builder.build()); 32 | new Thread(new Runnable() { 33 | @Override 34 | public void run() { 35 | SystemClock.sleep(1000); 36 | stopForeground(true); 37 | NotificationManager manager = 38 | (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 39 | manager.cancel(111); 40 | 41 | } 42 | }).start(); 43 | // isFirst = false; 44 | // } 45 | // sendBroadcast(new Intent(this,RemoteBroadcastRecieve.class)); 46 | // startService(new Intent(this,RemoteService.class)); 47 | 48 | return START_STICKY; 49 | } 50 | 51 | @Override 52 | public void onDestroy() { 53 | Log.i("hehe", "DaemonService is Destroy"); 54 | super.onDestroy(); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/maat/hello_daemon/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.maat.hello_daemon; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.view.View; 7 | import android.widget.Button; 8 | 9 | public class MainActivity extends AppCompatActivity { 10 | 11 | Button btnstart; 12 | 13 | @Override 14 | protected void onCreate(Bundle savedInstanceState) { 15 | super.onCreate(savedInstanceState); 16 | setContentView(R.layout.activity_main); 17 | btnstart = (Button) findViewById(R.id.button); 18 | btnstart.setOnClickListener(new View.OnClickListener() { 19 | @Override 20 | public void onClick(View v) { 21 | 22 | // sendBroadcast(new Intent(MainActivity.this,DaemonBroadcastRecieve.class)); 23 | startService(new Intent(MainActivity.this, RemoteService.class)); 24 | 25 | } 26 | }); 27 | 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/maat/hello_daemon/RemoteBroadcastRecieve.java: -------------------------------------------------------------------------------- 1 | package com.example.maat.hello_daemon; 2 | 3 | import android.content.BroadcastReceiver; 4 | import android.content.Context; 5 | import android.content.Intent; 6 | 7 | /** 8 | * Created by xinghongfei on 16/10/8. 9 | */ 10 | 11 | public class RemoteBroadcastRecieve extends BroadcastReceiver { 12 | 13 | boolean isFirst = true; 14 | 15 | @Override 16 | public void onReceive(final Context context, Intent intent) { 17 | 18 | 19 | if (isFirst) { 20 | isFirst=false; 21 | while (true){ 22 | try { 23 | Thread.sleep(2000); 24 | context.startService(new Intent(context, DaemonService.class)); 25 | } catch (InterruptedException e) { 26 | e.printStackTrace(); 27 | } 28 | } 29 | 30 | } 31 | 32 | 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/maat/hello_daemon/RemoteService.java: -------------------------------------------------------------------------------- 1 | package com.example.maat.hello_daemon; 2 | 3 | import android.app.Notification; 4 | import android.app.Service; 5 | import android.content.Intent; 6 | import android.os.Build; 7 | import android.os.IBinder; 8 | import android.support.annotation.Nullable; 9 | import android.util.Log; 10 | 11 | /** 12 | * Created by xinghongfei on 16/10/8. 13 | */ 14 | 15 | public class RemoteService extends Service { 16 | 17 | private boolean isFirst = true; 18 | 19 | @Nullable 20 | @Override 21 | public IBinder onBind(Intent intent) { 22 | return null; 23 | } 24 | 25 | @Override 26 | public int onStartCommand(Intent intent, int flags, int startId) { 27 | Log.i("hehe", "RemoteService is on receive"); 28 | // 29 | // if (isFirst) { 30 | // isFirst=false; 31 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { 32 | Notification.Builder builder = new Notification.Builder(this); 33 | builder.setSmallIcon(R.mipmap.ic_launcher); 34 | startForeground(111, builder.build()); 35 | } else { 36 | startForeground(111, new Notification()); 37 | } 38 | // RemoteService.this.sendBroadcast(new Intent(RemoteService.this,DaemonBroadcastRecieve.class)); 39 | startService(new Intent(this, DaemonService.class)); 40 | //// sendBroadcast(new Intent(this,RemoteBroadcastRecieve.class)); 41 | // } 42 | return START_STICKY; 43 | } 44 | 45 | @Override 46 | public void onDestroy() { 47 | 48 | Log.i("hehe", "RemoteService is Destroy"); 49 | super.onDestroy(); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 19 | 20 |