I am developing an app to play music in which I have included a notification with the playback controls which allows you to continue listening to the music even if you close the application. The notification allows you to return to the app by clicking on it. This works perfectly if the application is paused ( onPause()
) but not if the application is closed ( onDestroy()
). When you return to the application by means of the notification after closing the app, you stop updating the elements. Ex: at the end of the song if we can listen as it happens to the next but we do not see on the screen the change of nobre in TextView
.
Creating the intent:
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_APP_MUSIC);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
Creation of the service intent that controls the notification:
intent = new Intent(getApplicationContext(), MediaPlayerIntentService.class);
startService(intent);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
AndroidManifest:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher_yousiccloud"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_yousiccloud_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:launchMode="singleInstance"
android:allowTaskReparenting="false"
android:alwaysRetainTaskState="true"
android:finishOnTaskLaunch="true"
android:debuggable="false"
tools:ignore="HardcodedDebugMode"
android:hardwareAccelerated="false"
android:largeHeap="true">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MediaPlayerIntentService"
android:exported="false"
android:enabled="true"
android:stopWithTask="false"/>
</application>