Skip to content

Commit

Permalink
update TimeWatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
cbfg5210 committed Oct 21, 2019
1 parent 555862a commit e157098
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 13 deletions.
8 changes: 3 additions & 5 deletions app/src/main/java/com/bcst/receiver/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,15 @@ class MainActivity : AppCompatActivity(), View.OnClickListener {

when (v.id) {
R.id.btnTime -> {
val bcstWatcher = TimeWatcher("yyyy-MM-dd HH:mm:ss").onTimeEvent { timeMills, formattedTime ->
Log.e("***", "timeMills=$timeMills,formattedTime=$formattedTime")
}

BcstReceiver()
.withFilter { intentFilter ->
intentFilter.addAction(Intent.ACTION_TIME_CHANGED)
intentFilter.addAction(Intent.ACTION_TIME_TICK)
}
//.setCallback { context, intent -> Log.e("***", "${System.currentTimeMillis()}") }
.setBcstWatcher(bcstWatcher)
.setBcstWatcher(TimeWatcher("yyyy-MM-dd HH:mm:ss") { timeMills, formattedTime ->
Log.e("***", "timeMills=$timeMills,formattedTime=$formattedTime")
})
.bind(this, lifecycle)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,11 @@ import java.util.*
* 修改时间:2019/10/10 16:15
* 修改内容:
*/
class TimeWatcher(format: String? = null, locale: Locale? = null) : BcstWatcher {
class TimeWatcher(format: String? = null, locale: Locale? = null, private val action: (timeMills: Long, formattedTime: String?) -> Unit) : BcstWatcher {
private var dateFormat: SimpleDateFormat? = null
private lateinit var action: (timeMills: Long, formattedTime: String?) -> Unit

init {
format?.run { dateFormat = SimpleDateFormat(this, locale ?: Locale.CHINA) }
}

fun onTimeEvent(cb: (timeMills: Long, formattedTime: String?) -> Unit): TimeWatcher {
this.action = cb
return this
format?.run { dateFormat = SimpleDateFormat(this, locale ?: Locale.getDefault()) }
}

override fun create(): (context: Context, intent: Intent) -> Unit {
Expand Down

0 comments on commit e157098

Please sign in to comment.