├── .gitignore ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── zl │ │ │ └── weilu │ │ │ └── saber │ │ │ ├── MyApp.java │ │ │ ├── activity │ │ │ ├── MainActivity.java │ │ │ ├── MediatorLiveDataActivity.java │ │ │ ├── TestActivity.java │ │ │ └── TestKotlinActivity.kt │ │ │ ├── bean │ │ │ ├── JavaBean.java │ │ │ ├── KotlinBean.kt │ │ │ ├── MediatorTest.java │ │ │ ├── SeekBar.java │ │ │ ├── Single.java │ │ │ └── Timer.java │ │ │ ├── fragment │ │ │ ├── TestFragment.java │ │ │ └── TestFragment1.java │ │ │ └── viewmodel │ │ │ └── LiveDataTimerViewModel.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ ├── activity_main.xml │ │ ├── activity_mediator.xml │ │ ├── activity_test.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 │ └── zl │ └── weilu │ └── saber │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── saber-annotation ├── .gitignore ├── build.gradle └── src │ └── main │ └── java │ └── com │ └── zl │ └── weilu │ └── saber │ └── annotation │ ├── AndroidViewModel.java │ ├── BindViewModel.java │ ├── LiveData.java │ ├── LiveDataClassType.java │ ├── LiveDataType.java │ ├── LiveEventBus.java │ ├── ObserveType.java │ └── OnChange.java ├── saber-api ├── .gitignore ├── build.gradle ├── consumer-proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ └── java │ └── com │ └── zl │ └── weilu │ └── saber │ └── api │ ├── Saber.java │ ├── UnBinder.java │ └── event │ └── SingleLiveEvent.java ├── saber-compiler ├── .gitignore ├── build.gradle └── src │ └── main │ ├── java │ └── com │ │ └── zl │ │ └── weilu │ │ └── saber │ │ └── compiler │ │ ├── BindViewModelProcessor.java │ │ ├── LiveDataProcessor.java │ │ └── utils │ │ ├── BaseProcessor.java │ │ ├── ClassEntity.java │ │ ├── Entity.java │ │ ├── EntityHandler.java │ │ ├── FieldEntity.java │ │ ├── JavaPoetTypeUtils.java │ │ ├── MethodEntity.java │ │ ├── OnRegulaListener.java │ │ └── StringUtils.java │ └── resources │ └── META-INF │ └── gradle │ └── incremental.annotation.processors ├── saberx-api ├── .gitignore ├── build.gradle ├── consumer-proguard-rules.pro └── src │ ├── main │ ├── AndroidManifest.xml │ └── java │ │ └── com │ │ └── zl │ │ └── weilu │ │ └── saber │ │ └── api │ │ ├── Saber.java │ │ ├── UnBinder.java │ │ └── event │ │ └── SingleLiveEvent.java │ └── test │ └── java │ └── com │ └── zl │ └── weilu │ └── saber │ └── api │ └── ExampleUnitTest.java └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | .idea 5 | .DS_Store 6 | /build 7 | /captures 8 | .externalNativeBuild 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Saber 2 | 3 | [![jitpack](https://jitpack.io/v/simplezhli/saber.svg)](https://jitpack.io/#simplezhli/saber) [![LICENSE](https://img.shields.io/badge/license-Apache%202-blue.svg)](https://raw.githubusercontent.com/simplezhli/Saber/master/LICENSE) [![作者](https://img.shields.io/badge/%E4%BD%9C%E8%80%85-weilu-orange.svg)](http://blog.csdn.net/qq_17766199) 4 | 5 | ## 本项目帮助你快速使用LiveData与ViewModel 6 | 7 | - 已适配AndroidX。 8 | 9 | - 支持Kotlin。 10 | 11 | - 支持 `ViewModel`、`AndroidViewModel`。(默认为 `ViewModel`) 12 | 13 | - 支持 `observe`、`observeForever` 两种观察模式。(默认为 `observe`) 14 | 15 | - 支持 `SingleLiveEvent`、`MediatorLiveData`、`MutableLiveData`。(默认为 `MutableLiveData`) 16 | 17 | - 支持 `SavedState`(仅AndroidX) 18 | 19 | - 支持自定义`LiveData`类型。 20 | 21 | - 支持事件总线的操作。 22 | 23 | - Forever模式自动取消订阅。 24 | 25 | - 支持注解处理器增量编译。 26 | 27 | ## 详细介绍 28 | 29 | - [感受LiveData 与 ViewModel结合之美](https://blog.csdn.net/qq_17766199/article/details/80732836) 30 | 31 | ## 使用方式 32 | 33 | 添加依赖 34 | 35 | ```gradle 36 | implementation 'com.github.simplezhli.saber:saber-api:0.3.1' 37 | //AndroidX使用 38 | implementation 'com.github.simplezhli.saber:saberx-api:0.3.1' 39 | 40 | annotationProcessor 'com.github.simplezhli.saber:saber-compiler:0.3.1' 41 | ``` 42 | 43 | 首先创建一个类,使用`@LiveData`注解标记你要保存的数据。注意这里的参数名称value,下面会用到。 44 | ```java 45 | public class SeekBar { 46 | 47 | @LiveData 48 | Integer value; 49 | } 50 | ``` 51 | 52 | 当然也可以直接标记你的JavaBean,来直接保存此类。那么参数名为类名的首字母小写:seekBar 53 | ```java 54 | @LiveData 55 | public class SeekBar { 56 | 57 | Integer value; 58 | } 59 | ``` 60 | 61 | 使用`@LiveData(classType = LiveDataClassType.LIST)`可以指定对应的数据集合类型 62 | 63 | Build -- > Make Project 会生成代码如下: 64 | 65 | ```java 66 | public class SeekBarViewModel extends ViewModel { 67 | private MutableLiveData mValue; 68 | 69 | public MutableLiveData getValue() { 70 | if (mValue == null) { 71 | mValue = new MutableLiveData<>(); 72 | } 73 | return mValue; 74 | } 75 | 76 | public Integer getValueValue() { 77 | return getValue().getValue(); 78 | } 79 | 80 | public void setValue(Integer mValue) { 81 | if (this.mValue == null) { 82 | return; 83 | } 84 | this.mValue.setValue(mValue); 85 | } 86 | 87 | public void postValue(Integer mValue) { 88 | if (this.mValue == null) { 89 | return; 90 | } 91 | this.mValue.postValue(mValue); 92 | } 93 | } 94 | 95 | ``` 96 | 97 | 如果想使用`AndroidViewModel`的话,可以添加`@AndroidViewModel`注解 98 | 99 | ```java 100 | @AndroidViewModel 101 | public class SeekBar { 102 | 103 | @LiveData 104 | Integer value; 105 | } 106 | ``` 107 | 108 | 自定义`LiveData`类型 109 | 110 | ```java 111 | public class Single { 112 | 113 | @LiveData(type = LiveDataType.OTHER, liveDataType = XXXLiveData.class) 114 | Integer value; 115 | } 116 | 117 | ``` 118 | 119 | 生成代码提供了LiveData的常用操作。 120 | 121 | - `setXXX()`要在主线程中调用。 122 | 123 | - `postXXX()`既可在主线程也可在子线程中调用。 124 | 125 | - `getXXX()`用于获取观察者。 126 | 127 | - `getXXXValue()`可以获取保存的数据。 128 | 129 | - `addSource()`用于监听LiveData。(MediatorLiveData专用) 130 | 131 | - `removeSource()`移除监听的LiveData。(MediatorLiveData专用) 132 | 133 | ### 1. 普通使用方法 134 | 135 | 一般情况下可以直接使用它。比如: 136 | 137 | ```java 138 | public class TestFragment extends Fragment { 139 | 140 | private SeekBar mSeekBar; 141 | 142 | @BindViewModel(isShare = true) //<--标记需要绑定的ViewModel 143 | SeekBarViewModel mSeekBarViewModel; 144 | 145 | @Override 146 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 147 | // Inflate the layout for this fragment 148 | View root = inflater.inflate(R.layout.fragment_test, container, false); 149 | mSeekBar = root.findViewById(R.id.seekBar); 150 | Saber.bind(this); // <--这里绑定ViewModel 151 | subscribeSeekBar(); 152 | return root; 153 | } 154 | 155 | private void subscribeSeekBar() { 156 | 157 | mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 158 | @Override 159 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 160 | if (fromUser) { 161 | mSeekBarViewModel.setValue(progress); 162 | } 163 | } 164 | ...... 165 | }); 166 | } 167 | 168 | @OnChange(model = "mSeekBarViewModel") //<--接收变化的ViewModel变量名 169 | void setData(Integer value){ //注意这里使用 @LiveData 标记的参数名 170 | if (value != null) { 171 | mSeekBar.setProgress(value); 172 | } 173 | } 174 | } 175 | ``` 176 | 177 | `@BindViewModel`用于绑定ViewModel。 178 | 179 | `@OnChange(model = "xxx")`用于接收指定ViewModel的数据变化,可以不设置,默认model名称为mViewModel。 180 | 181 | 如果需要`Fragment`之间数据共享,需要`@BindViewModel(isShare = true)`,当然也要保证传入相同的key值。默认key值是类的规范名称,也就是包名加类名。 182 | 183 | ![这里写图片描述](https://img-blog.csdn.net/20180619100304754?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzE3NzY2MTk5/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70) 184 | 185 | 所以一旦需要互通的Fragment类名或包名不一致,就无法数据共享。这时可以指定key值:`@BindViewModel(key = "value")` 186 | 187 | ### 2. 事件总线使用方法,[详细用法参看LiveEventBus](https://github.com/JeremyLiao/LiveEventBus) 188 | 189 | ```java 190 | @LiveEventBus(model = "key_name") 191 | void liveDataBus(String value){ 192 | 193 | } 194 | ``` 195 | 196 | 发送: 197 | 198 | ```java 199 | LiveEventBus.get().with("key_name").postValue("value"); 200 | ``` 201 | 202 | 203 | 更多的使用方法可以参看本项目demo。 204 | 205 | ### 3.Kotlin环境使用注意事项 206 | 207 | 1.将以下代码添加到 `build.gradle` 文件中,保证生成代码的正确性。 208 | 209 | ```gradle 210 | 211 | kapt { 212 | correctErrorTypes = true 213 | } 214 | 215 | ``` 216 | 217 | 2.Kotlin默认会生成set/get方法,并把属性设置为`private` 所以只要保证Kotlin中字段可见性不是`private`即可,简单解决可以在字段上添加 `@JvmField`,也可以使用`lateinit`. 218 | 219 | ```kotlin 220 | 221 | @BindViewModel 222 | lateinit var mViewModel: TestViewModel 223 | 224 | //或 225 | 226 | @JvmField 227 | @BindViewModel 228 | var mViewModel: TestViewModel? = null 229 | ``` 230 | 231 | ## TODO 232 | 233 | 1.~~因为现有的`@OnChange`注解承载的功能过多,不易使用。后面会将`EventBus`功能从中提出,添加一个新的注解(或许叫做`@LiveEventBus`)。~~ 234 | 235 | 2.有什么好的建议或者功能欢迎提Issues。 236 | 237 | ## 版本变化 238 | 239 | - [点击查看](https://github.com/simplezhli/Saber/releases) 240 | 241 | ## Thanks For 242 | 243 | - [butterknife](https://github.com/JakeWharton/butterknife) 244 | 245 | - [在 SnackBar,Navigation 和其他事件中使用 LiveData](https://juejin.im/post/5b2b1b2cf265da5952314b63) 246 | 247 | - [LiveEventBus](https://github.com/JeremyLiao/LiveEventBus) 248 | 249 | ## License 250 | 251 | Copyright 2018 simplezhli 252 | 253 | Licensed under the Apache License, Version 2.0 (the "License"); 254 | you may not use this file except in compliance with the License. 255 | You may obtain a copy of the License at 256 | 257 | http://www.apache.org/licenses/LICENSE-2.0 258 | 259 | Unless required by applicable law or agreed to in writing, software 260 | distributed under the License is distributed on an "AS IS" BASIS, 261 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 262 | See the License for the specific language governing permissions and 263 | limitations under the License. 264 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-kapt' 4 | 5 | android { 6 | compileSdkVersion 29 7 | defaultConfig { 8 | applicationId "com.zl.weilu.saber" 9 | minSdkVersion 15 10 | targetSdkVersion 29 11 | versionCode 6 12 | versionName "0.1.1" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled true 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'consumer-proguard-rules.pro' 18 | } 19 | } 20 | kapt { 21 | correctErrorTypes = true 22 | } 23 | } 24 | 25 | dependencies { 26 | implementation fileTree(dir: 'libs', include: ['*.jar']) 27 | implementation 'androidx.appcompat:appcompat:1.2.0' 28 | testImplementation 'junit:junit:4.13.2' 29 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.4.31" 30 | // implementation 'com.github.simplezhli.saber:saberx-api:0.3.1' 31 | // kapt 'com.github.simplezhli.saber:saber-compiler:0.3.1' 32 | implementation project(':saberx-api') 33 | kapt project(':saber-compiler') 34 | } 35 | -------------------------------------------------------------------------------- /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 -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /app/src/main/java/com/zl/weilu/saber/MyApp.java: -------------------------------------------------------------------------------- 1 | package com.zl.weilu.saber; 2 | 3 | import android.app.Application; 4 | 5 | import com.jeremyliao.liveeventbus.LiveEventBus; 6 | 7 | /** 8 | * @Description: 9 | * @Author: weilu 10 | * @Time: 2019/5/16 0016 09:28. 11 | */ 12 | public class MyApp extends Application { 13 | 14 | @Override 15 | public void onCreate() { 16 | super.onCreate(); 17 | LiveEventBus.config() 18 | .enableLogger(BuildConfig.DEBUG) 19 | .lifecycleObserverAlwaysActive(false); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /app/src/main/java/com/zl/weilu/saber/activity/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.zl.weilu.saber.activity; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.util.Log; 6 | import android.view.View; 7 | import android.widget.SeekBar; 8 | import android.widget.TextView; 9 | 10 | import androidx.appcompat.app.AppCompatActivity; 11 | 12 | import com.zl.weilu.saber.R; 13 | import com.zl.weilu.saber.annotation.BindViewModel; 14 | import com.zl.weilu.saber.annotation.LiveEventBus; 15 | import com.zl.weilu.saber.annotation.ObserveType; 16 | import com.zl.weilu.saber.annotation.OnChange; 17 | import com.zl.weilu.saber.api.Saber; 18 | import com.zl.weilu.saber.viewmodel.LiveDataTimerViewModel; 19 | import com.zl.weilu.saber.viewmodel.SingleViewModel; 20 | 21 | 22 | public class MainActivity extends AppCompatActivity { 23 | 24 | private TextView textView; 25 | private SeekBar mSeekBar; 26 | 27 | @BindViewModel 28 | LiveDataTimerViewModel mTimerViewModel; 29 | @BindViewModel 30 | SingleViewModel mSingleViewModel; 31 | 32 | @Override 33 | protected void onCreate(Bundle savedInstanceState) { 34 | super.onCreate(savedInstanceState); 35 | setContentView(R.layout.activity_main); 36 | 37 | textView = this.findViewById(R.id.tv); 38 | textView.setOnClickListener(new View.OnClickListener() { 39 | @Override 40 | public void onClick(View v) { 41 | startActivity(new Intent(MainActivity.this, TestActivity.class)); 42 | } 43 | }); 44 | 45 | mSeekBar = this.findViewById(R.id.seekBar); 46 | mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 47 | @Override 48 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 49 | if (fromUser) { 50 | mSingleViewModel.setValue(progress); 51 | } 52 | } 53 | 54 | @Override 55 | public void onStartTrackingTouch(SeekBar seekBar) { } 56 | 57 | @Override 58 | public void onStopTrackingTouch(SeekBar seekBar) { } 59 | }); 60 | 61 | Saber.bind(this); 62 | } 63 | 64 | @OnChange(model = "mTimerViewModel") 65 | void setData(Long time){ 66 | String newText = MainActivity.this.getResources().getString(R.string.seconds, time); 67 | textView.setText(newText); 68 | Log.d("MainActivity", "Updating timer"); 69 | } 70 | 71 | /** 72 | * SingleViewModel的变动每次只能发送一次 73 | * 添加了多个,则只会有一个响应变化 74 | * */ 75 | 76 | @OnChange(model = "mSingleViewModel") 77 | void setData(Integer value){ 78 | if (value != null) { 79 | Log.d("MainActivity", "监听一号SeekBar的数值:" + value); 80 | } 81 | } 82 | 83 | @OnChange(model = "mSingleViewModel") 84 | void setData1(Integer value){ 85 | if (value != null) { 86 | Log.d("MainActivity", "监听二号SeekBar的数值:" + value); 87 | } 88 | } 89 | 90 | /** 91 | * isSticky = true 表示使用Sticky模式 92 | * type = ObserveType.FOREVER 使用Forever模式,默认具有生命周期感知能力。 93 | * 可以使用LiveEventBus.get().with("key_name").postValue("") 来发送事件。 94 | * */ 95 | @LiveEventBus(key = "key_name", isSticky = true, type = ObserveType.FOREVER) 96 | void liveDataBus(String value){ 97 | Log.d("MainActivity", "LiveDataBus接收到的值:" +value); 98 | } 99 | 100 | public void toMediatorLiveDataActivity(View view) { 101 | startActivity(new Intent(MainActivity.this, MediatorLiveDataActivity.class)); 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /app/src/main/java/com/zl/weilu/saber/activity/MediatorLiveDataActivity.java: -------------------------------------------------------------------------------- 1 | package com.zl.weilu.saber.activity; 2 | 3 | import android.os.Bundle; 4 | import android.util.Log; 5 | 6 | import com.zl.weilu.saber.R; 7 | import com.zl.weilu.saber.annotation.BindViewModel; 8 | import com.zl.weilu.saber.annotation.OnChange; 9 | import com.zl.weilu.saber.api.Saber; 10 | import com.zl.weilu.saber.bean.JavaBean; 11 | import com.zl.weilu.saber.viewmodel.JavaBeanViewModel; 12 | import com.zl.weilu.saber.viewmodel.MediatorTestViewModel; 13 | import com.zl.weilu.saber.viewmodel.SeekBarViewModel; 14 | import com.zl.weilu.saber.viewmodel.TimerViewModel; 15 | 16 | import java.util.ArrayList; 17 | 18 | import androidx.appcompat.app.AppCompatActivity; 19 | import androidx.lifecycle.Observer; 20 | 21 | public class MediatorLiveDataActivity extends AppCompatActivity { 22 | 23 | @BindViewModel 24 | SeekBarViewModel mSeekBarViewModel; 25 | @BindViewModel 26 | TimerViewModel mTimerViewModel; 27 | @BindViewModel 28 | JavaBeanViewModel mJavaBeanViewModel; 29 | @BindViewModel 30 | MediatorTestViewModel mediatorTestViewModel; 31 | 32 | @Override 33 | protected void onCreate(Bundle savedInstanceState) { 34 | super.onCreate(savedInstanceState); 35 | setContentView(R.layout.activity_mediator); 36 | Saber.bind(this); 37 | 38 | mediatorTestViewModel.addValueSource(mSeekBarViewModel.getValue(), new Observer() { 39 | 40 | @Override 41 | public void onChanged(Integer integer) { 42 | mediatorTestViewModel.setValue(String.valueOf(integer)); 43 | } 44 | }); 45 | 46 | mediatorTestViewModel.addValueSource(mTimerViewModel.getTime(), new Observer() { 47 | 48 | @Override 49 | public void onChanged(Long aLong) { 50 | mediatorTestViewModel.setValue(String.valueOf(aLong)); 51 | } 52 | 53 | }); 54 | 55 | mTimerViewModel.setTime(900L); 56 | mSeekBarViewModel.setValue(120); 57 | } 58 | 59 | 60 | @OnChange(model = "mediatorTestViewModel") 61 | void setData(String value){ 62 | // 输出 120 900 ,为倒序 63 | Log.d("MediatorActivity", value); 64 | } 65 | 66 | @OnChange(model = "mJavaBeanViewModel") 67 | void setJavaBean(ArrayList javaBean){ 68 | 69 | } 70 | 71 | @Override 72 | protected void onDestroy() { 73 | super.onDestroy(); 74 | mediatorTestViewModel.removeValueSource(mSeekBarViewModel.getValue()); 75 | mediatorTestViewModel.removeValueSource(mTimerViewModel.getTime()); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /app/src/main/java/com/zl/weilu/saber/activity/TestActivity.java: -------------------------------------------------------------------------------- 1 | package com.zl.weilu.saber.activity; 2 | 3 | import android.os.Bundle; 4 | import android.util.Log; 5 | 6 | import androidx.appcompat.app.AppCompatActivity; 7 | 8 | import com.jeremyliao.liveeventbus.LiveEventBus; 9 | import com.zl.weilu.saber.R; 10 | import com.zl.weilu.saber.annotation.BindViewModel; 11 | import com.zl.weilu.saber.annotation.ObserveType; 12 | import com.zl.weilu.saber.annotation.OnChange; 13 | import com.zl.weilu.saber.api.Saber; 14 | import com.zl.weilu.saber.viewmodel.SeekBarViewModel; 15 | 16 | /** 17 | * Activity监听Fragment数据改变 18 | */ 19 | public class TestActivity extends AppCompatActivity { 20 | 21 | @BindViewModel(key = "mm") 22 | SeekBarViewModel mViewModel; 23 | 24 | @Override 25 | protected void onCreate(Bundle savedInstanceState) { 26 | super.onCreate(savedInstanceState); 27 | setContentView(R.layout.activity_test); 28 | Saber.bind(this); 29 | LiveEventBus.get("key_name").postDelay("----1111", 2000); 30 | } 31 | 32 | // 使用observeForever 33 | // 不设置model,默认为mViewModel。 34 | @OnChange(type = ObserveType.FOREVER) 35 | void setData(Integer value){ 36 | if (value != null) { 37 | Log.e("TestActivity:", "Fragment1,2中,SeekBar的数值:" + value); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /app/src/main/java/com/zl/weilu/saber/activity/TestKotlinActivity.kt: -------------------------------------------------------------------------------- 1 | package com.zl.weilu.saber.activity 2 | 3 | import android.os.Bundle 4 | import android.util.Log 5 | import androidx.appcompat.app.AppCompatActivity 6 | import com.zl.weilu.saber.R 7 | import com.zl.weilu.saber.annotation.BindViewModel 8 | import com.zl.weilu.saber.annotation.OnChange 9 | import com.zl.weilu.saber.api.Saber 10 | import com.zl.weilu.saber.viewmodel.KotlinBeanViewModel 11 | 12 | class TestKotlinActivity : AppCompatActivity() { 13 | 14 | @JvmField 15 | @BindViewModel 16 | var mViewModel: KotlinBeanViewModel? = null 17 | 18 | override fun onCreate(savedInstanceState: Bundle?) { 19 | super.onCreate(savedInstanceState) 20 | setContentView(R.layout.activity_test) 21 | Saber.bind(this) 22 | mViewModel?.setB(false) 23 | } 24 | 25 | @OnChange 26 | fun onChange(b: Boolean?) { 27 | Log.e("TestKotlinActivity:", "$b") 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /app/src/main/java/com/zl/weilu/saber/bean/JavaBean.java: -------------------------------------------------------------------------------- 1 | package com.zl.weilu.saber.bean; 2 | 3 | import com.zl.weilu.saber.annotation.AndroidViewModel; 4 | import com.zl.weilu.saber.annotation.LiveData; 5 | import com.zl.weilu.saber.annotation.LiveDataClassType; 6 | 7 | /** 8 | * @Description: 9 | * @Author: weilu 10 | * @Time: 2018/11/22 0022 11:46. 11 | */ 12 | @AndroidViewModel 13 | @LiveData(classType = LiveDataClassType.ARRAY_LIST) 14 | public class JavaBean { 15 | 16 | private int age; 17 | private String name; 18 | private boolean sex; 19 | 20 | public int getAge() { 21 | return age; 22 | } 23 | 24 | public void setAge(int age) { 25 | this.age = age; 26 | } 27 | 28 | public String getName() { 29 | return name; 30 | } 31 | 32 | public void setName(String name) { 33 | this.name = name; 34 | } 35 | 36 | public boolean isSex() { 37 | return sex; 38 | } 39 | 40 | public void setSex(boolean sex) { 41 | this.sex = sex; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /app/src/main/java/com/zl/weilu/saber/bean/KotlinBean.kt: -------------------------------------------------------------------------------- 1 | package com.zl.weilu.saber.bean 2 | 3 | import com.zl.weilu.saber.annotation.LiveData 4 | 5 | /** 6 | * @Description: 7 | * @Author: weilu 8 | * @Time: 2019/8/30 0030 10:34. 9 | */ 10 | 11 | class KotlinBean { 12 | 13 | @LiveData 14 | var i: Int = 0 15 | @LiveData 16 | var s: Short = 0 17 | @LiveData 18 | var bt: Byte = 0 19 | @LiveData 20 | var c: Char = 's' 21 | @LiveData 22 | var f: Float = 0f 23 | @LiveData 24 | var d: Double = 0.0 25 | @LiveData 26 | var l: Long = 0 27 | @LiveData 28 | var b: Boolean? = false 29 | } -------------------------------------------------------------------------------- /app/src/main/java/com/zl/weilu/saber/bean/MediatorTest.java: -------------------------------------------------------------------------------- 1 | package com.zl.weilu.saber.bean; 2 | 3 | import com.zl.weilu.saber.annotation.LiveData; 4 | import com.zl.weilu.saber.annotation.LiveDataType; 5 | 6 | import java.util.List; 7 | 8 | public class MediatorTest { 9 | 10 | @LiveData(type = LiveDataType.MEDIATOR) 11 | String value; 12 | 13 | @LiveData(type = LiveDataType.MEDIATOR) 14 | String name; 15 | 16 | @LiveData 17 | List list; 18 | } 19 | -------------------------------------------------------------------------------- /app/src/main/java/com/zl/weilu/saber/bean/SeekBar.java: -------------------------------------------------------------------------------- 1 | package com.zl.weilu.saber.bean; 2 | 3 | import com.zl.weilu.saber.annotation.LiveData; 4 | 5 | /** 6 | * @Description: 7 | * @Author: weilu 8 | * @Time: 2018/6/11 0011 17:13. 9 | */ 10 | public class SeekBar { 11 | 12 | @LiveData(isSavedState = true) // 是否启用SavedState 13 | Integer value; 14 | 15 | @LiveData 16 | Integer value1; 17 | } 18 | -------------------------------------------------------------------------------- /app/src/main/java/com/zl/weilu/saber/bean/Single.java: -------------------------------------------------------------------------------- 1 | package com.zl.weilu.saber.bean; 2 | 3 | import com.zl.weilu.saber.annotation.LiveData; 4 | import com.zl.weilu.saber.annotation.LiveDataType; 5 | 6 | /** 7 | * @Description: 8 | * @Author: weilu 9 | * @Time: 2018/7/25 0025 17:53. 10 | */ 11 | public class Single { 12 | 13 | // @LiveData(type = LiveDataType.OTHER, liveDataType = SingleLiveEvent.class) 14 | // 或 15 | @LiveData(type = LiveDataType.SINGLE) 16 | Integer value; 17 | } 18 | -------------------------------------------------------------------------------- /app/src/main/java/com/zl/weilu/saber/bean/Timer.java: -------------------------------------------------------------------------------- 1 | package com.zl.weilu.saber.bean; 2 | 3 | import com.zl.weilu.saber.annotation.AndroidViewModel; 4 | import com.zl.weilu.saber.annotation.LiveData; 5 | 6 | @AndroidViewModel 7 | public class Timer { 8 | 9 | @LiveData 10 | Long time; 11 | } 12 | -------------------------------------------------------------------------------- /app/src/main/java/com/zl/weilu/saber/fragment/TestFragment.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017, The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.zl.weilu.saber.fragment; 18 | 19 | import android.os.Bundle; 20 | import android.util.Log; 21 | import android.view.LayoutInflater; 22 | import android.view.View; 23 | import android.view.ViewGroup; 24 | import android.widget.SeekBar; 25 | import android.widget.TextView; 26 | 27 | import androidx.fragment.app.Fragment; 28 | 29 | import com.zl.weilu.saber.R; 30 | import com.zl.weilu.saber.annotation.BindViewModel; 31 | import com.zl.weilu.saber.annotation.OnChange; 32 | import com.zl.weilu.saber.api.Saber; 33 | import com.zl.weilu.saber.viewmodel.SeekBarViewModel; 34 | 35 | /** 36 | * Shows a SeekBar that is synced with a value in a ViewModel. 37 | */ 38 | public class TestFragment extends Fragment { 39 | 40 | private SeekBar mSeekBar; 41 | private TextView mTextView; 42 | 43 | @BindViewModel(isShare = true) 44 | SeekBarViewModel mSeekBarViewModel; 45 | 46 | @Override 47 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 48 | // Inflate the layout for this fragment 49 | View root = inflater.inflate(R.layout.fragment_test, container, false); 50 | mSeekBar = root.findViewById(R.id.seekBar); 51 | mTextView = root.findViewById(R.id.tv); 52 | Saber.bind(this); 53 | subscribeSeekBar(); 54 | return root; 55 | } 56 | 57 | private void subscribeSeekBar() { 58 | 59 | mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 60 | @Override 61 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 62 | if (fromUser) { 63 | mSeekBarViewModel.setValue(progress); 64 | } 65 | } 66 | 67 | @Override 68 | public void onStartTrackingTouch(SeekBar seekBar) { } 69 | 70 | @Override 71 | public void onStopTrackingTouch(SeekBar seekBar) { } 72 | }); 73 | } 74 | 75 | @OnChange(model = "mSeekBarViewModel") 76 | void setData(Integer value) { 77 | Log.d("TestFragment", value + ""); 78 | if (value != null) { 79 | mTextView.setText(value + "%"); 80 | mSeekBar.setProgress(value); 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /app/src/main/java/com/zl/weilu/saber/fragment/TestFragment1.java: -------------------------------------------------------------------------------- 1 | package com.zl.weilu.saber.fragment; 2 | 3 | import android.os.Bundle; 4 | import android.util.Log; 5 | import android.view.LayoutInflater; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | import android.widget.SeekBar; 9 | import android.widget.TextView; 10 | 11 | import androidx.fragment.app.Fragment; 12 | 13 | import com.zl.weilu.saber.R; 14 | import com.zl.weilu.saber.annotation.BindViewModel; 15 | import com.zl.weilu.saber.annotation.OnChange; 16 | import com.zl.weilu.saber.api.Saber; 17 | import com.zl.weilu.saber.viewmodel.SeekBarViewModel; 18 | 19 | /** 20 | * Shows a SeekBar that is synced with a value in a ViewModel. 21 | */ 22 | public class TestFragment1 extends Fragment { 23 | 24 | private SeekBar mSeekBar; 25 | private TextView mTextView; 26 | /** 27 | * 指定key值 28 | */ 29 | @BindViewModel(key = "mm", isShare = true) 30 | SeekBarViewModel mViewModel; 31 | 32 | @Override 33 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 34 | // Inflate the layout for this fragment 35 | View root = inflater.inflate(R.layout.fragment_test, container, false); 36 | mSeekBar = root.findViewById(R.id.seekBar); 37 | mTextView = root.findViewById(R.id.tv); 38 | Saber.bind(this); 39 | subscribeSeekBar(); 40 | return root; 41 | } 42 | 43 | private void subscribeSeekBar() { 44 | 45 | mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 46 | @Override 47 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 48 | if (fromUser) { 49 | mViewModel.setValue1(progress); 50 | } 51 | } 52 | 53 | @Override 54 | public void onStartTrackingTouch(SeekBar seekBar) { } 55 | 56 | @Override 57 | public void onStopTrackingTouch(SeekBar seekBar) { } 58 | }); 59 | } 60 | 61 | @OnChange 62 | void setData(Integer value1){ 63 | Log.d("TestFragment1", value1 + ""); 64 | if (value1 != null) { 65 | mTextView.setText(value1 + "%"); 66 | mSeekBar.setProgress(value1); 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /app/src/main/java/com/zl/weilu/saber/viewmodel/LiveDataTimerViewModel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017, The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.zl.weilu.saber.viewmodel; 18 | 19 | import android.app.Application; 20 | import android.os.SystemClock; 21 | 22 | import com.zl.weilu.saber.activity.MainActivity; 23 | 24 | import java.util.Timer; 25 | import java.util.TimerTask; 26 | 27 | /** 28 | * A ViewModel used for the {@link MainActivity}. 29 | */ 30 | public class LiveDataTimerViewModel extends TimerViewModel { 31 | 32 | private static final int ONE_SECOND = 1000; 33 | 34 | private long mInitialTime; 35 | 36 | public LiveDataTimerViewModel(Application application) { 37 | super(application); 38 | 39 | mInitialTime = SystemClock.elapsedRealtime(); 40 | Timer timer = new Timer(); 41 | 42 | // Update the elapsed time every second. 43 | timer.scheduleAtFixedRate(new TimerTask() { 44 | @Override 45 | public void run() { 46 | final long newValue = (SystemClock.elapsedRealtime() - mInitialTime) / 1000; 47 | // setValue() cannot be called from a background thread so post to main thread. 48 | postTime(newValue); 49 | } 50 | }, ONE_SECOND, ONE_SECOND); 51 | 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 |