pull/32/head
kunfei 5 years ago
parent 03b89125cc
commit 0ff5d733e6
  1. 108
      app/src/main/java/io/legado/app/help/ActivityHelp.kt
  2. 11
      app/src/main/java/io/legado/app/receiver/MediaButtonReceiver.kt

@ -0,0 +1,108 @@
package io.legado.app.help
import android.app.Activity
import java.lang.ref.WeakReference
import java.util.*
/**
* Activity管理器,管理项目中Activity的状态
*/
class ActivityHelp private constructor() {
companion object {
private var activities: MutableList<WeakReference<Activity>> = arrayListOf()
@Volatile
private var instance: ActivityHelp? = null
fun getInstance(): ActivityHelp? {
if (null == instance) {
synchronized(ActivityHelp::class.java) {
if (null == instance) {
instance = ActivityHelp()
}
}
}
return instance
}
}
fun getActivities(): List<WeakReference<Activity>> {
return activities
}
/**
* 添加Activity
*/
fun add(activity: Activity) {
activities.add(WeakReference(activity))
}
/**
* 移除Activity
*/
fun remove(activity: Activity) {
for (temp in activities) {
if (null != temp.get() && temp.get() === activity) {
activities.remove(temp)
break
}
}
}
/**
* 移除Activity
*/
fun remove(activityClass: Class<*>) {
val iterator = activities.iterator()
while (iterator.hasNext()) {
val item = iterator.next()
if (item.get()?.javaClass == activityClass) {
iterator.remove()
}
}
}
/**
* 关闭指定 activity
*/
fun finishActivity(vararg activities: Activity) {
for (activity in activities) {
activity?.finish()
}
}
/**
* 关闭指定 activity(class)
*/
fun finishActivity(vararg activityClasses: Class<*>) {
val waitFinish = ArrayList<WeakReference<Activity>>()
for (temp in activities) {
for (activityClass in activityClasses) {
if (temp.get()?.javaClass == activityClass) {
waitFinish.add(temp)
break
}
}
}
for (activityWeakReference in waitFinish) {
activityWeakReference.get()?.finish()
}
}
/**
* 判断指定Activity是否存在
*/
fun isExist(activityClass: Class<*>): Boolean? {
var result = false
for (item in activities) {
if (item.get()?.javaClass == activityClass) {
result = true
break
}
}
return result
}
}

@ -24,16 +24,15 @@ class MediaButtonReceiver : BroadcastReceiver() {
val keycode = event.keyCode
val action = event.action
val command: String? = null
var command = false
when (keycode) {
KeyEvent.KEYCODE_MEDIA_STOP, KeyEvent.KEYCODE_MEDIA_PAUSE, KeyEvent.KEYCODE_MEDIA_PLAY, KeyEvent.KEYCODE_HEADSETHOOK, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE -> {
command = true
}
else -> {
}
}// command = ReadAloudService.ActionMediaButton;
if (command != null) {
if (command) {
if (action == KeyEvent.ACTION_DOWN) {
readAloud(context, command)
readAloud(context)
return true
}
}
@ -41,7 +40,7 @@ class MediaButtonReceiver : BroadcastReceiver() {
return false
}
private fun readAloud(context: Context, command: String?) {
private fun readAloud(context: Context) {
}
}

Loading…
Cancel
Save