Skip to content

Commit

Permalink
1. 添加 LiveEventBus;
Browse files Browse the repository at this point in the history
2. 添加 ZKLiveEventBus Demo;
3. 添加 ZKLiveEventBus 使用说明。
  • Loading branch information
WangQing committed Jun 30, 2019
1 parent fd364a3 commit e9dc59b
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 2 deletions.
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,53 @@
# ZKLiveEventBus
ZKLiveEventBus

## 优先在 Application 注册

如果没跨进程的,可以不用注册

```
// 请优先注册,最好是 Application 注册
ZKLiveEventBus.init(this)
```

## 添加监听消息事件

可以在 Activity 或者 Fragment

```
ZKLiveEventBus.instance.with(TAG).observe(this, Observer {
toast(it)
})
or
ZKLiveEventBus.instance.with(TAG, String::class.java).observe(this, Observer {
toast(it)
})
```


## 发送消息

```
ZKLiveEventBus.instance.with(TAG).post("测试消息")
or
ZKLiveEventBus.instance.with(TAG).postDelay("测试消息", 1000)
or
ZKLiveEventBus.instance.with(TAG, String::class.java).post("测试消息")
```

## 参考地址

https://github.com/JeremyLiao/LiveEventBus

为了方便使用,直接将:LiveEventBus.get() 修改 ZKLiveEventBus.instance 即可。

## 原理详解

https://www.cnblogs.com/meituantech/p/9376449.html
2 changes: 2 additions & 0 deletions ZKLiveEventBus/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ android {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation deps.kotlin_stdlib8

api 'com.jeremyliao:live-event-bus:1.4.4'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.zkteam.live.event

import android.content.Context
import com.jeremyliao.liveeventbus.LiveEventBus

class ZKLiveEventBus {

private object Holder {
val INSTANCE = LiveEventBus.get()!!
}

companion object {
fun init(context: Context) {
val mContext = context.applicationContext
ZKLiveEventBus.instance
.config()
.supportBroadcast(mContext)
.lifecycleObserverAlwaysActive(true)
.autoClear(false)
}

val instance: LiveEventBus by lazy {
Holder.INSTANCE
}
}

}
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ dependencies {
implementation support.design
implementation support.app_compat
implementation deps.kotlin_stdlib8
implementation project(path: ':ZKLiveEventBus')
}
20 changes: 20 additions & 0 deletions app/src/main/java/com/zkteam/live/event/demo/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
package com.zkteam.live.event.demo

import android.arch.lifecycle.Observer
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.Toast
import com.zkteam.live.event.ZKLiveEventBus
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

private val TAG = "flag_data"

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

// 请优先注册,最好是 Application 注册
ZKLiveEventBus.init(this)

ZKLiveEventBus.instance.with(TAG).observe(this, Observer {
toast(it)
})

btn.setOnClickListener {
ZKLiveEventBus.instance.with(TAG).post("测试消息")
}
}

private fun toast(it: Any?) {
Toast.makeText(this, "$it", Toast.LENGTH_SHORT).show()
}
}
5 changes: 3 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Hello World!"/>
android:text="测试发送事件"/>

</RelativeLayout>

0 comments on commit e9dc59b

Please sign in to comment.