fix: Fix for ForegroundServiceDidNotStartInTimeException
This commit is contained in:
parent
3c84274e2d
commit
fb1d6990c1
3 changed files with 16 additions and 5 deletions
|
@ -19,9 +19,7 @@ import android.app.Activity
|
||||||
import android.content.*
|
import android.content.*
|
||||||
import android.database.Cursor
|
import android.database.Cursor
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.os.Handler
|
|
||||||
import android.os.IBinder
|
import android.os.IBinder
|
||||||
import android.os.Looper
|
|
||||||
import android.provider.DocumentsContract
|
import android.provider.DocumentsContract
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
|
@ -117,7 +115,12 @@ object MusicPlayerRemote : KoinComponent {
|
||||||
val realActivity = (context as Activity).parent ?: context
|
val realActivity = (context as Activity).parent ?: context
|
||||||
val contextWrapper = ContextWrapper(realActivity)
|
val contextWrapper = ContextWrapper(realActivity)
|
||||||
val intent = Intent(contextWrapper, MusicService::class.java)
|
val intent = Intent(contextWrapper, MusicService::class.java)
|
||||||
Handler(Looper.getMainLooper()).post {
|
|
||||||
|
// https://issuetracker.google.com/issues/76112072#comment184
|
||||||
|
// Workaround for ForegroundServiceDidNotStartInTimeException
|
||||||
|
try {
|
||||||
|
context.startService(intent)
|
||||||
|
} catch (e: Exception) {
|
||||||
ContextCompat.startForegroundService(context, intent)
|
ContextCompat.startForegroundService(context, intent)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -156,7 +156,9 @@ class MediaButtonIntentReceiver : MediaButtonReceiver() {
|
||||||
private fun startService(context: Context, command: String?) {
|
private fun startService(context: Context, command: String?) {
|
||||||
val intent = Intent(context, MusicService::class.java)
|
val intent = Intent(context, MusicService::class.java)
|
||||||
intent.action = command
|
intent.action = command
|
||||||
Handler(Looper.getMainLooper()).post {
|
try {
|
||||||
|
context.startService(intent)
|
||||||
|
} catch (e: Exception) {
|
||||||
ContextCompat.startForegroundService(context, intent)
|
ContextCompat.startForegroundService(context, intent)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1228,7 +1228,13 @@ class MusicService : MediaBrowserServiceCompat(),
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun playFromPlaylist(intent: Intent) {
|
private fun playFromPlaylist(intent: Intent) {
|
||||||
val playlist: AbsSmartPlaylist? = intent.extras?.let { BundleCompat.getParcelable(it, INTENT_EXTRA_PLAYLIST, AbsSmartPlaylist::class.java) }
|
val playlist: AbsSmartPlaylist? = intent.extras?.let {
|
||||||
|
BundleCompat.getParcelable(
|
||||||
|
it,
|
||||||
|
INTENT_EXTRA_PLAYLIST,
|
||||||
|
AbsSmartPlaylist::class.java
|
||||||
|
)
|
||||||
|
}
|
||||||
val shuffleMode = intent.getIntExtra(INTENT_EXTRA_SHUFFLE_MODE, getShuffleMode())
|
val shuffleMode = intent.getIntExtra(INTENT_EXTRA_SHUFFLE_MODE, getShuffleMode())
|
||||||
if (playlist != null) {
|
if (playlist != null) {
|
||||||
val playlistSongs = playlist.songs()
|
val playlistSongs = playlist.songs()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue