-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2. 添加 ZKLiveEventBus Demo; 3. 添加 ZKLiveEventBus 使用说明。
- Loading branch information
WangQing
committed
Jun 30, 2019
1 parent
fd364a3
commit e9dc59b
Showing
6 changed files
with
104 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
ZKLiveEventBus/src/main/java/com.zkteam.live.event/ZKLiveEventBus.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
app/src/main/java/com/zkteam/live/event/demo/MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters