Last active
October 23, 2018 14:54
-
-
Save jirevwe/2927244d00c38c9a745c2636d82dcd8e to your computer and use it in GitHub Desktop.
Some Fix for Unexpected Service Shutdown
This file contains hidden or 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
<!-- KitKat Fix Activity --> | |
<activity | |
android:name=".player.KitKatFixActivity" | |
android:allowTaskReparenting="true" | |
android:alwaysRetainTaskState="true" | |
android:clearTaskOnLaunch="true" | |
android:enabled="true" | |
android:excludeFromRecents="true" | |
android:finishOnTaskLaunch="true" | |
android:noHistory="true" | |
android:stateNotNeeded="true" | |
android:theme="@android:style/Theme.NoDisplay"/> |
This file contains hidden or 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
/** | |
* Fix for KitKat error where the service is killed as soon | |
* as the app is swiped away from the Recents menu. | |
*/ | |
override fun onTaskRemoved(rootIntent: Intent) { | |
val intent = Intent(this, KitKatFixActivity::class.java) | |
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) | |
startActivity(intent) | |
} | |
override fun onDestroy() { | |
super.onDestroy() | |
eventBus.cleanup() | |
with(player!!) | |
{ | |
queueManager.duration = currentPosition | |
queueManager.isPlaying = isPlaying | |
release() | |
} | |
isPlayerReleased.set(true) | |
isServiceRunning = false | |
L.d("AudioPlayerService: onDestroy()") | |
if (killedSelf) return | |
val intent = Intent(this, KitKatFixActivity::class.java) | |
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or | |
Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) | |
startActivity(intent) | |
} |
This file contains hidden or 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
/* | |
* Copyright (C) 2014 Saravan Pantham | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package com.ouida.app.player | |
import android.app.Activity | |
import android.content.Intent | |
import android.os.Bundle | |
import com.ouida.app.utils.others.L | |
/* KitKat introduced a new bug: swiping away the app from the | |
* "Recent Apps" list causes all background services to shut | |
* down. To circumvent this issue, this dummy activity will | |
* launch momentarily and close (it will be invisible to the | |
* user). This will fool the OS into thinking that the service | |
* still has an Activity bound to it, and prevent it from being | |
* killed. | |
* | |
* ISSUE #53313: | |
* https://code.google.com/p/android/issues/detail?id=53313 | |
* | |
* ISSUE #63618: | |
* https://code.google.com/p/android/issues/detail?id=63618 | |
* | |
* General discussion thread: | |
* https://groups.google.com/forum/#!topic/android-developers/LtmA9xbrD5A | |
*/ | |
class KitKatFixActivity : Activity() { | |
public override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
L.d("KitKatFixActivity: onCreate()") | |
finish() | |
} | |
override fun onDestroy() { | |
L.d("KitKatFixActivity: onDestroy()") | |
val serviceIntent = Intent(applicationContext, AudioPlayerService::class.java) | |
startService(serviceIntent) | |
super.onDestroy() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment