Context.startForegroundService did not then call Service.startForeground

19,802

According to official document of Android 8.0 Background Execution Limits

Android 8.0 introduces the new method startForegroundService() to start a new service in the foreground. After the system has created the service, the app has five seconds to call the service's startForeground() method to show the new service's user-visible notification. If the app does not call startForeground() within the time limit, the system stops the service and declares the app to be ANR.

So, make sure you have started ongoing notification by calling startForeground (int id, Notification notification) in the onCreate() method of your service.

Note: Apps targeting API Build.VERSION_CODES.P or later must request the permission Manifest.permission.FOREGROUND_SERVICE in order to use this API.

Share:
19,802

Related videos on Youtube

Somomo1q
Author by

Somomo1q

Updated on September 16, 2022

Comments

  • Somomo1q
    Somomo1q almost 2 years

    It's mine BroadcastReciever class. The class working on Boot phone status.

    Code ;

    public class BroadCastRecieverBoot extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent ıntent) {
            if(Intent.ACTION_BOOT_COMPLETED.equals(ıntent.getAction()))
            {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    context.startForegroundService(new Intent(context, MyService.class));
                    context.startForegroundService(new Intent(context, GPSTracker.class));
                } else {
                    context.startService(new Intent(context, MyService.class));
                    context.startService(new Intent(context, GPSTracker.class));
                }
            }
        }
    }
    

    I get This Error ;

         android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
    
    
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1792)
    
    at android.os.Handler.dispatchMessage(Handler.java:106)                                            
    
            at android.os.Looper.loop(Looper.java:164)                                                   
    
            at android.app.ActivityThread.main(ActivityThread.java:6523)                                        
    
            at java.lang.reflect.Method.invoke(Native Method)                                                   
    
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:857)
    

    It doesn't work on Android Oreo now. I don't know what is the mistake of that.

  • Sagar
    Sagar over 5 years
    Apps targeting API 27 but getting crashes on Build.VERSION_CODES.P
  • Priyank Patel
    Priyank Patel over 5 years
    @Sagar Have you started the notification using startForeground(int id, Notification notification) method in your service class?
  • Sagar
    Sagar over 5 years
    yes I have started the notification using startForeground(int id, Notification notification)
  • Priyank Patel
    Priyank Patel over 5 years
    @Sagar Can you post a new question with your crash logs? Send me link of that, I will surely help you if I can.
  • Gowthaman M
    Gowthaman M about 5 years
    i add this android.permission.FOREGROUND_SERVICE it's working
  • Limyandi Vico Trico
    Limyandi Vico Trico almost 4 years
    @GowthamanM have you experience any performance issue with adding that permission?
  • Gowthaman M
    Gowthaman M almost 4 years
    @LimyandiVicoTrico Becase of permission you will not get the performance issuse....There is issues in your service please check your service class
  • Jithish P N
    Jithish P N over 2 years
    still it is not working