├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ ├── colors.xml │ │ │ │ ├── dimens.xml │ │ │ │ └── styles.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 │ │ │ └── layout │ │ │ │ └── activity_main.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── vmloft │ │ │ │ └── develop │ │ │ │ └── daemon │ │ │ │ ├── VMApplication.java │ │ │ │ ├── services │ │ │ │ ├── VMBackgroundService.java │ │ │ │ ├── VMDaemonJobService.java │ │ │ │ ├── VMForegroundService.java │ │ │ │ ├── VMCoreService.java │ │ │ │ └── VMDaemonService.java │ │ │ │ ├── VMDaemonActivity.java │ │ │ │ ├── receiver │ │ │ │ └── VMWakeReceiver.java │ │ │ │ ├── VMDaemonManager.java │ │ │ │ └── MainActivity.java │ │ └── AndroidManifest.xml │ └── androidTest │ │ └── java │ │ └── com │ │ └── vmloft │ │ └── develop │ │ └── daemon │ │ └── ExampleInstrumentedTest.java ├── build.gradle └── proguard-rules.pro ├── settings.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── README.md ├── gradle.properties ├── gradlew.bat └── gradlew /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | VMDaemonServices 3 | 4 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lzan13/VMDaemonServices/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lzan13/VMDaemonServices/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lzan13/VMDaemonServices/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lzan13/VMDaemonServices/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lzan13/VMDaemonServices/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lzan13/VMDaemonServices/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .idea 7 | .DS_Store 8 | *libs 9 | /build 10 | /captures 11 | .externalNativeBuild 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | VMDaemonServices 2 | ---------------- 3 | 4 | 测试 android 实现守护进程的几种方式 5 | 6 | 详细介绍可以查看我的博客:【[Android 守护进程的实现方式](http://melove.net/blog/2017/03/android-daemon-service-1488942411000.html)】 -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Dec 28 10:00:20 PST 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip 7 | -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/java/com/vmloft/develop/daemon/VMApplication.java: -------------------------------------------------------------------------------- 1 | package com.vmloft.develop.daemon; 2 | 3 | import android.app.Application; 4 | import android.content.Context; 5 | 6 | /** 7 | * Created by lzan13 on 2017/3/9. 8 | */ 9 | 10 | public class VMApplication extends Application { 11 | 12 | private static Context context; 13 | 14 | @Override public void onCreate() { 15 | super.onCreate(); 16 | context = this; 17 | } 18 | 19 | public static Context getContext() { 20 | return context; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "25.0.2" 6 | defaultConfig { 7 | applicationId "com.vmloft.develop.daemon" 8 | minSdkVersion 15 9 | targetSdkVersion 23 10 | versionCode 1 11 | versionName "1.0" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | compile fileTree(dir: 'libs', include: ['*.jar']) 23 | compile 'com.android.support:appcompat-v7:25.1.1' 24 | } 25 | -------------------------------------------------------------------------------- /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/lzan13/develop/android/android-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 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | org.gradle.jvmargs=-Xmx1536m 13 | 14 | # When configured, Gradle will run in incubating parallel mode. 15 | # This option should only be used with decoupled projects. More details, visit 16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 17 | # org.gradle.parallel=true 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/vmloft/develop/daemon/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.vmloft.develop.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) public class ExampleInstrumentedTest { 18 | @Test public void useAppContext() throws Exception { 19 | // Context of the app under test. 20 | Context appContext = InstrumentationRegistry.getTargetContext(); 21 | 22 | assertEquals("com.vmloft.develop.daemonservices", appContext.getPackageName()); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /app/src/main/java/com/vmloft/develop/daemon/services/VMBackgroundService.java: -------------------------------------------------------------------------------- 1 | package com.vmloft.develop.daemon.services; 2 | 3 | import android.app.Service; 4 | import android.content.Intent; 5 | import android.os.IBinder; 6 | import android.util.Log; 7 | 8 | /** 9 | * 普通的后台 Service 服务,此类只是为了展示正常服务进程的优先级 10 | * 11 | * Created by lzan13 on 2017/3/7.JobScheduler机制唤醒 12 | */ 13 | public class VMBackgroundService extends Service { 14 | 15 | private final static String TAG = VMBackgroundService.class.getSimpleName(); 16 | 17 | @Override public void onCreate() { 18 | Log.i(TAG, "onCreate"); 19 | super.onCreate(); 20 | } 21 | 22 | @Override public IBinder onBind(Intent intent) { 23 | // TODO: Return the communication channel to the service. 24 | throw new UnsupportedOperationException("onBind 未实现"); 25 | } 26 | 27 | @Override public void onDestroy() { 28 | Log.i(TAG, "onDestroy"); 29 | super.onDestroy(); 30 | } 31 | } -------------------------------------------------------------------------------- /app/src/main/java/com/vmloft/develop/daemon/VMDaemonActivity.java: -------------------------------------------------------------------------------- 1 | package com.vmloft.develop.daemon; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.util.Log; 7 | import android.view.Gravity; 8 | import android.view.Window; 9 | import android.view.WindowManager; 10 | 11 | /** 12 | * Created by lzan13 on 2017/3/9. 13 | */ 14 | 15 | public class VMDaemonActivity extends AppCompatActivity { 16 | 17 | @Override protected void onCreate(@Nullable Bundle savedInstanceState) { 18 | super.onCreate(savedInstanceState); 19 | Log.d("lzan13", "VMDaemonActivity -> onCreate"); 20 | 21 | Window window = getWindow(); 22 | window.setGravity(Gravity.LEFT | Gravity.TOP); 23 | WindowManager.LayoutParams lp = window.getAttributes(); 24 | lp.width = 1; 25 | lp.height = 1; 26 | 27 | window.setAttributes(lp); 28 | 29 | VMDaemonManager.getInstance().setDaemonActivity(this); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /app/src/main/java/com/vmloft/develop/daemon/services/VMDaemonJobService.java: -------------------------------------------------------------------------------- 1 | package com.vmloft.develop.daemon.services; 2 | 3 | import android.annotation.TargetApi; 4 | import android.app.job.JobParameters; 5 | import android.app.job.JobService; 6 | import android.os.Build; 7 | import android.util.Log; 8 | import com.vmloft.develop.daemon.VMDaemonManager; 9 | 10 | /** 11 | * 5.x 以上使用 JobService 实现守护进程,这个守护进程要做的工作很简单,就是启动应用的核心进程 12 | * Created by lzan13 on 2017/3/8. 13 | */ 14 | @TargetApi(Build.VERSION_CODES.LOLLIPOP) public class VMDaemonJobService extends JobService { 15 | 16 | private final static String TAG = VMDaemonJobService.class.getSimpleName(); 17 | 18 | @Override public boolean onStartJob(JobParameters params) { 19 | Log.d(TAG, "onStartJob"); 20 | // 这里为了演示,这里直接启动核心进程,没有做其他判断操作 21 | VMDaemonManager.getInstance().startCoreProcess(); 22 | return false; 23 | } 24 | 25 | @Override public boolean onStopJob(JobParameters params) { 26 | Log.d(TAG, "onStopJob"); 27 | return false; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /app/src/main/java/com/vmloft/develop/daemon/receiver/VMWakeReceiver.java: -------------------------------------------------------------------------------- 1 | package com.vmloft.develop.daemon.receiver; 2 | 3 | import android.content.BroadcastReceiver; 4 | import android.content.Context; 5 | import android.content.Intent; 6 | import android.util.Log; 7 | import com.vmloft.develop.daemon.VMDaemonManager; 8 | 9 | /** 10 | * 接收唤醒应用核心服务的广播接收器,接收守护进程发送过来的广播 11 | * 12 | * Created by lzan13 on 2017/3/7. 13 | */ 14 | public class VMWakeReceiver extends BroadcastReceiver { 15 | 16 | private final static String TAG = VMWakeReceiver.class.getSimpleName(); 17 | 18 | // 定义守护进程发送唤醒广播的 Action 19 | public final static String DAEMON_WAKE_ACTION = "com.vmloft.develop.daemon.wake"; 20 | 21 | @Override public void onReceive(Context context, Intent intent) { 22 | String action = intent.getAction(); 23 | if (action.equals(DAEMON_WAKE_ACTION)) { 24 | Log.i(TAG, "onReceive: 起床了!"); 25 | // 接收到守护进程发送的广播,开始启动核心进程 26 | VMDaemonManager.getInstance().startCoreProcess(); 27 | } else if (action.equals(Intent.ACTION_SCREEN_OFF)) { 28 | Log.i(TAG, "onReceive: 锁屏了!"); 29 | // 检测到锁屏,启动一像素的流氓 Activity 30 | VMDaemonManager.getInstance().startDaemonActivity(); 31 | } else if (action.equals(Intent.ACTION_USER_PRESENT)) { 32 | Log.i(TAG, "onReceive: 解锁了!"); 33 | // 解锁后,结束一像素的流氓 Activity 34 | VMDaemonManager.getInstance().finishDaemonActivity(); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 14 | 19 |