A related service error using "Context.startForegroundService () did not cause a Service.startForeground () error"

I am currently working on an application for playing sound, and I am using a running service to play music in the background. I start and get attached to the service using the following code.

val intent = Intent(context, MusicService::class.java)
context.startService(intent)
context.bindService(intent, serviceConnection, 0)

He moves to the forefront of the game and gets a downgrade when the music pauses.

// onAudioPlay
service.startForeground(NOTIFY_ID, notification)

// onAudioPause
service.stopForeground(false)

The service is still working. But when the notification is paused (deleted) by the user in a paused state, the service is reset after a few seconds, giving this error.

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myplayer, PID: 4380
    android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
        android.app.ActivityThread$H.handleMessage(ActivityThread.java:1775)
        android.os.Handler.dispatchMessage(Handler.java:105)
        android.os.Looper.loop(Looper.java:164)
        android.app.ActivityThread.main(ActivityThread.java:6541)
        java.lang.reflect.Method.invoke(Native Method)
      com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

This only happens in Oreo, and I already read about background restrictions in Oreo. But the following points are deceiving me.

  • This service is a related service (not subject to restrictions)
  • Context.startForegroundService() ( ).
  • , , .

? ? , - , .

+20
5

UdeshUk - MediaButtonReceiver ( , ) , startForegroundService, MediaButtonReceiver.onReceive Oreo. .

+11

Service onCreate(): ->

private void startServiceOreoCondition(){
        if (Build.VERSION.SDK_INT >= 26) {


            String CHANNEL_ID = "my_service";
            String CHANNEL_NAME = "My Background Service";

            NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
                    CHANNEL_NAME,NotificationManager.IMPORTANCE_NONE);
            ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);

            Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                    .setCategory(Notification.CATEGORY_SERVICE).setSmallIcon(R.drawable.ic_tgc_icon).setPriority(PRIORITY_MIN).build();

            startForeground(101, notification);
        }
    }
+8

.

setDeleteIntent(MediaButtonReceiver.buildMediaButtonPendingIntent(service, PlaybackStateCompat.ACTION_STOP))

, , startForegroundService(), stopForeground() .

, onStartCommand() MediaButtonReveiver PendingIntent

+5

8.0

Context.startForegroundService() . startForeground() .

, startForeground onCreate() .

+2

Late to the party, but as mentioned earlier, this MediaButtonReceiver; he has this code:

    @Override
    public void onReceive(Context context, Intent intent) {
        ...
        startForegroundService(context, intent);
        ...
    }

I use the recipient in a related service, so I created a class that has the same code as that MediaButtonReceiver, except for this line. I replaced it with:

        context.startService(intent);
0
source

Source: https://habr.com/ru/post/1690173/


All Articles