├── .gitignore
├── .idea
├── codeStyles
│ └── Project.xml
├── compiler.xml
├── encodings.xml
├── gradle.xml
├── inspectionProfiles
│ └── Project_Default.xml
├── misc.xml
├── modules.xml
├── runConfigurations.xml
└── vcs.xml
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── silencedut
│ │ └── sample
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── silencedut
│ │ │ └── sample
│ │ │ ├── LifeFragment.java
│ │ │ └── MainActivity.java
│ └── res
│ │ ├── drawable-v24
│ │ └── ic_launcher_foreground.xml
│ │ ├── drawable
│ │ └── ic_launcher_background.xml
│ │ ├── layout
│ │ ├── activity_main.xml
│ │ └── fragment_test.xml
│ │ ├── mipmap-anydpi-v26
│ │ ├── ic_launcher.xml
│ │ └── ic_launcher_round.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
│ │ └── values
│ │ ├── colors.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test
│ └── java
│ └── com
│ └── silencedut
│ └── sample
│ └── ExampleUnitTest.java
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
├── taskscheduler
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── silencedut
│ │ └── taskscheduler
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── silencedut
│ │ │ └── taskscheduler
│ │ │ ├── ILog.java
│ │ │ ├── LifecycleRunnableDelegate.java
│ │ │ ├── SafeSchedulerHandler.java
│ │ │ ├── SchedulerTask.java
│ │ │ ├── Task.java
│ │ │ ├── TaskScheduler.java
│ │ │ └── ThreadFactory.java
│ └── res
│ │ └── values
│ │ └── strings.xml
│ └── test
│ └── java
│ └── com
│ └── silencedut
│ └── taskscheduler
│ └── ExampleUnitTest.java
└── version.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 |
11 | proguard-rules.pro
12 |
13 |
--------------------------------------------------------------------------------
/.idea/codeStyles/Project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
21 |
22 |
--------------------------------------------------------------------------------
/.idea/inspectionProfiles/Project_Default.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
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 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 | Android
129 |
130 |
131 | Bitwise operation issuesJava
132 |
133 |
134 | C/C++
135 |
136 |
137 | Concurrency annotation issuesJava
138 |
139 |
140 | Control FlowGroovy
141 |
142 |
143 | Control flow issuesJava
144 |
145 |
146 | CorrectnessLintAndroid
147 |
148 |
149 | Error handlingJava
150 |
151 |
152 | Finalization issuesJava
153 |
154 |
155 | FunctionsC/C++
156 |
157 |
158 | General
159 |
160 |
161 | Groovy
162 |
163 |
164 | Initialization issuesJava
165 |
166 |
167 | Internationalization issuesJava
168 |
169 |
170 | JUnit issuesJava
171 |
172 |
173 | Java
174 |
175 |
176 | Java language level issuesJava
177 |
178 |
179 | Java language level migration aidsJava
180 |
181 |
182 | Language Injection
183 |
184 |
185 | LintAndroid
186 |
187 |
188 | Logging issuesJava
189 |
190 |
191 | Method metricsJava
192 |
193 |
194 | Modularization issuesJava
195 |
196 |
197 | Naming conventionsJava
198 |
199 |
200 | Numeric issuesJava
201 |
202 |
203 | Performance issuesJava
204 |
205 |
206 | PerformanceLintAndroid
207 |
208 |
209 | Potentially confusing code constructsGroovy
210 |
211 |
212 | Probable bugsGroovy
213 |
214 |
215 | Probable bugsJava
216 |
217 |
218 | Properties Files
219 |
220 |
221 | Serialization issuesJava
222 |
223 |
224 | TestNGJava
225 |
226 |
227 | Threading issuesGroovy
228 |
229 |
230 | Threading issuesJava
231 |
232 |
233 | Verbose or redundant code constructsJava
234 |
235 |
236 |
237 |
238 | ThreadWithDefaultRunMethod
239 |
240 |
241 |
242 |
243 |
244 |
245 |
246 |
247 |
248 |
249 |
250 |
251 |
252 |
253 |
254 | 1.8
255 |
256 |
257 |
258 |
259 |
260 |
261 |
262 |
263 |
264 |
265 |
266 |
267 |
268 |
269 |
270 |
271 |
272 |
273 |
274 |
275 |
276 |
277 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## TaskScheduler
2 |
3 | A concise,practical async library for Android project,already was used in million devices
4 | [](https://jitpack.io/#SilenceDut/TaskScheduler)
5 |
6 | ## About
7 |
8 | Android的日常开发中,经常需要处理异步任务,系统提供的Handler和Asynctask实在不方便,一些开源的第三方库由于过于庞大和复杂,例如RxJava,虽然很方便,但RxJava在团队中不容易推广,所以实现了一个简洁、方便、实用的异步库。
9 |
10 | **该异步库已经用于多个日活过百万线上项目中,暂时没发现什么问题,由于线上项目对崩溃率的要求比较严格,可以规避了执行异步任务或UI操作等带来的崩溃**。这个库因为单独抽离出来,实际线上会有些差别,但核心思想是一样的,相对原来的更简洁一些。
11 |
12 | 这个库用起来会很简单,只提供少量的接口,但功能会很强大,其实目的也就是这,就是利用已经相对完善的jdk或者sdk提供的类进行一定的封装,让异步任务更简洁,没必要提供很多看起来功能很全但实际中基本上用不到的接口,达到一看到接口基本就能看懂该怎么用,避免臃肿。
13 |
14 | ## 绑定生命周期
15 |
16 | **支持当通过Handler的方式在Activity、Fragment中执行或延迟执行匿名内部类的runnable时,当onDestroy时,runnable自动被移除,极大简化使用,避免内存泄漏**
17 |
18 | 在Activity里或者Fragment里使用时,在onDestory不需要显示的移除匿名内部类(注意support版本要大于27,官方建议一直用最新的compileSdkVersion和supportVersion)
19 |
20 | ```java
21 |
22 | TaskScheduler.runOnUIThread(this,new Runnable() {
23 | @Override
24 | public void run() {
25 |
26 | }
27 | },5000);
28 | ```
29 | 可指定在特定生命周期移除未执行任务,如onStop
30 |
31 | ```java
32 | TaskScheduler.runOnUIThread(this, Lifecycle.Event.ON_STOP, new Runnable() {
33 | @Override
34 | public void run() {
35 | Log.i("LifeFragment","runTask with life on Stop");
36 | }
37 | },5000);
38 | ```
39 |
40 | 外部传入任意Handler
41 |
42 | ```java
43 | TaskScheduler.runLifecycleRunnable(this,anyHandler,,new Runnable() {
44 | @Override
45 | public void run() {
46 |
47 | }
48 | },5000);
49 |
50 |
51 | ```
52 |
53 |
54 | ## Using
55 |
56 | #### 简单的不需要回调的异步任务
57 |
58 | ```java
59 | TaskScheduler.execute(new Runnable() {
60 | @Override
61 | public void run() {
62 | // DO BACKGROUND WORK
63 | }
64 | });
65 | ```
66 |
67 | #### 异步需要回调的任务
68 |
69 | ```java
70 | Task task = new Task() {
71 |
72 | @Override
73 | public String doInBackground() {
74 | return "background task";
75 | }
76 |
77 | @Override
78 | public void onSuccess(String result) {
79 | //回调到主线程
80 | }
81 |
82 | @Override
83 | public void onFail(Throwable throwable) {
84 | super.onFail(throwable);
85 | //doInBackground 里发生错误时回调
86 | }
87 |
88 | @Override
89 | public void onCancel() {
90 | super.onCancel();
91 | //任务被取消时回调
92 | }
93 | });
94 | ```
95 | doInBackground 和 onSuccess(Object result)时默认必须实现的接口,其他实现都是可选的。除了doInBackground在异步线程中执行,其他的都会回调到主线程执行。
96 |
97 | **执行任务**
98 |
99 | ```java
100 | TaskScheduler.execute(task);
101 |
102 | ```
103 |
104 | **取消任务**
105 |
106 | 将会回调到onCancel(),没法真正取消正在执行的任务,只是结果不在onSuccess里回调, 不一定能让任务停止,和AsyncTask同样道理,可参考之前写的一篇博客[为什么AsyncTask的cancel不能真正的让线程终止运行](http://silencedut.com/2016/07/08/基于最新版本的AsyncTask源码解读及AsyncTask的黑暗面/)
107 |
108 | ```java
109 | TaskScheduler.cancel(task);
110 |
111 | ```
112 |
113 | **超时任务**
114 |
115 | 如果任务超时,将会回调到onCancel()
116 |
117 | ```java
118 | TaskScheduler.executeTimeOutTask(timeOutMillis,task);
119 |
120 | ```
121 |
122 | **周期性任务**
123 |
124 | 如果任务超时,将会回调到onCancel()
125 |
126 | ```java
127 |
128 | 主线程,Io线程可选
129 | TaskScheduler.scheduleUITask( SchedulerTask task);
130 |
131 | 取消任务
132 | TaskScheduler.stopScheduleTask(SchedulerTask task)
133 |
134 | ```
135 |
136 | **其他的一些常用方法**
137 |
138 |
139 | ```java
140 | /**
141 | *获取回调到handlerName线程的handler.一般用于在一个后台线程执行同一种任务,避免线程安全问题。如数据库,文件操作
142 | */
143 | Handler provideHandler(String handlerName)
144 |
145 | /**
146 | * 提供一个公用的异步handler
147 | */
148 | Handler ioHandler()
149 |
150 | /**
151 | *常用的handler的操作
152 | */
153 | runOnUIThread(@NonNull Runnable runnable)
154 |
155 | runOnUIThread(Runnable runnable,long delayed)
156 |
157 | removeUICallback(@NonNull Runnable runnable)
158 |
159 |
160 | ```
161 |
162 | ## Add to library
163 |
164 | **Step1.Add it in your root build.gradle at the end of repositories:**
165 |
166 | ```java
167 | allprojects {
168 | repositories {
169 | ...
170 | maven { url 'https://jitpack.io' }
171 | }
172 | }
173 | ```
174 |
175 |
176 | **Step2. Add the dependency:**
177 |
178 | ```java
179 | dependencies {
180 | compile 'com.github.silencedut:taskscheduler:latest'
181 | }
182 | ```
183 |
184 |
185 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
3 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 28
5 | defaultConfig {
6 | applicationId "com.silencedut.sample"
7 | minSdkVersion 14
8 | targetSdkVersion 26
9 | versionCode 1
10 | versionName "1.0"
11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 |
20 | }
21 |
22 | dependencies {
23 | implementation fileTree(include: ['*.jar'], dir: 'libs')
24 | implementation 'com.android.support:appcompat-v7:28.0.0'
25 | implementation 'com.android.support.constraint:constraint-layout:1.0.2'
26 | // implementation 'com.github.SilenceDut:TaskScheduler:1.0.0'
27 | testImplementation 'junit:junit:4.12'
28 | androidTestImplementation 'com.android.support.test:runner:1.0.1'
29 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
30 | implementation project(':taskscheduler')
31 | }
32 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/silencedut/sample/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.silencedut.sample;
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() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.silencedut.sample", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/app/src/main/java/com/silencedut/sample/LifeFragment.java:
--------------------------------------------------------------------------------
1 | package com.silencedut.sample;
2 |
3 | import android.arch.lifecycle.Lifecycle;
4 | import android.os.Bundle;
5 | import android.support.annotation.NonNull;
6 | import android.support.annotation.Nullable;
7 | import android.support.v4.app.Fragment;
8 | import android.util.Log;
9 | import android.view.LayoutInflater;
10 | import android.view.View;
11 | import android.view.ViewGroup;
12 |
13 | import com.silencedut.taskscheduler.TaskScheduler;
14 |
15 | /**
16 | * @author SilenceDut
17 | * @date 2018/11/26
18 | */
19 | public class LifeFragment extends Fragment {
20 |
21 | @Nullable
22 | @Override
23 | public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
24 | View rootView = inflater.inflate(R.layout.fragment_test,container,false );
25 | rootView.findViewById(R.id.remove_fragment).setOnClickListener(new View.OnClickListener() {
26 | @Override
27 | public void onClick(View v) {
28 | removeThis();
29 | }
30 | });
31 | runTask();
32 | return rootView;
33 | }
34 |
35 | private void removeThis() {
36 | if(getFragmentManager()!=null) {
37 | getFragmentManager().beginTransaction().remove(this).commitAllowingStateLoss();
38 | }
39 | }
40 |
41 | private void runTask() {
42 | TaskScheduler.runOnUIThread(new Runnable() {
43 | @Override
44 | public void run() {
45 |
46 | Log.i("LifeFragment","runTask no life");
47 | }
48 | },5000);
49 |
50 | TaskScheduler.runOnUIThread(this,new Runnable() {
51 | @Override
52 | public void run() {
53 | Log.i("LifeFragment","runTask with life");
54 | }
55 | },5000);
56 | TaskScheduler.runOnUIThread(this, Lifecycle.Event.ON_STOP, new Runnable() {
57 | @Override
58 | public void run() {
59 | Log.i("LifeFragment","runTask with life on Stop");
60 | }
61 | },5000);
62 |
63 | TaskScheduler.runLifecycleRunnable(this, TaskScheduler.ioHandler(), new Runnable() {
64 | @Override
65 | public void run() {
66 | Log.i("LifeFragment","io thread runTask with life");
67 | }
68 | },5000);
69 |
70 |
71 | }
72 |
73 | @Override
74 | public void onStop() {
75 | super.onStop();
76 | }
77 |
78 | @Override
79 | public void onDestroy() {
80 | super.onDestroy();
81 | Log.i("LifeFragment","onDestroy");
82 | }
83 | }
84 |
--------------------------------------------------------------------------------
/app/src/main/java/com/silencedut/sample/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.silencedut.sample;
2 |
3 | import android.os.Bundle;
4 | import android.os.SystemClock;
5 | import android.support.v7.app.AppCompatActivity;
6 | import android.util.Log;
7 | import android.view.View;
8 |
9 | import com.silencedut.taskscheduler.SchedulerTask;
10 | import com.silencedut.taskscheduler.Task;
11 | import com.silencedut.taskscheduler.TaskScheduler;
12 |
13 | public class MainActivity extends AppCompatActivity implements View.OnClickListener{
14 |
15 | private static final String TAG ="MainActivity" ;
16 | private Task mDemoTask;
17 | private long mStartMillis;
18 | private static final String SCHEDULE_TOKEN = "Test_schedule";
19 | private SchedulerTask mSchedulerTask;
20 | private int times = 10;
21 | @Override
22 | protected void onCreate(Bundle savedInstanceState) {
23 | super.onCreate(savedInstanceState);
24 | setContentView(R.layout.activity_main);
25 |
26 | findViewById(R.id.start_task).setOnClickListener(this);
27 | findViewById(R.id.cancel_task).setOnClickListener(this);
28 | findViewById(R.id.timeout_task).setOnClickListener(this);
29 | findViewById(R.id.lift_task).setOnClickListener(this);
30 |
31 | mDemoTask = new Task() {
32 |
33 | @Override
34 | public String doInBackground() {
35 |
36 | mStartMillis = System.currentTimeMillis();
37 | try {
38 | Thread.sleep(8000);
39 | }catch (InterruptedException e) {
40 | Log.i(TAG,""+e);
41 | }
42 | Log.i(TAG,"withResultTask doInBackground, current thread is ? "+Thread.currentThread().getName());
43 | return "休眠"+(System.currentTimeMillis() - mStartMillis)/1000+"秒";
44 | }
45 |
46 | @Override
47 | public void onSuccess(String result) {
48 | Log.i(TAG,"withResultTask onSuccess, current thread is main ? "
49 | +TaskScheduler.isMainThread()+", result : "+result);
50 | }
51 |
52 | @Override
53 | public void onFail(Throwable throwable) {
54 | super.onFail(throwable);
55 | Log.i(TAG,"onFail: 休眠 "+(System.currentTimeMillis() - mStartMillis)/1000+"秒");
56 | }
57 |
58 | @Override
59 | public void onCancel() {
60 | super.onCancel();
61 | Log.i(TAG,"onCancel: 休眠 "+(System.currentTimeMillis() - mStartMillis)/1000+"秒");
62 | }
63 | };
64 |
65 |
66 | mSchedulerTask = new SchedulerTask(1000,false,3000){
67 |
68 | @Override
69 | public void onSchedule() {
70 | if(times--< 0) {
71 | TaskScheduler.stopScheduleTask(this);
72 | }else {
73 | Log.i(TAG," current thread is ? "+Thread.currentThread().getName()+" uptimeMillis "+ SystemClock.uptimeMillis());
74 | }
75 |
76 | }
77 | };
78 |
79 | }
80 |
81 |
82 | private void noResultTask() {
83 | TaskScheduler.execute(new Runnable() {
84 | @Override
85 | public void run() {
86 | try {
87 | Thread.sleep(5000);
88 | } catch (InterruptedException e) {
89 | e.printStackTrace();
90 | }
91 | Log.i(TAG,"noResultTask current thread is main ? "+TaskScheduler.isMainThread());
92 | }
93 | });
94 | }
95 |
96 | private void withResultTask() {
97 | TaskScheduler.execute(mDemoTask);
98 | }
99 |
100 | /**
101 | * 期望结果是休眠3秒,回调到onCancel,代表超时
102 | */
103 | private void timeOutTask() {
104 | TaskScheduler.executeTimeOutTask(3000, mDemoTask);
105 | }
106 |
107 |
108 |
109 | @Override
110 | protected void onDestroy() {
111 | super.onDestroy();
112 | TaskScheduler.cancelTask(mDemoTask);
113 | }
114 |
115 | /**
116 | * Called when a view has been clicked.
117 | *
118 | * @param v The view that was clicked.
119 | */
120 | @Override
121 | public void onClick(View v) {
122 | switch(v.getId()) {
123 | case R.id.start_task:
124 | Log.i(TAG,"startTask");
125 | times=10;
126 | TaskScheduler.scheduleTask(mSchedulerTask);
127 | // noResultTask();
128 | // withResultTask();
129 | break;
130 | case R.id.cancel_task:
131 |
132 | TaskScheduler.stopScheduleTask(mSchedulerTask);
133 | // Log.i(TAG,"cancelTask");
134 | // TaskScheduler.cancelTask(mDemoTask);
135 | break;
136 | case R.id.timeout_task:
137 | Log.i(TAG,"timeOutTask");
138 | timeOutTask();
139 | break;
140 | case R.id.lift_task:
141 | Log.i(TAG,"lifeTask");
142 | getSupportFragmentManager().beginTransaction().replace(R.id.life_fragment_container,new LifeFragment()).commitAllowingStateLoss();
143 | break;
144 | default:break;
145 | }
146 | }
147 | }
148 |
--------------------------------------------------------------------------------
/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/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
11 |
16 |
21 |
26 |
31 |
36 |
41 |
46 |
51 |
56 |
61 |
66 |
71 |
76 |
81 |
86 |
91 |
96 |
101 |
106 |
111 |
116 |
121 |
126 |
131 |
136 |
141 |
146 |
151 |
156 |
161 |
166 |
171 |
172 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
21 |
22 |
33 |
34 |
45 |
46 |
57 |
58 |
63 |
64 |
65 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_test.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
17 |
18 |
27 |
28 |
--------------------------------------------------------------------------------
/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/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SilenceDut/TaskScheduler/381bb1d6e3d8b5bf1fd431f783766279199668d7/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SilenceDut/TaskScheduler/381bb1d6e3d8b5bf1fd431f783766279199668d7/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SilenceDut/TaskScheduler/381bb1d6e3d8b5bf1fd431f783766279199668d7/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SilenceDut/TaskScheduler/381bb1d6e3d8b5bf1fd431f783766279199668d7/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SilenceDut/TaskScheduler/381bb1d6e3d8b5bf1fd431f783766279199668d7/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SilenceDut/TaskScheduler/381bb1d6e3d8b5bf1fd431f783766279199668d7/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SilenceDut/TaskScheduler/381bb1d6e3d8b5bf1fd431f783766279199668d7/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SilenceDut/TaskScheduler/381bb1d6e3d8b5bf1fd431f783766279199668d7/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SilenceDut/TaskScheduler/381bb1d6e3d8b5bf1fd431f783766279199668d7/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SilenceDut/TaskScheduler/381bb1d6e3d8b5bf1fd431f783766279199668d7/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | TaskScheduler
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/test/java/com/silencedut/sample/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.silencedut.sample;
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() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 | apply from : 'version.gradle'
3 | buildscript {
4 |
5 | repositories {
6 | google()
7 | jcenter()
8 | }
9 | dependencies {
10 | classpath 'com.android.tools.build:gradle:3.2.1'
11 | classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0'
12 | classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.70'//kotlin插件
13 | // NOTE: Do not place your application dependencies here; they belong
14 | // in the individual module build.gradle files
15 | }
16 | }
17 |
18 | allprojects {
19 | repositories {
20 | google()
21 | jcenter()
22 | maven { url 'https://jitpack.io' }
23 | }
24 | }
25 |
26 | task clean(type: Delete) {
27 | delete rootProject.buildDir
28 | }
29 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SilenceDut/TaskScheduler/381bb1d6e3d8b5bf1fd431f783766279199668d7/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Nov 26 10:04:05 CST 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.6-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/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 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
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 Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':taskscheduler'
2 |
--------------------------------------------------------------------------------
/taskscheduler/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/taskscheduler/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'com.github.dcendents.android-maven'
3 | group='com.github.silencedut'
4 |
5 | android {
6 | compileSdkVersion _compileSdkVersion
7 |
8 | defaultConfig {
9 | minSdkVersion _minSdkVersion
10 | targetSdkVersion _targetSdkVersion
11 | versionCode _versionCode
12 | versionName "1.0"
13 |
14 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
15 |
16 | }
17 | buildTypes {
18 | release {
19 | minifyEnabled false
20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
21 | }
22 | }
23 | // 指定编码
24 | tasks.withType(JavaCompile) {
25 | options.encoding = "UTF-8"
26 | }
27 |
28 | // 打包源码
29 | task sourcesJar(type: Jar) {
30 | from android.sourceSets.main.java.srcDirs
31 | classifier = 'sources'
32 | }
33 |
34 | artifacts {
35 | archives sourcesJar
36 | }
37 |
38 | }
39 |
40 | dependencies {
41 | implementation fileTree(include: ['*.jar'], dir: 'libs')
42 | implementation "com.android.support:appcompat-v7:$_supportVersion"
43 |
44 | implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
45 | testImplementation 'junit:junit:4.12'
46 | }
47 |
--------------------------------------------------------------------------------
/taskscheduler/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/taskscheduler/src/androidTest/java/com/silencedut/taskscheduler/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.silencedut.taskscheduler;
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() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.silencedut.taskscheduler.test", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/taskscheduler/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
--------------------------------------------------------------------------------
/taskscheduler/src/main/java/com/silencedut/taskscheduler/ILog.java:
--------------------------------------------------------------------------------
1 | package com.silencedut.taskscheduler;
2 |
3 | /**
4 | * @author SilenceDut
5 | * @date 2019/3/25
6 | * 外部提供日志输出接口
7 | */
8 | public interface ILog {
9 |
10 | void info(String info);
11 | void error(String error);
12 | }
13 |
--------------------------------------------------------------------------------
/taskscheduler/src/main/java/com/silencedut/taskscheduler/LifecycleRunnableDelegate.java:
--------------------------------------------------------------------------------
1 | package com.silencedut.taskscheduler;
2 |
3 | import android.arch.lifecycle.GenericLifecycleObserver;
4 | import android.arch.lifecycle.Lifecycle;
5 | import android.arch.lifecycle.LifecycleOwner;
6 | import android.os.Handler;
7 |
8 | /**
9 | * @author SilenceDut
10 | * @date 2018/11/26
11 | */
12 | public class LifecycleRunnableDelegate implements Runnable {
13 | private Runnable mOriginRunnable;
14 | private LifecycleOwner mLifecycleOwner;
15 | private GenericLifecycleObserver mLifecycleObserver;
16 |
17 |
18 | LifecycleRunnableDelegate(LifecycleOwner lifecycleOwner, final Handler handler, final Lifecycle.Event targetEvent, final Runnable originRunnable) {
19 | if(originRunnable == null || lifecycleOwner == null) {
20 | return;
21 | }
22 | this.mLifecycleOwner = lifecycleOwner;
23 | this.mOriginRunnable = originRunnable;
24 | mLifecycleObserver = new GenericLifecycleObserver() {
25 | @Override
26 | public void onStateChanged(LifecycleOwner source, Lifecycle.Event event) {
27 |
28 | if(event == targetEvent) {
29 | if(mLifecycleOwner!=null ) {
30 | mLifecycleOwner.getLifecycle().removeObserver(this);
31 | }
32 | handler.removeCallbacks(LifecycleRunnableDelegate.this);
33 | }
34 | }
35 | };
36 | if(TaskScheduler.isMainThread()) {
37 | mLifecycleOwner.getLifecycle().addObserver(mLifecycleObserver);
38 | }else {
39 | TaskScheduler.runOnUIThread(new Runnable() {
40 | @Override
41 | public void run() {
42 | mLifecycleOwner.getLifecycle().addObserver(mLifecycleObserver);
43 | }
44 | });
45 | }
46 |
47 | }
48 |
49 |
50 | @Override
51 | public void run() {
52 | if(mOriginRunnable!=null && mLifecycleOwner!=null) {
53 | mOriginRunnable.run();
54 | mLifecycleOwner.getLifecycle().removeObserver(mLifecycleObserver);
55 | }
56 |
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/taskscheduler/src/main/java/com/silencedut/taskscheduler/SafeSchedulerHandler.java:
--------------------------------------------------------------------------------
1 | package com.silencedut.taskscheduler;
2 |
3 | import android.os.Handler;
4 | import android.os.Looper;
5 | import android.os.Message;
6 | import android.util.Log;
7 |
8 |
9 | /**
10 | *
11 | * @author SilenceDut
12 | * @date 17/04/18
13 | *
14 | * a safe Handler avoid crash
15 | */
16 | class SafeSchedulerHandler extends Handler{
17 |
18 | private static final String TAG = "SafeSchedulerHandler";
19 | SafeSchedulerHandler(Looper looper) {
20 | super(looper);
21 | }
22 |
23 | SafeSchedulerHandler() {
24 | super();
25 | }
26 |
27 |
28 | @Override
29 | public void dispatchMessage(Message msg) {
30 | try {
31 | super.dispatchMessage(msg);
32 | } catch (Exception e) {
33 | Log.d(TAG, "dispatchMessage Exception " + msg + " , " + e);
34 | } catch (Error error) {
35 | Log.d(TAG, "dispatchMessage error " + msg + " , " + error);
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/taskscheduler/src/main/java/com/silencedut/taskscheduler/SchedulerTask.java:
--------------------------------------------------------------------------------
1 | package com.silencedut.taskscheduler;
2 |
3 | import java.util.concurrent.atomic.AtomicBoolean;
4 |
5 | /**
6 | * @author SilenceDut
7 | * @date 2018/6/22
8 | */
9 | public abstract class SchedulerTask implements Runnable {
10 |
11 |
12 | long startDelayMillisecond;
13 | long periodMillisecond;
14 | boolean mainThread = true;
15 | AtomicBoolean canceled = new AtomicBoolean(false);
16 |
17 | protected SchedulerTask(long periodMillisecond) {
18 | this.periodMillisecond = periodMillisecond;
19 | }
20 |
21 | protected SchedulerTask(long periodMillisecond,boolean mainThread) {
22 | this.periodMillisecond = periodMillisecond;
23 | this.mainThread = mainThread;
24 | }
25 |
26 | protected SchedulerTask(long periodMillisecond,boolean mainThread,long startDelayMillisecond) {
27 | this.periodMillisecond = periodMillisecond;
28 | this.mainThread = mainThread;
29 | this.startDelayMillisecond = startDelayMillisecond;
30 | }
31 |
32 | public abstract void onSchedule();
33 |
34 | @Override
35 | public void run() {
36 | if(!canceled.get()) {
37 | onSchedule();
38 | }
39 | }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/taskscheduler/src/main/java/com/silencedut/taskscheduler/Task.java:
--------------------------------------------------------------------------------
1 | package com.silencedut.taskscheduler;
2 |
3 | import android.util.Log;
4 |
5 | import java.util.concurrent.atomic.AtomicBoolean;
6 | import java.util.concurrent.atomic.AtomicReference;
7 |
8 | /**
9 | *
10 | * @author SilenceDut
11 | * @date 2017/12/25
12 | */
13 |
14 | public abstract class Task implements Runnable {
15 |
16 | private static final String TAG = "Task";
17 | private AtomicBoolean mCanceledAtomic = new AtomicBoolean(false);
18 | private AtomicReference mTaskThread = new AtomicReference<>();
19 |
20 |
21 | /**
22 | * 异步线程处理任务,在非主线程执行
23 | * @return 处理后的结果
24 | * @throws InterruptedException 获取InterruptedException异常,来判断任务是否被取消
25 | */
26 | public abstract R doInBackground() throws InterruptedException;
27 |
28 | /**
29 | * 异步线程处理后返回的结果,在主线程执行
30 | * @param result 结果
31 | */
32 | public abstract void onSuccess(R result);
33 |
34 | /**
35 | * 异步线程处理出现异常的回调,按需处理,未置成抽象,主线程执行
36 | * @param throwable 异常
37 | */
38 | public void onFail(Throwable throwable){
39 |
40 | }
41 |
42 | /**
43 | * 任务被取消的回调,主线程执行
44 | *
45 | */
46 | public void onCancel(){
47 |
48 | }
49 |
50 | /**
51 | * 将任务标记为取消,没法真正取消正在执行的任务,只是结果不在onSuccess里回调
52 | * cancel 不一定能让任务停止,和AsyncTask同样道理,可参考
53 | * {#link http://silencedut.com/2016/07/08/%E5%9F%BA%E4%BA%8E%E6%9C%80%E6%96%B0%E7%89%88%E6%9C%AC%E7%9A%84AsyncTask%E6%BA%90%E7%A0%81%E8%A7%A3%E8%AF%BB%E5%8F%8AAsyncTask%E7%9A%84%E9%BB%91%E6%9A%97%E9%9D%A2/}
54 | **/
55 |
56 | void cancel() {
57 |
58 | this.mCanceledAtomic.set(true);
59 |
60 | Thread t = mTaskThread.get();
61 | if(t!=null) {
62 | Log.d(TAG,"Task cancel: "+t.getName());
63 | t.interrupt();
64 | }
65 |
66 | TaskScheduler.runOnUIThread(new Runnable() {
67 | @Override
68 | public void run() {
69 | onCancel();
70 | }
71 | });
72 | }
73 |
74 | /**
75 | * 任务是已取消
76 | * @return 任务是否已被取消
77 | */
78 | public boolean isCanceled() {
79 |
80 | return mCanceledAtomic.get();
81 | }
82 |
83 | @Override
84 | public void run() {
85 | try {
86 |
87 | Log.d(TAG,"Task : "+Thread.currentThread().getName());
88 | mTaskThread.compareAndSet(null,Thread.currentThread());
89 |
90 | mCanceledAtomic.set(false);
91 |
92 | final R result = doInBackground();
93 |
94 | TaskScheduler.runOnUIThread(new Runnable() {
95 | @Override
96 | public void run() {
97 | if(!isCanceled()){
98 | onSuccess(result);
99 | }
100 | }
101 | });
102 | } catch (final Throwable throwable) {
103 |
104 | Log.e(TAG,"handle background Task error " +throwable);
105 | TaskScheduler.runOnUIThread(new Runnable() {
106 | @Override
107 | public void run() {
108 | if(!isCanceled()){
109 | onFail(throwable);
110 | }
111 | }
112 | });
113 | }
114 | }
115 |
116 | }
117 |
--------------------------------------------------------------------------------
/taskscheduler/src/main/java/com/silencedut/taskscheduler/TaskScheduler.java:
--------------------------------------------------------------------------------
1 | package com.silencedut.taskscheduler;
2 |
3 | import android.arch.lifecycle.Lifecycle;
4 | import android.arch.lifecycle.LifecycleOwner;
5 | import android.os.Handler;
6 | import android.os.HandlerThread;
7 | import android.os.Looper;
8 | import android.os.Process;
9 | import android.util.Log;
10 |
11 | import java.util.concurrent.BlockingQueue;
12 | import java.util.concurrent.ExecutionException;
13 | import java.util.concurrent.ExecutorService;
14 | import java.util.concurrent.Future;
15 | import java.util.concurrent.LinkedBlockingQueue;
16 | import java.util.concurrent.ScheduledExecutorService;
17 | import java.util.concurrent.ScheduledThreadPoolExecutor;
18 | import java.util.concurrent.SynchronousQueue;
19 | import java.util.concurrent.ThreadPoolExecutor;
20 | import java.util.concurrent.TimeUnit;
21 | import java.util.concurrent.TimeoutException;
22 |
23 | /**
24 | *
25 | * @author SilenceDut
26 | * @date 17/04/18
27 | *
28 | */
29 | public class TaskScheduler {
30 |
31 |
32 | private volatile static TaskScheduler sTaskScheduler;
33 | private static final String TAG = "TaskScheduler";
34 |
35 | private ExecutorService mParallelExecutor ;
36 | private ExecutorService mTimeOutExecutor ;
37 | private Handler mIOHandler;
38 | private SafeSchedulerHandler mMainHandler = new SafeSchedulerHandler(Looper.getMainLooper());
39 | private ILog mILog = new ILog() {
40 | @Override
41 | public void info(String info) {
42 | Log.i(TAG,info);
43 | }
44 |
45 | @Override
46 | public void error(String error) {
47 | Log.e(TAG,error);
48 | }
49 | };
50 |
51 | private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
52 | /**
53 | * copy from AsyncTask
54 | * We want at least 2 threads and at most 4 threads in the core pool,
55 | * preferring to have 1 less than the CPU count to avoid saturating
56 | * the CPU with background work
57 | */
58 | private static final int CORE_POOL_SIZE = Math.max(2, Math.min(CPU_COUNT - 1, 4));
59 | private static final int MAXIMUM_POOL_SIZE = CORE_POOL_SIZE * 2 + 1;
60 | private static final long KEEP_ALIVE = 60L;
61 | private static final BlockingQueue POOL_WORK_QUEUE =
62 | new LinkedBlockingQueue<>(128);
63 |
64 | private static TaskScheduler getInstance() {
65 | if(sTaskScheduler==null) {
66 | synchronized (TaskScheduler.class) {
67 | if(sTaskScheduler==null) {
68 | sTaskScheduler = new TaskScheduler();
69 | }
70 | }
71 | }
72 | return sTaskScheduler;
73 | }
74 |
75 | private TaskScheduler() {
76 |
77 |
78 | mParallelExecutor = new ThreadPoolExecutor(CORE_POOL_SIZE,MAXIMUM_POOL_SIZE,
79 | KEEP_ALIVE,TimeUnit.SECONDS,POOL_WORK_QUEUE,ThreadFactory.TASKSCHEDULER_FACTORY);
80 |
81 |
82 | /*
83 | 没有核心线程的线程池要用 SynchronousQueue 而不是LinkedBlockingQueue,SynchronousQueue是一个只有一个任务的队列,
84 | 这样每次就会创建非核心线程执行任务,因为线程池任务放入队列的优先级比创建非核心线程优先级大.
85 | */
86 | mTimeOutExecutor = new ThreadPoolExecutor(0,MAXIMUM_POOL_SIZE,
87 | KEEP_ALIVE,TimeUnit.SECONDS,new SynchronousQueue(),ThreadFactory.TIME_OUT_THREAD_FACTORY);
88 |
89 | mIOHandler = provideHandler("IoHandler");
90 |
91 | }
92 |
93 | public static void addLogImpl(ILog taskLog) {
94 | if(taskLog != null) {
95 | getInstance().mILog = taskLog;
96 | }
97 | }
98 |
99 | public static ExecutorService executorService() {
100 | return getInstance().mParallelExecutor;
101 | }
102 |
103 | /**
104 | * 获取回调到handlerName线程的handler.一般用于在一个后台线程执行同一种任务,避免线程安全问题。如数据库,文件操作
105 | * @param handlerName 线程名
106 | * @return 异步任务handler
107 | */
108 | public static Handler provideHandler(String handlerName) {
109 |
110 |
111 | HandlerThread handlerThread = new HandlerThread(handlerName,Process.THREAD_PRIORITY_BACKGROUND);
112 | handlerThread.start();
113 |
114 | return new SafeSchedulerHandler(handlerThread.getLooper());
115 | }
116 |
117 | /**
118 | * 提供一个公用的异步handler
119 | */
120 | public static Handler ioHandler() {
121 | return getInstance().mIOHandler;
122 | }
123 |
124 | /**
125 | * 主线程周期性执行任务,默认立刻执行,之后间隔period执行,不需要时注意取消,每次执行时如果有相同的任务,默认会先取消
126 | * @param task 执行的任务
127 | */
128 | public static void scheduleTask(final SchedulerTask task) {
129 |
130 | task.canceled.compareAndSet(true,false);
131 |
132 | final ScheduledExecutorService service = new ScheduledThreadPoolExecutor(1,ThreadFactory.SCHEDULER_THREAD_FACTORY);
133 |
134 | service.scheduleAtFixedRate(new Runnable() {
135 | @Override
136 | public void run() {
137 | if(task.canceled.get()) {
138 | service.shutdownNow();
139 | }else {
140 | if(task.mainThread) {
141 | runOnUIThread(task);
142 | }else {
143 | task.run();
144 | }
145 | }
146 | }
147 | }, task.startDelayMillisecond, task.periodMillisecond, TimeUnit.MILLISECONDS);
148 | }
149 |
150 | /**
151 | * 取消周期性任务
152 | * @param schedulerTask 任务对象
153 | */
154 | public static void stopScheduleTask(final SchedulerTask schedulerTask) {
155 | schedulerTask.canceled.compareAndSet(false,true);
156 | }
157 |
158 |
159 | /**
160 | *执行一个后台任务,无回调
161 | * **/
162 | public static void execute(Runnable task) {
163 | getInstance().mILog.info("execute Runnable"+task.toString());
164 | getInstance().mParallelExecutor.execute(task);
165 | }
166 |
167 | /**
168 | *执行一个后台任务,如果不需回调
169 | * @see #execute(Runnable)
170 | **/
171 | public static void execute(Task task) {
172 | getInstance().mILog.info("execute task"+task.toString());
173 | getInstance().mParallelExecutor.execute(task);
174 | }
175 |
176 | /**
177 | * 取消一个任务
178 | * @param task 被取消的任务
179 | */
180 | public static void cancelTask(Task task) {
181 | if(task!=null) {
182 | task.cancel();
183 | }
184 | }
185 |
186 | /**
187 | * 使用一个单独的线程池来执行超时任务,避免引起他线程不够用导致超时
188 | * @param timeOutMillis 超时时间,单位毫秒
189 | ** 通过实现error(Exception) 判断是否为 TimeoutException 来判断是否超时,
190 | * 不能100%保证实际的超时时间就是timeOutMillis,但一般没必要那么精确
191 | * */
192 | public static void executeTimeOutTask(final long timeOutMillis, final Task timeOutTask) {
193 | final Future future =getInstance().mTimeOutExecutor.submit(timeOutTask);
194 |
195 | getInstance().mTimeOutExecutor.execute(new Runnable() {
196 | @Override
197 | public void run() {
198 | try {
199 | future.get(timeOutMillis,TimeUnit.MILLISECONDS);
200 | } catch (InterruptedException | ExecutionException | TimeoutException e ) {
201 | runOnUIThread(new Runnable() {
202 | @Override
203 | public void run() {
204 | if(!timeOutTask.isCanceled()) {
205 | timeOutTask.cancel();
206 | }
207 | }
208 | });
209 | }
210 |
211 | }
212 | });
213 | }
214 |
215 |
216 | public static Handler mainHandler() {
217 | return getInstance().mMainHandler;
218 | }
219 |
220 | /**
221 | *use {@link #mainHandler}
222 | */
223 | @Deprecated
224 | public static Handler getMainHandler() {
225 | return getInstance().mMainHandler;
226 | }
227 |
228 | public static void runOnUIThread(Runnable runnable) {
229 |
230 | getInstance().mMainHandler.post(runnable);
231 | }
232 |
233 | /**
234 | * 执行有生命周期的任务
235 | */
236 | public static Runnable runOnUIThread(LifecycleOwner lifecycleOwner,Runnable runnable) {
237 | LifecycleRunnableDelegate lifecycleRunnableDelegate = new LifecycleRunnableDelegate(lifecycleOwner,getInstance().mMainHandler,Lifecycle.Event.ON_DESTROY,runnable);
238 | getInstance().mMainHandler.post(lifecycleRunnableDelegate);
239 | return lifecycleRunnableDelegate;
240 | }
241 |
242 |
243 | /**
244 | * 执行有生命周期的任务,指定Lifecycle.Event
245 | */
246 | public static Runnable runOnUIThread(LifecycleOwner lifecycleOwner,Lifecycle.Event targetEvent,Runnable runnable) {
247 | LifecycleRunnableDelegate lifecycleRunnableDelegate = new LifecycleRunnableDelegate(lifecycleOwner,getInstance().mMainHandler,targetEvent,runnable);
248 | getInstance().mMainHandler.post(lifecycleRunnableDelegate);
249 | return lifecycleRunnableDelegate;
250 | }
251 |
252 | public static void runOnUIThread(Runnable runnable,long delayed) {
253 | getInstance().mMainHandler.postDelayed(runnable,delayed);
254 | }
255 |
256 | public static Runnable runOnUIThread(LifecycleOwner lifecycleOwner,Runnable runnable,long delayed) {
257 | LifecycleRunnableDelegate lifecycleRunnableDelegate = new LifecycleRunnableDelegate(lifecycleOwner,getInstance().mMainHandler,Lifecycle.Event.ON_DESTROY,runnable);
258 | getInstance().mMainHandler.postDelayed(lifecycleRunnableDelegate,delayed);
259 | return lifecycleRunnableDelegate;
260 | }
261 |
262 | public static Runnable runOnUIThread(LifecycleOwner lifecycleOwner,Lifecycle.Event targetEvent,Runnable runnable,long delayed) {
263 | LifecycleRunnableDelegate lifecycleRunnableDelegate = new LifecycleRunnableDelegate(lifecycleOwner,getInstance().mMainHandler,targetEvent,runnable);
264 | getInstance().mMainHandler.postDelayed(lifecycleRunnableDelegate,delayed);
265 | return lifecycleRunnableDelegate;
266 | }
267 |
268 | /**
269 | * 外部提供执行任务的Handler
270 | */
271 |
272 | public static Runnable runLifecycleRunnable(LifecycleOwner lifecycleOwner,Handler anyThreadHandler,Runnable runnable) {
273 | LifecycleRunnableDelegate lifecycleRunnableDelegate = new LifecycleRunnableDelegate(lifecycleOwner,anyThreadHandler,Lifecycle.Event.ON_DESTROY,runnable);
274 | anyThreadHandler.post(lifecycleRunnableDelegate);
275 | return lifecycleRunnableDelegate;
276 | }
277 |
278 |
279 | public static Runnable runLifecycleRunnable(LifecycleOwner lifecycleOwner,Handler anyThreadHandler,Runnable runnable,long delayed) {
280 | LifecycleRunnableDelegate lifecycleRunnableDelegate = new LifecycleRunnableDelegate(lifecycleOwner,anyThreadHandler,Lifecycle.Event.ON_DESTROY,runnable);
281 | anyThreadHandler.postDelayed(lifecycleRunnableDelegate,delayed);
282 | return lifecycleRunnableDelegate;
283 | }
284 |
285 | /**
286 | * 外部提供执行任务的Handler,指定移除的Lifecycle.Event
287 | */
288 |
289 | public static Runnable runLifecycleRunnable(LifecycleOwner lifecycleOwner,Handler anyThreadHandler,Lifecycle.Event targetEvent,Runnable runnable,long delayed) {
290 | LifecycleRunnableDelegate lifecycleRunnableDelegate = new LifecycleRunnableDelegate(lifecycleOwner,anyThreadHandler,targetEvent,runnable);
291 | anyThreadHandler.postDelayed(lifecycleRunnableDelegate,delayed);
292 | return lifecycleRunnableDelegate;
293 | }
294 |
295 |
296 | public static void removeUICallback(Runnable runnable) {
297 | mainHandler().removeCallbacks(runnable);
298 | }
299 |
300 | public static boolean isMainThread() {
301 | return Thread.currentThread()== getInstance().mMainHandler.getLooper().getThread();
302 | }
303 |
304 |
305 | }
306 |
--------------------------------------------------------------------------------
/taskscheduler/src/main/java/com/silencedut/taskscheduler/ThreadFactory.java:
--------------------------------------------------------------------------------
1 | package com.silencedut.taskscheduler;
2 |
3 | import android.os.Process;
4 |
5 | import java.util.concurrent.atomic.AtomicInteger;
6 |
7 | /**
8 | * @author SilenceDut
9 | * @date 2018/7/5
10 | */
11 | class ThreadFactory {
12 |
13 | static final class BackgroundRunnable implements Runnable {
14 | private Runnable runnable;
15 | BackgroundRunnable(Runnable runnable) {
16 | this.runnable = runnable;
17 | }
18 | @Override
19 | public void run() {
20 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
21 | runnable.run();
22 | }
23 | }
24 |
25 | static final java.util.concurrent.ThreadFactory TASKSCHEDULER_FACTORY = new java.util.concurrent.ThreadFactory() {
26 | private final AtomicInteger mCount = new AtomicInteger(1);
27 |
28 | @Override
29 | public Thread newThread(Runnable r) {
30 | return new Thread(new BackgroundRunnable(r), "TaskScheduler #" + mCount.getAndIncrement());
31 | }
32 | };
33 |
34 | static final java.util.concurrent.ThreadFactory TIME_OUT_THREAD_FACTORY = new java.util.concurrent.ThreadFactory() {
35 | private final AtomicInteger mCount = new AtomicInteger(1);
36 |
37 | @Override
38 | public Thread newThread(Runnable r) {
39 | return new Thread(new BackgroundRunnable(r), "TaskScheduler timeoutThread #" + mCount.getAndIncrement());
40 | }
41 | };
42 |
43 | static final java.util.concurrent.ThreadFactory SCHEDULER_THREAD_FACTORY = new java.util.concurrent.ThreadFactory() {
44 | private final AtomicInteger mCount = new AtomicInteger(1);
45 |
46 | @Override
47 | public Thread newThread(Runnable r) {
48 | return new Thread(new BackgroundRunnable(r), "TaskScheduler scheduler #" + mCount.getAndIncrement());
49 | }
50 | };
51 | }
52 |
--------------------------------------------------------------------------------
/taskscheduler/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | TaskScheduler
3 |
4 |
--------------------------------------------------------------------------------
/taskscheduler/src/test/java/com/silencedut/taskscheduler/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.silencedut.taskscheduler;
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() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/version.gradle:
--------------------------------------------------------------------------------
1 | //lib version
2 | buildscript {
3 | ext {
4 | //android
5 | _compileSdkVersion = 28
6 | _targetSdkVersion = 27
7 | _supportVersion = '28.0.0'
8 | _minSdkVersion = 14
9 | _versionCode = System.getenv("BUILD_NUMBER") as Integer ?: 1
10 | lifecycle_extensions = '1.1.1'
11 | kotlin_version = '1.2.71'
12 |
13 | }
14 | }
15 |
--------------------------------------------------------------------------------