pull/32/head
kunfei 5 years ago
parent debf517d1d
commit 43530e2246
  1. 2
      app/src/main/AndroidManifest.xml
  2. 4
      app/src/main/java/io/legado/app/receiver/MediaButtonReceiver.kt
  3. 6
      app/src/main/java/io/legado/app/service/ReadAloudService.kt

@ -64,7 +64,7 @@
<service android:name=".service.UpdateService" /> <service android:name=".service.UpdateService" />
<service android:name=".service.WebService" /> <service android:name=".service.WebService" />
<receiver android:name=".receiver.MediaButtonIntentReceiver"> <receiver android:name=".receiver.MediaButtonReceiver">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" /> <action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter> </intent-filter>

@ -11,10 +11,10 @@ import android.view.KeyEvent
* 监听耳机键 * 监听耳机键
*/ */
class MediaButtonIntentReceiver : BroadcastReceiver() { class MediaButtonReceiver : BroadcastReceiver() {
companion object { companion object {
val TAG = MediaButtonIntentReceiver::class.java.simpleName val TAG = MediaButtonReceiver::class.java.simpleName
fun handleIntent(context: Context, intent: Intent): Boolean { fun handleIntent(context: Context, intent: Intent): Boolean {
val intentAction = intent.action val intentAction = intent.action

@ -18,7 +18,7 @@ import io.legado.app.constant.AppConst
import io.legado.app.constant.Bus import io.legado.app.constant.Bus
import io.legado.app.constant.Status import io.legado.app.constant.Status
import io.legado.app.help.PendingIntentHelp import io.legado.app.help.PendingIntentHelp
import io.legado.app.receiver.MediaButtonIntentReceiver import io.legado.app.receiver.MediaButtonReceiver
import io.legado.app.utils.postEvent import io.legado.app.utils.postEvent
import io.legado.app.utils.toast import io.legado.app.utils.toast
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -140,7 +140,7 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage
* 初始化MediaSession * 初始化MediaSession
*/ */
private fun initMediaSession() { private fun initMediaSession() {
val mComponent = ComponentName(packageName, MediaButtonIntentReceiver::class.java.name) val mComponent = ComponentName(packageName, MediaButtonReceiver::class.java.name)
val mediaButtonIntent = Intent(Intent.ACTION_MEDIA_BUTTON) val mediaButtonIntent = Intent(Intent.ACTION_MEDIA_BUTTON)
mediaButtonIntent.component = mComponent mediaButtonIntent.component = mComponent
val mediaButtonReceiverPendingIntent = PendingIntent.getBroadcast( val mediaButtonReceiverPendingIntent = PendingIntent.getBroadcast(
@ -151,7 +151,7 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage
mediaSessionCompat = MediaSessionCompat(this, tag, mComponent, mediaButtonReceiverPendingIntent) mediaSessionCompat = MediaSessionCompat(this, tag, mComponent, mediaButtonReceiverPendingIntent)
mediaSessionCompat?.setCallback(object : MediaSessionCompat.Callback() { mediaSessionCompat?.setCallback(object : MediaSessionCompat.Callback() {
override fun onMediaButtonEvent(mediaButtonEvent: Intent): Boolean { override fun onMediaButtonEvent(mediaButtonEvent: Intent): Boolean {
return MediaButtonIntentReceiver.handleIntent(this@ReadAloudService, mediaButtonEvent) return MediaButtonReceiver.handleIntent(this@ReadAloudService, mediaButtonEvent)
} }
}) })
mediaSessionCompat?.setMediaButtonReceiver(mediaButtonReceiverPendingIntent) mediaSessionCompat?.setMediaButtonReceiver(mediaButtonReceiverPendingIntent)

Loading…
Cancel
Save