pull/34/head
kunfei 5 years ago
parent 1e59df8536
commit 98003f6e08
  1. 12
      app/src/main/java/io/legado/app/help/IntentHelp.kt
  2. 11
      app/src/main/java/io/legado/app/service/BaseReadAloudService.kt
  3. 46
      app/src/main/java/io/legado/app/service/CheckSourceService.kt

@ -1,5 +1,6 @@
package io.legado.app.help
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import io.legado.app.R
@ -19,4 +20,15 @@ object IntentHelp {
context.toast(R.string.tip_cannot_jump_setting_page)
}
}
inline fun <reified T> servicePendingIntent(context: Context, action: String): PendingIntent? {
return PendingIntent.getService(
context,
0,
Intent(context, T::class.java).apply {
this.action = action
},
PendingIntent.FLAG_UPDATE_CURRENT
)
}
}

@ -18,6 +18,7 @@ import io.legado.app.constant.AppConst
import io.legado.app.constant.Bus
import io.legado.app.constant.Status
import io.legado.app.help.IntentDataHelp
import io.legado.app.help.IntentHelp
import io.legado.app.help.MediaHelp
import io.legado.app.receiver.MediaButtonReceiver
import io.legado.app.ui.book.read.ReadBookActivity
@ -289,7 +290,9 @@ abstract class BaseReadAloudService : BaseService(),
.setOngoing(true)
.setContentTitle(nTitle)
.setContentText(nSubtitle)
.setContentIntent(readBookActivityPendingIntent(this))
.setContentIntent(
IntentHelp.servicePendingIntent<ReadBookActivity>(this, "activity")
)
if (pause) {
builder.addAction(
R.drawable.ic_play_24dp,
@ -323,12 +326,6 @@ abstract class BaseReadAloudService : BaseService(),
startForeground(112201, notification)
}
private fun readBookActivityPendingIntent(context: Context): PendingIntent {
val intent = Intent(context, ReadBookActivity::class.java)
intent.action = "readBookActivity"
return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
abstract fun aloudServicePendingIntent(context: Context, actionStr: String): PendingIntent
}

@ -1,8 +1,54 @@
package io.legado.app.service
import android.content.Intent
import androidx.core.app.NotificationCompat
import io.legado.app.R
import io.legado.app.base.BaseService
import io.legado.app.constant.Action
import io.legado.app.constant.AppConst
import io.legado.app.data.entities.BookSource
import io.legado.app.help.IntentHelp
import io.legado.app.ui.book.source.manage.BookSourceActivity
class CheckSourceService : BaseService() {
private var sourceList: List<BookSource>? = null
override fun onCreate() {
super.onCreate()
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
return super.onStartCommand(intent, flags, startId)
}
override fun onDestroy() {
super.onDestroy()
}
/**
* 更新通知
*/
private fun updateNotification(state: Int, msg: String) {
val builder = NotificationCompat.Builder(this, AppConst.channelIdReadAloud)
.setSmallIcon(R.drawable.ic_network_check)
.setOngoing(true)
.setContentTitle(getString(R.string.check_book_source))
.setContentText(msg)
.setContentIntent(
IntentHelp.servicePendingIntent<BookSourceActivity>(this, "activity")
)
.addAction(
R.drawable.ic_stop_black_24dp,
getString(R.string.cancel),
IntentHelp.servicePendingIntent<CheckSourceService>(this, Action.stop)
)
sourceList?.let {
builder.setProgress(it.size, state, false)
}
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
val notification = builder.build()
startForeground(112202, notification)
}
}
Loading…
Cancel
Save