├── app
├── .gitignore
├── lock.jks
├── lock2.jks
├── debug
│ ├── app-debug.apk
│ └── output.json
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── raw
│ │ │ │ └── silent.mp3
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── colors.xml
│ │ │ │ └── styles.xml
│ │ │ ├── mipmap-hdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-anydpi-v26
│ │ │ │ ├── ic_launcher.xml
│ │ │ │ └── ic_launcher_round.xml
│ │ │ ├── layout
│ │ │ │ ├── activity_single_pixel.xml
│ │ │ │ ├── activity_splash.xml
│ │ │ │ └── activity_second.xml
│ │ │ ├── drawable-v24
│ │ │ │ └── ic_launcher_foreground.xml
│ │ │ └── drawable
│ │ │ │ └── ic_launcher_background.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── example
│ │ │ │ └── administrator
│ │ │ │ └── lockservice
│ │ │ │ ├── RestartServiceReceiver.java
│ │ │ │ ├── SplashActivity.java
│ │ │ │ ├── SinglePixelActivity.java
│ │ │ │ ├── ScreenManager.java
│ │ │ │ ├── SecondActivity.java
│ │ │ │ ├── DefendService.java
│ │ │ │ ├── ScreenReceiverUtil.java
│ │ │ │ ├── HostService.java
│ │ │ │ ├── DefendManager.java
│ │ │ │ ├── ThreadManager.java
│ │ │ │ └── WorkService.java
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── example
│ │ │ └── administrator
│ │ │ └── lockservice
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── example
│ │ └── administrator
│ │ └── lockservice
│ │ └── ExampleInstrumentedTest.java
├── build.gradle
└── proguard-rules.pro
├── settings.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradle.properties
├── gradlew.bat
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/app/lock.jks:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LiMedici/LockService/HEAD/app/lock.jks
--------------------------------------------------------------------------------
/app/lock2.jks:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LiMedici/LockService/HEAD/app/lock2.jks
--------------------------------------------------------------------------------
/app/debug/app-debug.apk:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LiMedici/LockService/HEAD/app/debug/app-debug.apk
--------------------------------------------------------------------------------
/app/src/main/res/raw/silent.mp3:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LiMedici/LockService/HEAD/app/src/main/res/raw/silent.mp3
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | LockService
3 |
4 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LiMedici/LockService/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LiMedici/LockService/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LiMedici/LockService/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LiMedici/LockService/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LiMedici/LockService/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LiMedici/LockService/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LiMedici/LockService/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LiMedici/LockService/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LiMedici/LockService/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LiMedici/LockService/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LiMedici/LockService/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/debug/output.json:
--------------------------------------------------------------------------------
1 | [{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":1,"versionName":"1.0","enabled":true,"outputFile":"app-debug.apk","fullName":"debug","baseName":"debug"},"path":"app-debug.apk","properties":{}}]
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Fri May 18 12:38:23 GMT+08:00 2018
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-4.4-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_single_pixel.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/app/src/test/java/com/example/administrator/lockservice/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.example.administrator.lockservice;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_splash.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
18 |
19 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/example/administrator/lockservice/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.example.administrator.lockservice;
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 | * Instrumented 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() {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.example.administrator.lockservice", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/administrator/lockservice/RestartServiceReceiver.java:
--------------------------------------------------------------------------------
1 | package com.example.administrator.lockservice;
2 |
3 | import android.app.Service;
4 | import android.content.BroadcastReceiver;
5 | import android.content.Context;
6 | import android.content.Intent;
7 |
8 | /**
9 | * @desc 被通知启动所有核心服务
10 | * @author 李宗好
11 | */
12 | public class RestartServiceReceiver extends BroadcastReceiver {
13 |
14 | public static final String RESTART_SERVICE_ACTION = RestartServiceReceiver.class.getCanonicalName();
15 |
16 | public RestartServiceReceiver() {
17 | }
18 |
19 | @Override
20 | public void onReceive(Context context, Intent intent) {
21 | String action = intent.getAction();
22 | if(RESTART_SERVICE_ACTION.equals(action)){
23 | Class clazz = (Class) intent.getSerializableExtra("clazz");
24 | DefendManager.getInstance().startService(context,clazz);
25 | }
26 |
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_second.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
14 |
15 |
16 |
17 |
23 |
24 |
30 |
31 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | signingConfigs {
5 | config {
6 | keyAlias 'medici1'
7 | keyPassword '565656'
8 | storeFile file('E:/AndroidStudioWorkspance/LockService/app/lock2.jks')
9 | storePassword '565656'
10 | }
11 | }
12 | compileSdkVersion 27
13 | defaultConfig {
14 | applicationId "com.example.administrator.lockservice"
15 | minSdkVersion 19
16 | targetSdkVersion 27
17 | versionCode 1
18 | versionName "1.0"
19 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
20 | signingConfig signingConfigs.config
21 | }
22 | buildTypes {
23 | release {
24 | minifyEnabled false
25 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
26 | }
27 | debug {
28 | minifyEnabled true
29 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
30 | signingConfig signingConfigs.config
31 | }
32 | }
33 | productFlavors {
34 | }
35 | }
36 |
37 | dependencies {
38 | implementation fileTree(include: ['*.jar'], dir: 'libs')
39 | implementation 'com.android.support:appcompat-v7:27.0.2'
40 | implementation 'com.android.support.constraint:constraint-layout:1.0.2'
41 | testImplementation 'junit:junit:4.12'
42 | androidTestImplementation 'com.android.support.test:runner:1.0.1'
43 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
44 | }
45 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/administrator/lockservice/SplashActivity.java:
--------------------------------------------------------------------------------
1 | package com.example.administrator.lockservice;
2 |
3 | import android.support.v7.app.AppCompatActivity;
4 | import android.os.Bundle;
5 | import android.view.View;
6 |
7 | public class SplashActivity extends AppCompatActivity {
8 |
9 | private ScreenManager mScreenManager;
10 | private ScreenReceiverUtil mScreenReceiverUtil;
11 |
12 | @Override
13 | protected void onCreate(Bundle savedInstanceState) {
14 | super.onCreate(savedInstanceState);
15 | setContentView(R.layout.activity_splash);
16 | // 数据的初始化操作
17 | DefendManager.getInstance().startService(this,WorkService.class);
18 | DefendManager.getInstance().startService(this,DefendService.class);
19 | // 监听屏幕状态,显示1像素页面,使应用锁屏至后台能够存活
20 | mScreenManager = ScreenManager.getScreenManagerInstance(this);
21 | mScreenReceiverUtil = new ScreenReceiverUtil(this);
22 | mScreenReceiverUtil.begin(mScreenListener);
23 | }
24 |
25 | private ScreenReceiverUtil.ScreenStateListener mScreenListener = new ScreenReceiverUtil.ScreenStateListener() {
26 | @Override
27 | public void onScreenOn() {
28 | // 移出"1像素屏幕"
29 | mScreenManager.finishActivity();
30 | // 数据的初始化操作
31 | DefendManager.getInstance().startService(SplashActivity.this,WorkService.class);
32 | }
33 |
34 | @Override
35 | public void onScreenOff() {
36 | // 接到锁屏广播,将SportsActivity切换到可见模式
37 | // "咕咚"、"乐动力"、"悦动圈"就是这么做滴
38 | // 那么,我们就制造个"1像素"惨案
39 | mScreenManager.startActivity();
40 | }
41 |
42 | @Override
43 | public void onUserPresent() {
44 |
45 | }
46 | };
47 |
48 | public void enter(View view){
49 | SecondActivity.show(this);
50 | finish();
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/administrator/lockservice/SinglePixelActivity.java:
--------------------------------------------------------------------------------
1 | package com.example.administrator.lockservice;
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 | * @desc 1像素的Activity
13 | */
14 | public class SinglePixelActivity extends AppCompatActivity {
15 |
16 | public static final String PIXEL_KEY = "1像素惨案";
17 |
18 | private static final String TAG = "SinglePixelActivity";
19 |
20 | @Override
21 | protected void onCreate(@Nullable Bundle savedInstanceState) {
22 | super.onCreate(savedInstanceState);
23 | setContentView(R.layout.activity_single_pixel);
24 | initData();
25 | }
26 |
27 | private void initData() {
28 |
29 | // 获得activity的Window对象,设置其属性
30 | Window mWindow = getWindow();
31 | mWindow.setGravity(Gravity.LEFT | Gravity.TOP);
32 | WindowManager.LayoutParams attrParams = mWindow.getAttributes();
33 | attrParams.x = 0;
34 | attrParams.y = 0;
35 | attrParams.height = 1;
36 | attrParams.width = 1;
37 | mWindow.setAttributes(attrParams);
38 | // 绑定SinglePixelActivity到ScreenManager
39 | ScreenManager.getScreenManagerInstance(this).setSingleActivity(this);
40 | }
41 |
42 | @Override
43 | protected void onDestroy() {
44 | Log.w(TAG,"onDestroy--->1像素保活被终止");
45 | /** APP被干掉了,我要重启它
46 | if(!ActivityUtil.isActivityExistsInStack(this)){
47 | Intent intentAlive = new Intent(this, SplashActivity.class);
48 | startActivity(intentAlive);
49 | Log.i(TAG,"SinglePixelActivity---->APP被干掉了,我要重启它");
50 | }
51 | */
52 | super.onDestroy();
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/administrator/lockservice/ScreenManager.java:
--------------------------------------------------------------------------------
1 | package com.example.administrator.lockservice;
2 |
3 | import android.app.Activity;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import android.util.Log;
7 |
8 | import java.lang.ref.WeakReference;
9 |
10 | /**
11 | * ***************************************
12 | * @desc: 对1像素界面的管理
13 | * @author:李宗好
14 | * @time: 2017/11/20 0020 09:46
15 | * @email:lzh@cnbisoft.com
16 | * @version:
17 | * @history:
18 | * ***************************************
19 | */
20 | public class ScreenManager {
21 |
22 | private static final String TAG = "ScreenManager";
23 |
24 | private static ScreenManager mScreenManager;
25 |
26 | private Context mContext;
27 | /**
28 | * 使用弱引用,防止内存泄漏
29 | */
30 | private WeakReference mActivityRef;
31 |
32 | private ScreenManager(Context mContext){
33 | this.mContext = mContext;
34 | }
35 |
36 | /**
37 | * 单例模式
38 | * @param context
39 | * @return
40 | */
41 | public static ScreenManager getScreenManagerInstance(Context context){
42 | if(mScreenManager == null){
43 | mScreenManager = new ScreenManager(context);
44 | }
45 | return mScreenManager;
46 | }
47 |
48 | /**
49 | * 获得SinglePixelActivity的引用
50 | * @param mActivity Activity
51 | */
52 | public void setSingleActivity(Activity mActivity){
53 | mActivityRef = new WeakReference<>(mActivity);
54 | }
55 |
56 | /**
57 | * 启动SinglePixelActivity
58 | */
59 | public void startActivity(){
60 | Log.w(TAG,"准备启动SinglePixelActivity...");
61 | Intent intent = new Intent(mContext,SinglePixelActivity.class);
62 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
63 | mContext.startActivity(intent);
64 | }
65 |
66 | /**
67 | * 结束SinglePixelActivity
68 | */
69 | public void finishActivity(){
70 | Log.w(TAG,"准备结束SinglePixelActivity...");
71 | if(mActivityRef != null){
72 | Activity mActivity = mActivityRef.get();
73 | if(mActivity != null){
74 | mActivity.finish();
75 | }
76 | }
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/administrator/lockservice/SecondActivity.java:
--------------------------------------------------------------------------------
1 | package com.example.administrator.lockservice;
2 |
3 | import android.content.Context;
4 | import android.content.Intent;
5 | import android.os.Handler;
6 | import android.support.v4.content.LocalBroadcastManager;
7 | import android.support.v7.app.AppCompatActivity;
8 | import android.os.Bundle;
9 | import android.util.Log;
10 | import android.view.View;
11 | import android.widget.ArrayAdapter;
12 | import android.widget.ListView;
13 |
14 | import java.text.SimpleDateFormat;
15 | import java.util.ArrayList;
16 | import java.util.Date;
17 | import java.util.List;
18 |
19 | public class SecondActivity extends AppCompatActivity
20 | implements WorkService.Callback{
21 |
22 | private ListView mListView;
23 | private ArrayAdapter mAdapter;
24 | private List listData;
25 |
26 | private Handler mHandler = new Handler();
27 |
28 | @Override
29 | protected void onCreate(Bundle savedInstanceState) {
30 | super.onCreate(savedInstanceState);
31 | setContentView(R.layout.activity_second);
32 | mListView = findViewById(R.id.listView);
33 | listData = new ArrayList<>();
34 | mAdapter = new ArrayAdapter(this,android.R.layout.simple_expandable_list_item_1,listData);
35 | mListView.setAdapter(mAdapter);
36 | // 启动请求结果回调
37 | WorkService.setCallback(this);
38 | }
39 |
40 | @Override
41 | public void onResult(String result) {
42 | // 运行在子线程
43 | mHandler.post(new Runnable() {
44 | @Override
45 | public void run() {
46 | String data = "网络请求成功: "+new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss").format(new Date())+":===========>" + (listData.size() + 1);
47 | Log.e("WorkService","WorkService: ====== " + data);
48 | listData.add(data);
49 | mAdapter.notifyDataSetChanged();
50 | }
51 | });
52 |
53 | }
54 |
55 | /**
56 | * 启动任务
57 | * @param view
58 | */
59 | public void start(View view){
60 | Intent intent = new Intent(WorkService.WORK_RECEIVER_ACTION);
61 | intent.putExtra(WorkService.EXTRA_WORK_FLAG,true);
62 | LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
63 | }
64 |
65 | /**
66 | * 暂停任务
67 | * @param view
68 | */
69 | public void stop(View view){
70 | Intent intent = new Intent(WorkService.WORK_RECEIVER_ACTION);
71 | intent.putExtra(WorkService.EXTRA_WORK_FLAG,false);
72 | LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
73 | }
74 |
75 | public static void show(Context context) {
76 | Intent intent = new Intent(context,SecondActivity.class);
77 | context.startActivity(intent);
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
34 |
35 |
36 |
41 |
42 |
43 |
48 |
49 |
50 |
55 |
56 |
57 |
62 |
63 |
64 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/administrator/lockservice/DefendService.java:
--------------------------------------------------------------------------------
1 | package com.example.administrator.lockservice;
2 |
3 | import android.app.Notification;
4 | import android.app.Service;
5 | import android.content.Intent;
6 | import android.content.IntentFilter;
7 | import android.content.ServiceConnection;
8 | import android.media.MediaPlayer;
9 | import android.os.Build;
10 | import android.os.IBinder;
11 | import android.support.annotation.Nullable;
12 | import android.support.v4.content.LocalBroadcastManager;
13 | import android.util.Log;
14 |
15 | /**
16 | * @Desc 守护线程。保证核心服务不被杀死 服务互拉
17 | * @author 李宗好
18 | * @time:203月20日上午10:09:25
19 | */
20 | public class DefendService extends HostService {
21 |
22 | private static final int SERVICE_ID = 1 << 0;
23 |
24 | private MediaPlayer mMediaPlayer;
25 |
26 | @Nullable
27 | @Override
28 | public IBinder onBind(Intent intent) {
29 | return null;
30 | }
31 |
32 | @Override
33 | public int onStartCommand(Intent intent, int flags, int startId) {
34 | flags = START_NOT_STICKY;
35 |
36 | if (Build.VERSION.SDK_INT < 18) {
37 | // API < 18 ,此方法能有效隐藏Notification上的图标
38 | startForeground(SERVICE_ID, new Notification());
39 | } else {
40 | //进行灰色保活手段
41 | Intent innerIntent = new Intent(this, DefendInnerService.class);
42 | startService(innerIntent);
43 | startForeground(SERVICE_ID, new Notification());
44 | }
45 |
46 | //注册广播
47 | IntentFilter filter = new IntentFilter(RestartServiceReceiver.RESTART_SERVICE_ACTION);
48 | LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(new RestartServiceReceiver(),filter);
49 |
50 | //播放无声音乐
51 | ThreadManager.getLongPool().execute(new Runnable() {
52 | @Override
53 | public void run() {
54 | startPlayMusic();
55 | }
56 | });
57 |
58 | return super.onStartCommand(intent, flags, startId);
59 | }
60 |
61 | @Override
62 | public void unbindService(ServiceConnection conn) {
63 | super.unbindService(conn);
64 | }
65 |
66 | /**
67 | * 播放无声音乐
68 | */
69 | private void startPlayMusic(){
70 | try{
71 | mMediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.silent);
72 | mMediaPlayer.setLooping(true);
73 | mMediaPlayer.start();
74 | }catch (Exception e){
75 | e.printStackTrace();
76 | Log.e("DefendService",e.toString());
77 | }
78 |
79 | }
80 |
81 | private void stopPlayMusic(){
82 | if(mMediaPlayer != null){
83 | mMediaPlayer.stop();
84 | }
85 | }
86 |
87 | @Override
88 | public void onDestroy() {
89 | super.onDestroy();
90 |
91 | stopPlayMusic();
92 |
93 | //发送广播,重新启动服务
94 | Intent mIntent = new Intent();
95 | mIntent.setAction(RestartServiceReceiver.RESTART_SERVICE_ACTION);
96 | mIntent.putExtra("clazz",this.getClass());
97 | LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(mIntent);
98 | Log.e("DefendService",this.getClass().getCanonicalName() + ":" + "被杀死了");
99 | }
100 |
101 |
102 | /**
103 | * 给 API >= 18 的平台上用的灰色保活手段
104 | */
105 | public static class DefendInnerService extends Service {
106 |
107 | @Nullable
108 | @Override
109 | public IBinder onBind(Intent intent) {
110 | return null;
111 | }
112 |
113 | @Override
114 | public int onStartCommand(Intent intent, int flags, int startId) {
115 | startForeground(SERVICE_ID, new Notification());
116 | stopForeground(true);
117 | stopSelf();
118 | return super.onStartCommand(intent, flags, startId);
119 | }
120 | }
121 |
122 | }
123 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/administrator/lockservice/ScreenReceiverUtil.java:
--------------------------------------------------------------------------------
1 | package com.example.administrator.lockservice;
2 |
3 | import android.content.BroadcastReceiver;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import android.content.IntentFilter;
7 | import android.os.PowerManager;
8 |
9 | /**
10 | * ***************************************
11 | * desc:锁屏,开屏,解锁监听的工具类
12 | * author:李宗好
13 | * version:
14 | * history:
15 | * ***************************************
16 | */
17 | public class ScreenReceiverUtil {
18 |
19 | private Context mContext;
20 | private ScreenBroadcastReceiver mScreenReceiver;
21 | private ScreenStateListener mScreenStateListener;
22 |
23 | public ScreenReceiverUtil(Context context) {
24 | mContext = context;
25 | mScreenReceiver = new ScreenBroadcastReceiver();
26 | }
27 |
28 | /**
29 | * screen状态广播接收者
30 | */
31 | private class ScreenBroadcastReceiver extends BroadcastReceiver {
32 | private String action = null;
33 |
34 | @Override
35 | public void onReceive(Context context, Intent intent) {
36 | action = intent.getAction();
37 | if (Intent.ACTION_SCREEN_ON.equals(action)) {
38 | // 开屏
39 | mScreenStateListener.onScreenOn();
40 | } else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
41 | // 锁屏
42 | mScreenStateListener.onScreenOff();
43 | } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
44 | // 解锁
45 | mScreenStateListener.onUserPresent();
46 | }
47 | }
48 | }
49 |
50 | /**
51 | * 开始监听screen状态
52 | *
53 | * @param listener
54 | */
55 | public void begin(ScreenStateListener listener) {
56 | mScreenStateListener = listener;
57 | registerListener();
58 | getScreenState();
59 | }
60 |
61 | /**
62 | * 获取screen状态
63 | */
64 | private void getScreenState() {
65 | PowerManager manager = (PowerManager) mContext
66 | .getSystemService(Context.POWER_SERVICE);
67 | if (manager.isScreenOn()) {
68 | if (mScreenStateListener != null) {
69 | mScreenStateListener.onScreenOn();
70 | }
71 | } else {
72 | if (mScreenStateListener != null) {
73 | mScreenStateListener.onScreenOff();
74 | }
75 | }
76 | }
77 |
78 | /**
79 | * 停止screen状态监听
80 | */
81 | public void unregisterListener() {
82 | mContext.unregisterReceiver(mScreenReceiver);
83 | }
84 |
85 | /**
86 | * 启动screen状态广播接收器
87 | */
88 | private void registerListener() {
89 | IntentFilter filter = new IntentFilter();
90 | filter.addAction(Intent.ACTION_SCREEN_ON);
91 | filter.addAction(Intent.ACTION_SCREEN_OFF);
92 | filter.addAction(Intent.ACTION_USER_PRESENT);
93 | mContext.registerReceiver(mScreenReceiver, filter);
94 | }
95 |
96 | /**
97 | * 返回给调用者屏幕状态信息
98 | */
99 | public interface ScreenStateListener {
100 |
101 | /**
102 | * 屏幕打开的回调
103 | */
104 | public void onScreenOn();
105 |
106 | /**
107 | * 屏幕关闭的回调
108 | */
109 | public void onScreenOff();
110 |
111 | /**
112 | * 屏幕解锁的回调
113 | */
114 | public void onUserPresent();
115 | }
116 | }
117 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/administrator/lockservice/HostService.java:
--------------------------------------------------------------------------------
1 | package com.example.administrator.lockservice;
2 |
3 |
4 | import android.app.Service;
5 | import android.content.Context;
6 | import android.content.Intent;
7 | import android.os.IBinder;
8 | import android.os.PowerManager;
9 | import android.util.Log;
10 |
11 | import java.util.Timer;
12 | import java.util.TimerTask;
13 |
14 | /**
15 | * @desc:唤醒电源锁的公共类 和服务互相守护的基类
16 | * @time: 2017/7/29 0029 17:58
17 | */
18 | public class HostService extends Service {
19 |
20 | private DefendManager mDefendManager;
21 |
22 | private Timer mTimer = null;
23 |
24 | /**
25 | * 唤醒锁
26 | */
27 | private PowerManager.WakeLock mWakeLock = null;
28 |
29 |
30 | @Override
31 | public void onCreate() {
32 | super.onCreate();
33 | mDefendManager = DefendManager.getInstance();
34 | mTimer = new Timer();
35 | mTimer.schedule(new DefendTimerTask(), 0, 1000);
36 | acquireWakeLock();
37 | }
38 |
39 | @Override
40 | public IBinder onBind(Intent intent) {
41 | return null;
42 | }
43 |
44 | @Override
45 | public int onStartCommand(Intent intent, int flags, int startId) {
46 | // START_STICKY:如果service进程被kill掉,保留service的状态为开始状态,但不保留递送的intent对象。
47 | // 随后系统会尝试重新创建service,由于服务状态为开始状态,所以创建服务后一定会调用onStartCommand(Intent,int,int)方法。
48 | // 如果在此期间没有任何启动命令被传递到service,那么参数Intent将为null。
49 |
50 | // START_NOT_STICKY:"非粘性的"。使用这个返回值时,如果在执行完onStartCommand后,服务被异常kill掉,系统不会自动重启该服务。
51 |
52 | // START_REDELIVER_INTENT:重传Intent。使用这个返回值时,如果在执行完onStartCommand后,服务被异常kill掉,系统会自动重启该服务,并将Intent的值传入。
53 |
54 | // START_STICKY_COMPATIBILITY:START_STICKY的兼容版本,但不保证服务被kill后一定能重启。
55 | return super.onStartCommand(intent, flags, startId);
56 | }
57 |
58 | private void acquireWakeLock()
59 | {
60 | if (null == mWakeLock)
61 | {
62 | PowerManager pm = (PowerManager)this.getSystemService(Context.POWER_SERVICE);
63 | mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK|PowerManager.ON_AFTER_RELEASE, "HostService");
64 | if (null != mWakeLock)
65 | {
66 | Log.e(this.getClass().getCanonicalName()," ======= 唤醒电源锁");
67 | mWakeLock.acquire();
68 | }
69 | }
70 | }
71 | //释放设备电源锁
72 | private void releaseWakeLock()
73 | {
74 | if (null != mWakeLock)
75 | {
76 | Log.e(this.getClass().getCanonicalName()," ======= 释放电源锁");
77 | mWakeLock.release();
78 | mWakeLock = null;
79 | }
80 | }
81 |
82 |
83 | @Override
84 | public boolean onUnbind(Intent intent) {
85 | return super.onUnbind(intent);
86 | }
87 |
88 | @Override
89 | public void onDestroy() {
90 | super.onDestroy();
91 | releaseWakeLock();
92 | }
93 |
94 | /**
95 | * @Desc 待执行的任务
96 | * @author 李宗好
97 | * @time:2017年3月20日上午10:17:37
98 | */
99 | private class DefendTimerTask extends TimerTask {
100 |
101 | @Override
102 | public void run() {
103 | boolean serviceWorked = mDefendManager.isAllServiceWorked(HostService.this);
104 | Log.e(this.getClass().getCanonicalName(),"HostService ----------> " + serviceWorked);
105 | if(!serviceWorked) {
106 | mDefendManager.startAllKilledService(HostService.this);
107 | }
108 | }
109 |
110 | }
111 |
112 |
113 | }
114 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/administrator/lockservice/DefendManager.java:
--------------------------------------------------------------------------------
1 | package com.example.administrator.lockservice;
2 |
3 | import android.app.ActivityManager;
4 | import android.app.Service;
5 | import android.content.Context;
6 | import android.content.Intent;
7 | import android.os.Bundle;
8 | import android.support.annotation.CheckResult;
9 | import android.support.v4.util.ArrayMap;
10 | import android.util.Log;
11 |
12 | import java.util.ArrayList;
13 | import java.util.Map;
14 | import java.util.Set;
15 |
16 | /**
17 | * @desc:Service 互相监听是否被杀死,互相重启进程
18 | * @author:李宗好
19 | * @time: 2017/7/29 0029 16:13
20 | */
21 | public class DefendManager {
22 |
23 | private static DefendManager mDefendManager;
24 |
25 | private static ArrayMap> mClassMap;
26 |
27 | /**
28 | * 私有化构造函数
29 | */
30 | private DefendManager(){
31 | mClassMap = new ArrayMap<>();
32 | }
33 |
34 | /**
35 | * 单例设计模式
36 | * @return
37 | */
38 | public static DefendManager getInstance(){
39 | if(null == mDefendManager){
40 | synchronized (DefendManager.class){
41 | if(null == mDefendManager){
42 | mDefendManager = new DefendManager();
43 | }
44 | }
45 | }
46 | return mDefendManager;
47 | }
48 |
49 | /**
50 | * 判断某所有服务是否正常工作
51 | * @param context
52 | * @return
53 | */
54 | @CheckResult
55 | public static boolean isAllServiceWorked(final Context context) {
56 | ActivityManager myManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
57 | ArrayList runningService = (ArrayList) myManager.getRunningServices(Integer.MAX_VALUE);
58 | int worked= 0;
59 | for (Map.Entry> entry:mClassMap.entrySet()) {
60 | for (int i = 0; i < runningService.size(); i++) {
61 | Class extends Service> serviceClass = entry.getValue();
62 | String runningServiceName = runningService.get(i).service.getClassName().toString();
63 | if (runningServiceName.equals(serviceClass.getCanonicalName())) {
64 | Log.v(DefendManager.class.getCanonicalName(),runningServiceName+" ======= 正在运行");
65 | worked ++;
66 | }
67 | }
68 | }
69 | Log.v(DefendManager.class.getCanonicalName(),"正在运行进程数 ======= " + worked);
70 | return worked == mClassMap.size();
71 |
72 | }
73 |
74 | /**
75 | * 启动某个服务
76 | * @param context 上下文
77 | * @param clx 启动的Service clx对象
78 | */
79 | public void startService(final Context context, final Class extends Service> clx){
80 | // 启动Service
81 | Intent intent = new Intent(context, clx);
82 | // 高版本Service启动方式
83 | String canonicalName = clx.getCanonicalName();
84 | intent.setPackage(canonicalName);
85 | context.startService(intent);
86 | // remove clx , 取消重复
87 | mClassMap.remove(canonicalName);
88 | mClassMap.put(canonicalName,clx);
89 | }
90 |
91 | /**
92 | * 启动所有可能被killed的service
93 | */
94 | public void startAllKilledService(final Context context){
95 | for (Map.Entry> entry:mClassMap.entrySet()) {
96 | Class extends Service> serviceClass = entry.getValue();
97 | startService(context,serviceClass);
98 | }
99 | }
100 |
101 |
102 | }
103 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # This is a configuration file for ProGuard.
2 | # http://proguard.sourceforge.net/index.html#manual/usage.html
3 |
4 | #代码迭代优化的次数,取值范围0~7,默认为5
5 | -optimizationpasses 5
6 | #包名不使用大小写混合的方式,这样混合后都是小写字母的组合
7 | -dontusemixedcaseclassnames
8 | #混淆时不做预检验,预检验是Proguard四大功能给之一,在Android中一般不需要预检验,加快混淆速度
9 | -dontpreverify
10 | #混淆时是否记录日志
11 | -verbose
12 | # 混淆时所采用的算法
13 | -optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
14 | #如果项目中适用到注解,需要保留注解属性
15 | -keepattributes *Annotation*
16 | -keepattributes *Annotation*,InnerClasses
17 |
18 | #不混淆泛型
19 | -keepattributes Signature
20 | #保留代码行号,这在混淆厚代码运行中抛出异常信息时,有利于定位出问题的代码
21 | -keepattributes SourceFile,LineNumberTable
22 | #屏蔽警告
23 | -ignorewarnings
24 |
25 | # 保持 Android SDK 相关API类不被混淆
26 | -keep public class * extends android.app.Fragment
27 | -keep public class * extends android.app.Activity
28 | -keep public class * extends android.app.Application
29 | -keep public class * extends android.app.Service
30 | -keep public class * extends android.content.BroadcastReceiver
31 | -keep public class * extends android.content.ContentProvider
32 | -keep public class * extends android.app.backup.BackupAgentHelper
33 | -keep public class * extends android.preference.Preference
34 | -keep public class * extends android.support.v4.**
35 | -keep public class com.android.vending.licensing.ILicensingService
36 |
37 | #support包下混淆配置
38 | -dontwarn android.support.**
39 | #-keep class android.support.**.{*;}
40 |
41 | #如果有引用v4包可以添加下面这行
42 | -keep public class * extends android.support.v4.app.Fragment
43 |
44 | #保持自定义控件类不被混淆
45 | -keepclasseswithmembers class * {
46 | public (android.content.Context, android.util.AttributeSet);
47 | }
48 |
49 | -keepclasseswithmembers class * {
50 | public (android.content.Context, android.util.AttributeSet,int);
51 | }
52 |
53 | #保持自定义控件类不被混淆
54 | -keep public class * extends android.view.View {
55 | public (android.content.Context);
56 | public (android.content.Context, android.util.AttributeSet);
57 | public (android.content.Context, android.util.AttributeSet, int);
58 | public void set*(...);
59 | }
60 |
61 | #保持Activity中参数是View类型的函数,保证Layout XML 文件中配置onClick属性的值能够正常调用到
62 | -keepclassmembers class * extends android.app.Activity{
63 | public void *(android.view.View);
64 | }
65 |
66 | #保持 native 方法不被混淆
67 | -keepclasseswithmembernames class * {
68 | native ;
69 | }
70 |
71 | #保持 Parcelable 不被混淆
72 | -keep class * implements android.os.Parcelable {
73 | public static final android.os.Parcelable$Creator *;
74 | }
75 |
76 | #保持 Serializable 不被混淆
77 | -keepnames class * implements java.io.Serializable
78 |
79 | #保持 Serializable 不被混淆并且enum 类也不被混淆
80 | -keepclassmembers class * implements java.io.Serializable {
81 | static final long serialVersionUID;
82 | private static final java.io.ObjectStreamField[] serialPersistentFields;
83 | !static !transient ;
84 | !private ;
85 | !private ;
86 | private void writeObject(java.io.ObjectOutputStream);
87 | private void readObject(java.io.ObjectInputStream);
88 | java.lang.Object writeReplace();
89 | java.lang.Object readResolve();
90 | }
91 |
92 | #保持枚举 enum 类不被混淆 如果混淆报错,建议直接使用上面的 -keepclassmembers class * implements java.io.Serializable即可
93 | -keepclassmembers enum * {
94 | public static **[] values();
95 | public static ** valueOf(java.lang.String);
96 | }
97 |
98 | #不混淆资源类
99 | -keepclassmembers class **.R$* {
100 | *;
101 | }
102 |
103 | # Understand the @Keep support annotation.
104 | -keep class android.support.annotation.Keep
105 |
106 | -keep @android.support.annotation.Keep class * {*;}
107 |
108 | -keepclasseswithmembers class * {
109 | @android.support.annotation.Keep ;
110 | }
111 |
112 | -keepclasseswithmembers class * {
113 | @android.support.annotation.Keep ;
114 | }
115 |
116 | -keepclasseswithmembers class * {
117 | @android.support.annotation.Keep (...);
118 | }
119 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/administrator/lockservice/ThreadManager.java:
--------------------------------------------------------------------------------
1 | package com.example.administrator.lockservice;
2 |
3 | /*
4 | * File Name: ThreadManager.java
5 | * Created by 2016年12月15日 下午1:20:57
6 | */
7 |
8 | import java.util.HashMap;
9 | import java.util.Map;
10 | import java.util.concurrent.Executors;
11 | import java.util.concurrent.LinkedBlockingQueue;
12 | import java.util.concurrent.ThreadPoolExecutor;
13 | import java.util.concurrent.ThreadPoolExecutor.AbortPolicy;
14 | import java.util.concurrent.TimeUnit;
15 |
16 | /**
17 | *
18 | * @desc 一个简易的线程池管理类,提供三个线程池
19 | * @author 李宗好
20 | * @time:2016年12月15日 下午1:20:57
21 | *
22 | */
23 | public final class ThreadManager {
24 | public static final String DEFAULT_SINGLE_POOL_NAME = "DEFAULT_SINGLE_POOL_NAME";
25 |
26 | private static ThreadPoolProxy mLongPool = null;
27 | private static Object mLongLock = new Object();
28 |
29 | private static ThreadPoolProxy mShortPool = null;
30 | private static Object mShortLock = new Object();
31 |
32 | private static ThreadPoolProxy mDownloadPool = null;
33 | private static Object mDownloadLock = new Object();
34 |
35 | private static Map mMap = new HashMap();
36 | private static Object mSingleLock = new Object();
37 |
38 | /** 获取下载线程 */
39 | public static ThreadPoolProxy getDownloadPool() {
40 | synchronized (mDownloadLock) {
41 | if (mDownloadPool == null) {
42 | mDownloadPool = new ThreadPoolProxy(3, 3, 5L);
43 | }
44 | return mDownloadPool;
45 | }
46 | }
47 |
48 | /** 获取一个用于执行长耗时任务的线程池,避免和短耗时任务处在同一个队列而阻塞了重要的短耗时任务,通常用来联网操作 */
49 | public static ThreadPoolProxy getLongPool() {
50 | synchronized (mLongLock) {
51 | if (mLongPool == null) {
52 | mLongPool = new ThreadPoolProxy(5, 5, 5L);
53 | }
54 | return mLongPool;
55 | }
56 | }
57 |
58 | /** 获取一个用于执行短耗时任务的线程池,避免因为和耗时长的任务处在同一个队列而长时间得不到执行,通常用来执行本地的IO/SQL */
59 | public static ThreadPoolProxy getShortPool() {
60 | synchronized (mShortLock) {
61 | if (mShortPool == null) {
62 | mShortPool = new ThreadPoolProxy(2, 2, 5L);
63 | }
64 | return mShortPool;
65 | }
66 | }
67 |
68 | /** 获取一个单线程池,所有任务将会被按照加入的顺序执行,免除了同步开销的问题 */
69 | public static ThreadPoolProxy getSinglePool() {
70 | return getSinglePool(DEFAULT_SINGLE_POOL_NAME);
71 | }
72 |
73 | /** 获取一个单线程池,所有任务将会被按照加入的顺序执行,免除了同步开销的问题 */
74 | public static ThreadPoolProxy getSinglePool(String name) {
75 | synchronized (mSingleLock) {
76 | ThreadPoolProxy singlePool = mMap.get(name);
77 | if (singlePool == null) {
78 | singlePool = new ThreadPoolProxy(1, 1, 5L);
79 | mMap.put(name, singlePool);
80 | }
81 | return singlePool;
82 | }
83 | }
84 |
85 | public static class ThreadPoolProxy {
86 | private ThreadPoolExecutor mPool;
87 | private int mCorePoolSize;
88 | private int mMaximumPoolSize;
89 | private long mKeepAliveTime;
90 |
91 | private ThreadPoolProxy(int corePoolSize, int maximumPoolSize, long keepAliveTime) {
92 | mCorePoolSize = corePoolSize;
93 | mMaximumPoolSize = maximumPoolSize;
94 | mKeepAliveTime = keepAliveTime;
95 | }
96 |
97 | /** 执行任务,当线程池处于关闭,将会重新创建新的线程池 */
98 | public synchronized void execute(Runnable run) {
99 | if (run == null) {
100 | return;
101 | }
102 | if (mPool == null || mPool.isShutdown()) {
103 | //参数说明
104 | //当线程池中的线程小于mCorePoolSize,直接创建新的线程加入线程池执行任务
105 | //当线程池中的线程数目等于mCorePoolSize,将会把任务放入任务队列BlockingQueue中
106 | //当BlockingQueue中的任务放满了,将会创建新的线程去执行,
107 | //但是当总线程数大于mMaximumPoolSize时,将会抛出异常,交给RejectedExecutionHandler处理
108 | //mKeepAliveTime是线程执行完任务后,且队列中没有可以执行的任务,存活的时间,后面的参数是时间单位
109 | //ThreadFactory是每次创建新的线程工厂
110 | mPool = new ThreadPoolExecutor(mCorePoolSize, mMaximumPoolSize, mKeepAliveTime, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(), Executors.defaultThreadFactory(), new AbortPolicy());
111 | }
112 | mPool.execute(run);
113 | }
114 |
115 | /** 取消线程池中某个还未执行的任务 */
116 | public synchronized void cancel(Runnable run) {
117 | if (mPool != null && (!mPool.isShutdown() || mPool.isTerminating())) {
118 | mPool.getQueue().remove(run);
119 | }
120 | }
121 |
122 | /** 取消线程池中某个还未执行的任务 */
123 | public synchronized boolean contains(Runnable run) {
124 | if (mPool != null && (!mPool.isShutdown() || mPool.isTerminating())) {
125 | return mPool.getQueue().contains(run);
126 | } else {
127 | return false;
128 | }
129 | }
130 |
131 | /** 立刻关闭线程池,并且正在执行的任务也将会被中断 */
132 | public void stop() {
133 | if (mPool != null && (!mPool.isShutdown() || mPool.isTerminating())) {
134 | mPool.shutdownNow();
135 | }
136 | }
137 |
138 | /** 平缓关闭单任务线程池,但是会确保所有已经加入的任务都将会被执行完毕才关闭 */
139 | public synchronized void shutdown() {
140 | if (mPool != null && (!mPool.isShutdown() || mPool.isTerminating())) {
141 | mPool.shutdownNow();
142 | }
143 | }
144 | }
145 | }
146 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/administrator/lockservice/WorkService.java:
--------------------------------------------------------------------------------
1 | package com.example.administrator.lockservice;
2 |
3 | import android.app.Notification;
4 | import android.app.Service;
5 | import android.content.BroadcastReceiver;
6 | import android.content.Context;
7 | import android.content.Intent;
8 | import android.content.IntentFilter;
9 | import android.os.Build;
10 | import android.os.IBinder;
11 | import android.support.annotation.Nullable;
12 | import android.support.v4.content.LocalBroadcastManager;
13 | import android.text.TextUtils;
14 | import android.util.Log;
15 |
16 | import java.io.BufferedReader;
17 | import java.io.IOException;
18 | import java.io.InputStream;
19 | import java.io.InputStreamReader;
20 | import java.net.HttpURLConnection;
21 | import java.net.URL;
22 | import java.util.Timer;
23 | import java.util.TimerTask;
24 |
25 | /**
26 | * @desc 广播服务, 继续在后台运行, 应对从服务器推送通知事件。
27 | * @time:2016年12月18日 下午1:17:25
28 | */
29 | public class WorkService extends HostService {
30 |
31 | private static final int SERVICE_ID = 1 << 1;
32 |
33 | private static final String TAG = WorkService.class.getCanonicalName();
34 |
35 | public static final String WORK_RECEIVER_ACTION = WorkFlagReceiver.class.getCanonicalName();
36 |
37 | public static final String EXTRA_WORK_FLAG = "EXTRA_WORK_FLAG";
38 |
39 | private WorkFlagReceiver mFlagReceiver;
40 |
41 | private Timer mTimer;
42 |
43 | private static Callback mCallback;
44 |
45 | private volatile boolean mWorkFlag;
46 |
47 | @Override
48 | public void onCreate() {
49 | Log.d(TAG, "onCreate()...");
50 | }
51 |
52 | @Override
53 | public int onStartCommand(Intent intent, int flags, int startId) {
54 | // 如果服务被killed了,会重新启动该服务,并传入之前的intent 除非进程被关闭了。
55 | flags = Service.START_REDELIVER_INTENT;
56 |
57 | if (Build.VERSION.SDK_INT < 18) {
58 | //API < 18 ,此方法能有效隐藏Notification上的图标
59 | startForeground(SERVICE_ID, new Notification());
60 | } else {
61 | //进行灰色保活手段
62 | Intent innerIntent = new Intent(this, WorkInnerService.class);
63 | startService(innerIntent);
64 | startForeground(SERVICE_ID, new Notification());
65 | }
66 |
67 | return super.onStartCommand(intent, flags, startId);
68 | }
69 |
70 | @Override
71 | public void onStart(Intent intent, int startId) {
72 | Log.d(TAG, "onStart()...");
73 | registerBroadcastReceiver();
74 | }
75 |
76 | /**
77 | * 启动任务
78 | */
79 | private void doWork(){
80 | // 启动Timer
81 | if(mTimer == null){
82 | mTimer = new Timer();
83 | }
84 |
85 | mTimer.schedule(new TimerTask() {
86 | @Override
87 | public void run() {
88 |
89 | HttpURLConnection connection = null;
90 | BufferedReader reader = null;
91 | try {
92 | URL url = new URL("https://www.baidu.com");
93 | connection = (HttpURLConnection) url.openConnection();
94 | connection.setRequestMethod("GET");
95 | connection.setConnectTimeout(8000);
96 | connection.setReadTimeout(8000);
97 | InputStream in = connection.getInputStream();
98 | //下面对获取到的输入流进行读取
99 | reader = new BufferedReader(new InputStreamReader(in));
100 | StringBuilder response = new StringBuilder();
101 | String line;
102 | while ((line = reader.readLine()) != null){
103 | response.append(line);
104 | }
105 | String result = response.toString();
106 |
107 | if(!TextUtils.isEmpty(result) && mCallback != null){
108 | mCallback.onResult(result);
109 | }
110 | } catch (Exception e) {
111 | e.printStackTrace();
112 | }finally {
113 | if (reader != null){
114 | try {
115 | reader.close();
116 | } catch (IOException e) {
117 | e.printStackTrace();
118 | }
119 | }
120 | if (connection != null){
121 | connection.disconnect();
122 | }
123 | }
124 | }
125 | },0,1000*2);// 2S一个请求
126 | }
127 |
128 | /**
129 | * 关闭任务
130 | */
131 | private void closeWork(){
132 | // 关闭Timer
133 | if(mTimer != null){
134 | mTimer.cancel();
135 | mTimer = null;
136 | }
137 | }
138 |
139 | /**
140 | * 测试Demo 静态方法处理
141 | * @param callback
142 | */
143 | public static void setCallback(Callback callback){
144 | mCallback = callback;
145 | }
146 |
147 | @Nullable
148 | @Override
149 | public IBinder onBind(Intent intent) {
150 | Log.d(TAG, "onBind()...");
151 | return null;
152 | }
153 |
154 | @Override
155 | public void onDestroy() {
156 | Log.d(TAG, "onDestroy()...");
157 | unRegisterBroadcastReceiver();
158 | }
159 |
160 |
161 | /**
162 | * 给 API >= 18 的平台上用的灰色保活手段
163 | */
164 | public static class WorkInnerService extends Service {
165 |
166 | @Nullable
167 | @Override
168 | public IBinder onBind(Intent intent) {
169 | return null;
170 | }
171 |
172 | @Override
173 | public int onStartCommand(Intent intent, int flags, int startId) {
174 | startForeground(SERVICE_ID, new Notification());
175 | stopForeground(true);
176 | stopSelf();
177 | return super.onStartCommand(intent, flags, startId);
178 | }
179 | }
180 |
181 | /**
182 | * 启动注册
183 | */
184 | private void registerBroadcastReceiver(){
185 | mFlagReceiver = new WorkFlagReceiver();
186 | IntentFilter filter = new IntentFilter();
187 | filter.addAction(WORK_RECEIVER_ACTION);
188 | LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(mFlagReceiver,filter);
189 | }
190 |
191 | /**
192 | * 取消注册
193 | */
194 | private void unRegisterBroadcastReceiver(){
195 | if(mFlagReceiver != null){
196 | LocalBroadcastManager.getInstance(getApplicationContext()).unregisterReceiver(mFlagReceiver);
197 | }
198 | }
199 |
200 | /**
201 | * @desc 更新任务Flag
202 | * @author 李宗好
203 | */
204 | public class WorkFlagReceiver extends BroadcastReceiver {
205 |
206 | public WorkFlagReceiver() {
207 | }
208 |
209 | @Override
210 | public void onReceive(Context context, Intent intent) {
211 | String action = intent.getAction();
212 | if(WORK_RECEIVER_ACTION.equals(action)){
213 | boolean flag = intent.getBooleanExtra(EXTRA_WORK_FLAG,false);
214 | mWorkFlag = flag;
215 | if(mWorkFlag){
216 | // doWork
217 | doWork();
218 | }else{
219 | closeWork();
220 | }
221 | }
222 |
223 | }
224 | }
225 |
226 | public interface Callback{
227 | void onResult(String result);
228 | }
229 |
230 |
231 |
232 | }
233 |
--------------------------------------------------------------------------------