HYMXDEV
HYMXDEV

Reputation: 443

Android O: Service.stopForeground equivalent

I'm currently trying to make some kind of music player app. For playback I created a separate service. Because I want to keep it playing all the time, I'm calling Service.startForeground(). While compiling for Android O (SDK level 27) I have to use Context.startForegroundService() due to Androids "new" background execution limits.

This works fine, but when the user pauses playback, I want the notification to be dismissable. In the past, I was able to call stopForeground(), but this has no effect on Android O. Is there any other way to switch between background and foreground service or any other chance to implement this behaviour? Apps like Google Play Music are using exactly this notification pattern, but how do they do it?

Upvotes: 12

Views: 10655

Answers (4)

thaovd1712
thaovd1712

Reputation: 318

stopForeground(STOP_FOREGROUND_REMOVE)

Use this in case you want remove notification too

Upvotes: 4

Hardik Hirpara
Hardik Hirpara

Reputation: 3046

Use

stopForeground(STOP_FOREGROUND_DETACH)

instead of

stopForeground(true)

Upvotes: 1

Muhammad Sulaiman
Muhammad Sulaiman

Reputation: 151

I got the same issue and tried many solution but later on i got to know that i have given some permission in AndroidManifest.xml file which were of no use, when i commented them it was working fine

The commented permission are as following

<!-- <uses-permission android:name="android.permission.POST_NOTIFICATIONS"/> -->
<!-- <uses-permission android:name="android.permission.GET_ACCOUNTS" /> -->
<!-- <uses-permission android:name="android.permission.READ_CONTACTS" /> -->
<!-- <uses-permission android:name="android.permission.VIBRATE"/> -->
<!-- <uses-permission android:name="android.permission.READ_PHONE_STATE"/> -->
<!-- <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> -->

Upvotes: -2

HYMXDEV
HYMXDEV

Reputation: 443

This happened because I used the wrong method. You have to use stopForeground(STOP_FOREGROUND_DETACH) instead of stopForeground(true).

Upvotes: 31

Related Questions