├── .gitignore ├── .idea ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── dictionaries │ └── Zero.xml ├── gradle.xml ├── markdown-navigator │ └── profiles_settings.xml ├── misc.xml ├── modules.xml └── runConfigurations.xml ├── README-CHN.md ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── gavinrowe │ │ └── lgw │ │ └── simpletimertaskhandler │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── gavinrowe │ │ │ └── lgw │ │ │ └── simpletimertaskhandler │ │ │ └── MainActivity.java │ └── res │ │ ├── layout │ │ └── activity_main.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 │ └── gavinrowe │ └── lgw │ └── simpletimertaskhandler │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── library ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── gavinrowe │ │ └── lgw │ │ └── library │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── gavinrowe │ │ │ └── lgw │ │ │ └── library │ │ │ ├── SimpleTimerTask.java │ │ │ ├── SimpleTimerTaskHandler.java │ │ │ └── TimeUtils.java │ └── res │ │ └── values │ │ └── strings.xml │ └── test │ └── java │ └── com │ └── gavinrowe │ └── lgw │ └── library │ └── ExampleUnitTest.java └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/dictionaries/Zero.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | guowen 5 | 6 | 7 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 18 | 19 | -------------------------------------------------------------------------------- /.idea/markdown-navigator/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | 47 | 48 | 49 | 50 | 1.8 51 | 52 | 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /README-CHN.md: -------------------------------------------------------------------------------- 1 | # SimpleTimerTaskHandler 2 | - 一个基于android.os.Handler的简单定时任务处理器 - 你可以决定什么时候执行任务. 3 | - 作者: Luo Guowen 4 | - 邮箱: luoguowen123@qq.com 5 | 6 | * ### 设置 7 | 8 | **Step 1. 添加JitPack到你的Project的build.gradle:** 9 | 10 | ```groovy 11 | allprojects { 12 | repositories { 13 | ... 14 | maven { url 'https://jitpack.io' } 15 | } 16 | } 17 | ``` 18 | 19 | ​ 20 | 21 | **Step 2. 添加依赖:** 22 | 23 | ```groovy 24 | dependencies { 25 | compile 'com.github.lgw666:SimpleTimerTaskHandler:v1.0' 26 | } 27 | ``` 28 | 29 | ​ 30 | 31 | 32 | 33 | * ### 使用方法 34 | 35 | **Step 1. 获取到SimpleTimerTaskHandler的对象** 36 | 37 | `SimpleTimerTaskHandler handler = SimpleTimerTaskHandler.getInstance();` 38 | 39 | ​ 40 | 41 | **Step 2. 创建你的任务** 42 | 43 | ```Java 44 | SimpleTimerTask task = new SimpleTimerTask() { 45 | @Override 46 | public void run() { 47 | // Do what you want 48 | } 49 | }; 50 | ``` 51 | 52 | ​ or 53 | 54 | ```Java 55 | SimpleTimerTask loopTask = new SimpleTimerTask(loop interval) { 56 | @Override 57 | public void run() { 58 | // Do what you want 59 | } 60 | }; 61 | ``` 62 | 63 | ​ *温馨提示: 一旦使用了第二个构造,任务将会变成循环任务.* 64 | 65 | ​ 66 | 67 | **Step 3. 用SimpleTimerTaskHandler来执行你的任务** 68 | 69 | 执行即时任务 70 | 71 | ```Java 72 | void sendTask(int taskNum, SimpleTimerTask task); 73 | ``` 74 | 75 | 执行延时任务。 76 | 77 | ```java 78 | void sendTaskDelayed(int taskNum, SimpleTimerTask task, long delayMillis); 79 | ``` 80 | 81 | 执行定时任务, 精确到小时。 82 | 83 | ```java 84 | void sendTimerTask(int taskNum, SimpleTimerTask task, int hour); 85 | ``` 86 | 87 | 执行定时任务, 精确到分。 88 | 89 | ```java 90 | void sendTimerTask(int taskNum, SimpleTimerTask task, int hour, int minute); 91 | ``` 92 | 93 | 执行定时任务, 精确到秒。 94 | ```java 95 | void sendTimerTask(int taskNum, SimpleTimerTask task, int hour, int minute, int second); 96 | ``` 97 | 98 | ​ 99 | 100 | * ### 参数: 101 | 102 | 任务编号, eg: 0, 1, 2... 103 | 104 | ```Java 105 | int taskNum; 106 | ``` 107 | 108 | 需要执行的任务。 109 | 110 | ```java 111 | SimpleTimerTask task; 112 | ``` 113 | 114 | 执行任务的某一小时时刻, 基于当天. 115 | 116 | ```java 117 | int hour 118 | ``` 119 | 执行任务的某一小时内的分钟时刻, 基于当天. 120 | 121 | ```java 122 | int minute 123 | ``` 124 | 125 | 执行任务的某一小时内某一分钟内的秒数时刻, 基于当天. 126 | 127 | ```java 128 | int second 129 | ``` 130 | 131 | *例: 如果你想在 13:50:30 时执行一个任务, 可以像下面一样调用* 132 | 133 | ```java 134 | handler.void sendTimerTask(task num, task, 13, 50, 30); 135 | ``` 136 | 137 | * ### SimpleTimerTask: 138 | 139 | SimpleTimerTaskHandler 和 SimpleTimerTask 必须搭配使用. 140 | 141 | ​ 142 | 143 | **公共构造** 144 | 145 | 任务的默认构造。 146 | 147 | ```Java 148 | SimpleTimerTask(); 149 | ``` 150 | 151 | 152 | 带参的任务构造将任务类型从默认变为循环并设置循环间隔。 153 | 154 | ```Java 155 | SimpleTimerTask(long loopInterval); 156 | ``` 157 | 158 | 159 | 160 | 161 | 162 | * ### Tips: 163 | 164 | - 当任务数超过一个时,每个任务的任务编号都应不同 165 | - 进行耗时操作时最好启动一个异步线程或者使用AsyncTask, 例: 网络请求. 166 | - 可以在任务里操作UI 167 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SimpleTimerTaskHandler 2 | 3 | ### [中文版]( https://github.com/lgw666/SimpleTimerTaskHandler/blob/master/README-CHN.md) 4 | 5 | - A simple timer task handler based on android.os.Handler - You can decide when to execute tasks. 6 | - Author: Luo Guowen 7 | - Email: luoguowen123@qq.com 8 | 9 | * ### Setup 10 | 11 | **Step 1. Add the JitPack repository to your build file Add it in your root build.gradle at the end of repositories:** 12 | 13 | ```groovy 14 | allprojects { 15 | repositories { 16 | ... 17 | maven { url 'https://jitpack.io' } 18 | } 19 | } 20 | ``` 21 | 22 | ​ 23 | 24 | **Step 2. Add the dependency:** 25 | 26 | ```groovy 27 | dependencies { 28 | compile 'com.github.lgw666:SimpleTimerTaskHandler:v1.0' 29 | } 30 | ``` 31 | 32 | ​ 33 | 34 | 35 | 36 | * ### Usage 37 | 38 | **Step 1. Get the instance of SimpleTimerTaskHandler** 39 | 40 | `SimpleTimerTaskHandler handler = SimpleTimerTaskHandler.getInstance();` 41 | 42 | ​ 43 | 44 | **Step 2. Create your task** 45 | 46 | ```Java 47 | SimpleTimerTask task = new SimpleTimerTask() { 48 | @Override 49 | public void run() { 50 | // Do what you want 51 | } 52 | }; 53 | ``` 54 | 55 | ​ or 56 | 57 | ```Java 58 | SimpleTimerTask loopTask = new SimpleTimerTask(loop interval) { 59 | @Override 60 | public void run() { 61 | // Do what you want 62 | } 63 | }; 64 | ``` 65 | 66 | ​ *Tip: the task will be a loop task once you use the second constructor.* 67 | 68 | ​ 69 | 70 | **Step 3. Execute your task by using SimpleTimerTaskHandler** 71 | 72 | Execute real-time task 73 | 74 | ```Java 75 | void sendTask(int taskNum, SimpleTimerTask task); 76 | ``` 77 | 78 | Execute delay task 79 | 80 | ```java 81 | void sendTaskDelayed(int taskNum, SimpleTimerTask task, long delayMillis); 82 | ``` 83 | 84 | Execute timer task, accurate to hour. 85 | 86 | ```java 87 | void sendTimerTask(int taskNum, SimpleTimerTask task, int hour); 88 | ``` 89 | 90 | Execute timer task, accurate to minute. 91 | 92 | ```java 93 | void sendTimerTask(int taskNum, SimpleTimerTask task, int hour, int minute); 94 | ``` 95 | 96 | Execute timer task, accurate to second. 97 | ```java 98 | void sendTimerTask(int taskNum, SimpleTimerTask task, int hour, int minute, int second); 99 | ``` 100 | 101 | ​ 102 | 103 | * ### Params: 104 | 105 | Task id, eg: 0, 1, 2... 106 | 107 | ```Java 108 | int taskNum; 109 | ``` 110 | 111 | The task you want to execute. 112 | 113 | ```java 114 | SimpleTimerTask task; 115 | ``` 116 | 117 | The hour to execute this task, based on the day. 118 | 119 | ```java 120 | int hour 121 | ``` 122 | The minute in the hour, based on the day. 123 | 124 | ```java 125 | int minute 126 | ``` 127 | 128 | The seconds in the minute, based on the day. 129 | 130 | ```java 131 | int second 132 | ``` 133 | 134 | *e.g.: If you want execute a task at 13:50:30, you can invoke* 135 | 136 | ```java 137 | handler.sendTimerTask(task num, task, 13, 50, 30); 138 | ``` 139 | 140 | * ### SimpleTimerTask: 141 | 142 | Must use SimpleTimerTaskHandler and SimpleTimerTask together. 143 | 144 | ​ 145 | 146 | **Public constructors** 147 | 148 | Default constructor associates this task. 149 | 150 | ```Java 151 | SimpleTimerTask(); 152 | ``` 153 | 154 | 155 | Constructor associates this task change its task type from default to loop, and sets the loop interval. 156 | 157 | ```Java 158 | SimpleTimerTask(long loopInterval); 159 | ``` 160 | 161 | 162 | 163 | 164 | 165 | * ### Tips: 166 | 167 | - The task number of every task should be different if there are more than one tasks. 168 | - You`d better use a work Thread or AsyncTask to do some long time operations, e.g: network request. 169 | - You can do UI operation in the task 170 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "25.0.2" 6 | defaultConfig { 7 | applicationId "com.gavinrowe.lgw.simpletimertaskhandler" 8 | minSdkVersion 19 9 | targetSdkVersion 25 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(include: ['*.jar'], dir: 'libs') 24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 25 | exclude group: 'com.android.support', module: 'support-annotations' 26 | }) 27 | compile 'com.android.support:appcompat-v7:25.3.1' 28 | compile 'com.android.support.constraint:constraint-layout:1.0.2' 29 | testCompile 'junit:junit:4.12' 30 | compile project(':library') 31 | } 32 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/Zero/Library/Android/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/gavinrowe/lgw/simpletimertaskhandler/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.gavinrowe.lgw.simpletimertaskhandler; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.gavinrowe.lgw.simpletimertaskhandler", 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/gavinrowe/lgw/simpletimertaskhandler/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.gavinrowe.lgw.simpletimertaskhandler; 2 | 3 | import android.support.v7.app.AppCompatActivity; 4 | import android.os.Bundle; 5 | import android.util.Log; 6 | 7 | import com.gavinrowe.lgw.library.SimpleTimerTask; 8 | import com.gavinrowe.lgw.library.SimpleTimerTaskHandler; 9 | 10 | public class MainActivity extends AppCompatActivity { 11 | private static final String TAG = "MainActivity"; 12 | 13 | @Override 14 | protected void onCreate(Bundle savedInstanceState) { 15 | super.onCreate(savedInstanceState); 16 | setContentView(R.layout.activity_main); 17 | final SimpleTimerTask task = new SimpleTimerTask() { 18 | @Override 19 | public void run() { 20 | Log.e(TAG, "run: task"); 21 | } 22 | }; 23 | SimpleTimerTask loopTask = new SimpleTimerTask(5000) { 24 | @Override 25 | public void run() { 26 | Log.e(TAG, "run: loop task,loop interval:" + getLoopInterval()); 27 | } 28 | }; 29 | final SimpleTimerTaskHandler handler = SimpleTimerTaskHandler.getInstance(); 30 | 31 | handler.sendTask(0, task); 32 | // handler.sendTaskDelayed(1, task, 1000); 33 | // handler.sendTimerTask(2, task, 17); 34 | // handler.sendTimerTask(3, task, 17, 12); 35 | // handler.sendTimerTask(4, task, 17, 12, 50); 36 | // 37 | handler.sendTask(5, loopTask); 38 | // handler.sendTaskDelayed(6, loopTask, 1000); 39 | // handler.sendTimerTask(7, loopTask, 17); 40 | // handler.sendTimerTask(8, loopTask, 17, 12); 41 | // handler.sendTimerTask(9, loopTask, 17, 12, 50); 42 | 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgw666/SimpleTimerTaskHandler/3111f0e40fcaa8a03b1efb55f6275089ebfa9423/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgw666/SimpleTimerTaskHandler/3111f0e40fcaa8a03b1efb55f6275089ebfa9423/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgw666/SimpleTimerTaskHandler/3111f0e40fcaa8a03b1efb55f6275089ebfa9423/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgw666/SimpleTimerTaskHandler/3111f0e40fcaa8a03b1efb55f6275089ebfa9423/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgw666/SimpleTimerTaskHandler/3111f0e40fcaa8a03b1efb55f6275089ebfa9423/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgw666/SimpleTimerTaskHandler/3111f0e40fcaa8a03b1efb55f6275089ebfa9423/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgw666/SimpleTimerTaskHandler/3111f0e40fcaa8a03b1efb55f6275089ebfa9423/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgw666/SimpleTimerTaskHandler/3111f0e40fcaa8a03b1efb55f6275089ebfa9423/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgw666/SimpleTimerTaskHandler/3111f0e40fcaa8a03b1efb55f6275089ebfa9423/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgw666/SimpleTimerTaskHandler/3111f0e40fcaa8a03b1efb55f6275089ebfa9423/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 | SimpleTimerTaskHandler 3 | Hello SimpleTimerHandler!\nPlease Check Logs! 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/com/gavinrowe/lgw/simpletimertaskhandler/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.gavinrowe.lgw.simpletimertaskhandler; 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 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:2.3.3' 9 | 10 | // NOTE: Do not place your application dependencies here; they belong 11 | // in the individual module build.gradle files 12 | } 13 | } 14 | 15 | allprojects { 16 | repositories { 17 | jcenter() 18 | maven { url 'https://jitpack.io' } 19 | } 20 | } 21 | 22 | task clean(type: Delete) { 23 | delete rootProject.buildDir 24 | } 25 | -------------------------------------------------------------------------------- /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/lgw666/SimpleTimerTaskHandler/3111f0e40fcaa8a03b1efb55f6275089ebfa9423/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sat Jul 08 13:13:35 CST 2017 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-3.3-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 | -------------------------------------------------------------------------------- /library/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /library/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "25.0.2" 6 | 7 | defaultConfig { 8 | minSdkVersion 19 9 | targetSdkVersion 25 10 | versionCode 1 11 | versionName "1.0" 12 | 13 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 14 | 15 | } 16 | buildTypes { 17 | release { 18 | minifyEnabled false 19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 20 | } 21 | } 22 | } 23 | 24 | dependencies { 25 | compile fileTree(dir: 'libs', include: ['*.jar']) 26 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 27 | exclude group: 'com.android.support', module: 'support-annotations' 28 | }) 29 | compile 'com.android.support:appcompat-v7:25.3.1' 30 | testCompile 'junit:junit:4.12' 31 | } 32 | -------------------------------------------------------------------------------- /library/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/Zero/Library/Android/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /library/src/androidTest/java/com/gavinrowe/lgw/library/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.gavinrowe.lgw.library; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.gavinrowe.lgw.library.test", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /library/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /library/src/main/java/com/gavinrowe/lgw/library/SimpleTimerTask.java: -------------------------------------------------------------------------------- 1 | package com.gavinrowe.lgw.library; 2 | 3 | /** 4 | * Author: Luo Guowen 5 | * Email: luoguowen123@qq.com 6 | * Time: 2017/7/8 7 | * 定时任务 8 | */ 9 | 10 | @SuppressWarnings("all") 11 | public abstract class SimpleTimerTask { 12 | /** 13 | * 标志 14 | * 判断是否为循环任务 15 | */ 16 | private boolean mIsLoop; 17 | 18 | /** 19 | * 循环间隔 20 | * 单位:毫秒 21 | */ 22 | private long mLoopInterval; 23 | 24 | /** 25 | * 普通任务构造 26 | * 默认循环标志 mIsLoop = false 27 | * 默认循环间隔 mLoopInterval = 0 28 | */ 29 | public SimpleTimerTask() { 30 | } 31 | 32 | /** 33 | * 循环任务构造 34 | * 此构造执行时会设置循环标志 mIsLoop = true; 35 | * 36 | * @param loopInterval 循环间隔 37 | */ 38 | public SimpleTimerTask(long loopInterval) { 39 | mIsLoop = true; 40 | mLoopInterval = Math.abs(loopInterval); 41 | } 42 | 43 | /** 44 | * 将要执行的业务 45 | */ 46 | public abstract void run(); 47 | 48 | /** 49 | * @return 是否为循环任务的标志 50 | */ 51 | public boolean isLoop() { 52 | return mIsLoop; 53 | } 54 | 55 | /** 56 | * @return 循环间隔 57 | */ 58 | public long getLoopInterval() { 59 | return mLoopInterval; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /library/src/main/java/com/gavinrowe/lgw/library/SimpleTimerTaskHandler.java: -------------------------------------------------------------------------------- 1 | package com.gavinrowe.lgw.library; 2 | 3 | import android.os.Handler; 4 | import android.os.Looper; 5 | import android.os.Message; 6 | import android.util.Log; 7 | import android.util.SparseArray; 8 | 9 | /** 10 | * Author: Luo Guowen 11 | * Email: luoguowen123@qq.com 12 | * Time: 2017/7/8 13 | * 定时任务处理器 14 | */ 15 | 16 | @SuppressWarnings("all") 17 | public class SimpleTimerTaskHandler extends Handler { 18 | private static final String TAG = "SimpleTimerTaskHandler"; 19 | 20 | // 存放需要执行的任务 21 | private SparseArray mTasks = new SparseArray<>(); 22 | 23 | private static SimpleTimerTaskHandler mSimpleTimerTaskHandler; 24 | 25 | private SimpleTimerTaskHandler() { 26 | super(Looper.getMainLooper()); 27 | } 28 | 29 | public static SimpleTimerTaskHandler getInstance() { 30 | if (null == mSimpleTimerTaskHandler) { 31 | mSimpleTimerTaskHandler = new SimpleTimerTaskHandler(); 32 | } 33 | return mSimpleTimerTaskHandler; 34 | } 35 | 36 | @Override 37 | public void handleMessage(Message msg) { 38 | int pos = msg.what; 39 | Log.i(TAG, "handleMessage: The task is " + pos); 40 | SimpleTimerTask currentTask = mTasks.get(pos); 41 | if (null == currentTask) { 42 | Log.e(TAG, "handleMessage: No task of " + pos + ",please create it first!"); 43 | } else { 44 | currentTask.run(); 45 | if (currentTask.isLoop()) { 46 | long loopInterval = currentTask.getLoopInterval(); 47 | sendEmptyMessageDelayed(pos, loopInterval); 48 | Log.i(TAG, "handleMessage: The task of " + pos + " is a loop task, and it will be executed after " + loopInterval + " millis"); 49 | } else { 50 | mTasks.remove(pos); 51 | } 52 | } 53 | } 54 | 55 | /** 56 | * 即时发送任务 57 | * 58 | * @param taskNum 任务编号 59 | * @param task 任务 60 | */ 61 | public void sendTask(int taskNum, SimpleTimerTask task) { 62 | int msg = saveTask(taskNum, task); 63 | sendEmptyMessage(msg); 64 | Log.i(TAG, "sendTask: 当前任务类型为即时任务,取绝对值后的任务编号为" + msg); 65 | } 66 | 67 | /** 68 | * 延时发送任务 69 | * 70 | * @param taskNum 任务编号 71 | * @param task 任务 72 | * @param delayMillis 延时时间,单位毫秒 73 | */ 74 | public void sendTaskDelayed(int taskNum, SimpleTimerTask task, long delayMillis) { 75 | int msg = saveTask(taskNum, task); 76 | sendEmptyMessageDelayed(msg, delayMillis); 77 | Log.i(TAG, "sendTaskDelayed: 当前任务类型为延时任务,取绝对值后的任务编号为" + msg); 78 | } 79 | 80 | /** 81 | * 保存要执行的任务 82 | */ 83 | private int saveTask(int taskNum, SimpleTimerTask task) { 84 | Log.i(TAG, "saveTask: 保存要执行的任务编号 " + taskNum); 85 | int msg = Math.abs(taskNum); 86 | mTasks.put(msg, task); 87 | Log.i(TAG, "saveTask: 保存后的任务编号" + msg); 88 | return msg; 89 | } 90 | 91 | 92 | /** 93 | * 定时发送任务,精确到小时 94 | * 95 | * @param taskNum 任务编号 96 | * @param task 任务 97 | * @param hour 指定小时 98 | */ 99 | public void sendTimerTask(int taskNum, SimpleTimerTask task, int hour) { 100 | int msg = saveTask(taskNum, task); 101 | sendEmptyMessageDelayed(msg, TimeUtils.getFirstSendTaskDelayedTime(hour)); 102 | Log.i(TAG, "sendTimerTask: 当前任务类型为定时任务(精确到小时),取绝对值后的任务编号为" + msg + ",hour = " + hour); 103 | 104 | } 105 | 106 | /** 107 | * 定时发送任务,精确到分 108 | * 109 | * @param taskNum 任务编号 110 | * @param task 任务 111 | * @param hour 指定小时 112 | * @param minute 指定分钟 113 | */ 114 | public void sendTimerTask(int taskNum, SimpleTimerTask task, int hour, int minute) { 115 | int msg = saveTask(taskNum, task); 116 | sendEmptyMessageDelayed(msg, TimeUtils.getFirstSendTaskDelayedTime(hour, minute)); 117 | Log.i(TAG, "sendTimerTask: 当前任务类型为定时任务(精确到分),取绝对值后的任务编号为" + msg + ",hour = " + hour + ",minute = " + minute); 118 | } 119 | 120 | /** 121 | * 定时发送任务,精确到秒 122 | * 123 | * @param taskNum 任务编号 124 | * @param task 任务 125 | * @param hour 指定小时 126 | * @param minute 指定分钟 127 | * @param second 指定秒数 128 | */ 129 | public void sendTimerTask(int taskNum, SimpleTimerTask task, int hour, int minute, int second) { 130 | int msg = saveTask(taskNum, task); 131 | sendEmptyMessageDelayed(msg, TimeUtils.getFirstSendTaskDelayedTime(hour, minute, second)); 132 | Log.i(TAG, "sendTimerTask: 当前任务类型为定时任务(精确到秒),取绝对值后的任务编号为" + msg + ",hour = " + hour + ",minute = " + minute + ",second = " + second); 133 | } 134 | 135 | } 136 | -------------------------------------------------------------------------------- /library/src/main/java/com/gavinrowe/lgw/library/TimeUtils.java: -------------------------------------------------------------------------------- 1 | package com.gavinrowe.lgw.library; 2 | 3 | import android.util.Log; 4 | 5 | import java.text.SimpleDateFormat; 6 | import java.util.Calendar; 7 | import java.util.Locale; 8 | 9 | /** 10 | * Author: Luo Guowen 11 | * Email: luoguowen123@qq.com 12 | * Time: 2017/7/8 13 | * 工具类,处理时间 14 | */ 15 | @SuppressWarnings("all") 16 | public final class TimeUtils { 17 | private static final String TAG = "TimeUtils"; 18 | /** 19 | * 时间格式器 20 | * 最小单位为秒 21 | * 最大单位为月 22 | */ 23 | private static final SimpleDateFormat mFormatterMonthToSecond = new SimpleDateFormat("HH/dd HH:mm:ss", Locale.getDefault()); 24 | 25 | 26 | /** 27 | * 获取执行任务前的时间到目标时间的时间差 28 | * 公式:targetTime - firstTime 29 | * 30 | * @param firstTime 执行任务前的时间 31 | * @param targetTime 目标时间 32 | * @return 时间差 33 | */ 34 | private static long getDifferBetweenTime(long firstTime, long targetTime) { 35 | long differTime = targetTime - firstTime; 36 | Log.i(TAG, "getDifferBetweenTime: 当前时间到目标时间的时间差:" + differTime); 37 | return differTime; 38 | } 39 | 40 | /** 41 | * 获取第一次执行任务的延迟时间,精确到小时 42 | * 43 | * @param hour 选择的小时 44 | * @return 第一次执行任务的延迟时间 45 | */ 46 | public static long getFirstSendTaskDelayedTime(int hour) { 47 | long currentTime = System.currentTimeMillis(); 48 | long firstSendTaskTime = getDifferBetweenTime(currentTime, getTodayTargetTime(hour)); 49 | 50 | Log.d(TAG, 51 | "getFirstSendTaskTimeToHour,第一次执行任务的时间: " 52 | + mFormatterMonthToSecond.format(currentTime + firstSendTaskTime) 53 | + ",到该时间还差:" + firstSendTaskTime); 54 | return firstSendTaskTime; 55 | } 56 | 57 | /** 58 | * 获取第一次执行任务的延迟时间,精确到分 59 | * 60 | * @param hour 选择的小时 61 | * @param minute 选择的分钟 62 | * @return 第一次执行任务的延迟时间 63 | */ 64 | public static long getFirstSendTaskDelayedTime(int hour, int minute) { 65 | long currentTime = System.currentTimeMillis(); 66 | long firstSendTaskTime = getDifferBetweenTime(currentTime, getTodayTargetTime(hour, minute)); 67 | 68 | Log.d(TAG, 69 | "getFirstSendTaskTimeToHour,第一次执行任务的时间: " 70 | + mFormatterMonthToSecond.format(currentTime + firstSendTaskTime) 71 | + ",到该时间还差:" + firstSendTaskTime); 72 | return firstSendTaskTime; 73 | } 74 | 75 | /** 76 | * 获取第一次执行任务的延迟时间,精确到秒 77 | * 78 | * @param hour 选择的小时 79 | * @param minute 选择的分钟 80 | * @param second 选择的秒数 81 | * @return 第一次执行任务的延迟时间 82 | */ 83 | public static long getFirstSendTaskDelayedTime(int hour, int minute, int second) { 84 | long currentTime = System.currentTimeMillis(); 85 | long firstSendTaskTime = getDifferBetweenTime(currentTime, getTodayTargetTime(hour, minute, second)); 86 | 87 | Log.d(TAG, 88 | "getFirstSendTaskTimeToHour,第一次执行任务的时间: " 89 | + mFormatterMonthToSecond.format(currentTime + firstSendTaskTime) 90 | + ",到该时间还差:" + firstSendTaskTime); 91 | return firstSendTaskTime; 92 | } 93 | 94 | /** 95 | * 初始化Calendat 96 | * 97 | * @return 98 | */ 99 | private static Calendar getCalendar(int hour) { 100 | Calendar cal = Calendar.getInstance(); 101 | cal.set(Calendar.HOUR_OF_DAY, hour); 102 | cal.set(Calendar.MINUTE, 0); 103 | cal.set(Calendar.SECOND, 0); 104 | cal.set(Calendar.MILLISECOND, 0); 105 | return cal; 106 | } 107 | 108 | /** 109 | * 获取某一小时时刻的时间戳,以当天为基准 110 | * 111 | * @param hour 指定的小时,若小于0,日期会-1,若大于24会+1 112 | * @return 当天某一小时时刻的时间戳 113 | */ 114 | private static long getTodayTargetTime(int hour) { 115 | Calendar cal = getCalendar(hour); 116 | long time = cal.getTimeInMillis(); 117 | Log.i(TAG, "getTodayTargetTime,当天第: " + hour + "小时时间戳:" + time); 118 | return time; 119 | } 120 | 121 | /** 122 | * 获取某一小时某一分钟时刻的时间戳,以当天为基准 123 | * 124 | * @param hour 指定的小时,若小于0,日期会-1,若大于24会+1 125 | * @param minute 指定的分钟,若小于0,小时会-1,若大于60会+1 126 | * @return 当天某一小时时刻的时间戳某一分钟时刻的时间戳 127 | */ 128 | private static long getTodayTargetTime(int hour, int minute) { 129 | Calendar cal = getCalendar(hour); 130 | cal.set(Calendar.MINUTE, minute); 131 | long time = cal.getTimeInMillis(); 132 | Log.i(TAG, "getTodayTargetTime,当天第: " + hour + "小时" + minute + "分时间戳:" + time); 133 | return time; 134 | } 135 | 136 | /** 137 | * 获取某一小时某一分钟某一秒时刻的时间戳,以当天为基准 138 | * 139 | * @param hour 指定的小时,若小于0,日期会-1,若大于24会+1 140 | * @param minute 指定的分钟,若小于0,小时会-1,若大于60会+1 141 | * @param second 指定的秒数,若小于0,分钟会-1,若大于60会+1 142 | * @return 当天某一小时时刻的时间戳某一分钟时刻的时间戳 143 | */ 144 | private static long getTodayTargetTime(int hour, int minute, int second) { 145 | Calendar cal = getCalendar(hour); 146 | cal.set(Calendar.MINUTE, minute); 147 | cal.set(Calendar.SECOND, second); 148 | long time = cal.getTimeInMillis(); 149 | Log.i(TAG, "getTodayTargetTime,当天第: " + hour + "小时" + minute + "分" + second + "秒时间戳:" + time); 150 | return time; 151 | } 152 | 153 | 154 | } 155 | -------------------------------------------------------------------------------- /library/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | library 3 | 4 | -------------------------------------------------------------------------------- /library/src/test/java/com/gavinrowe/lgw/library/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.gavinrowe.lgw.library; 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 | } -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':library' 2 | --------------------------------------------------------------------------------