Android 8.0: java.lang.IllegalStateException: Not allowed to start service Intent

前端 未结 17 1029
借酒劲吻你
借酒劲吻你 2020-11-22 09:15

On application launch, app starts the service that should to do some network task. After targeting API level 26, my application fails to start service on Android 8.0 on back

相关标签:
17条回答
  • 2020-11-22 09:26

    I got solution. For pre-8.0 devices, you have to just use startService(), but for post-7.0 devices, you have to use startForgroundService(). Here is sample for code to start service.

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            context.startForegroundService(new Intent(context, ServedService.class));
        } else {
            context.startService(new Intent(context, ServedService.class));
        }
    

    And in service class, please add the code below for notification:

    @Override
    public void onCreate() {
        super.onCreate();
        startForeground(1,new Notification());
    }
    

    Where O is Android version 26.

    If you don't want your service to run in Foreground and want it to run in background instead, post Android O you must bind the service to a connection like below:

    Intent serviceIntent = new Intent(context, ServedService.class);
    context.startService(serviceIntent);
    context.bindService(serviceIntent, new ServiceConnection() {
         @Override
         public void onServiceConnected(ComponentName name, IBinder service) {
             //retrieve an instance of the service here from the IBinder returned 
             //from the onBind method to communicate with 
         }
    
         @Override
         public void onServiceDisconnected(ComponentName name) {
         }
    }, Context.BIND_AUTO_CREATE);
    
    0 讨论(0)
  • 2020-11-22 09:26

    if you have integrated firebase messaging push notification then,

    Add new/update firebase messaging dependencies for android O (Android 8.0), due to Background Execution Limits.

    compile 'com.google.firebase:firebase-messaging:11.4.0'
    

    upgrade google play services and google repositories if needed.

    Update:

     compile 'com.google.firebase:firebase-messaging:11.4.2'
    
    0 讨论(0)
  • 2020-11-22 09:27

    don't use in onStartCommand:

    return START_NOT_STICKY
    

    just change it to:

    return START_STICKY
    

    and it's will working

    0 讨论(0)
  • 2020-11-22 09:31

    From the firebase release notes, they state that support for Android O was first released in 10.2.1 (although I'd recommend using the most recent version).

    please add new firebase messaging dependencies for android O

    compile 'com.google.firebase:firebase-messaging:11.6.2'
    

    upgrade google play services and google repositories if needed.

    0 讨论(0)
  • 2020-11-22 09:34

    If any intent was previously working fine when the app is in the background, it won't be the case any more from Android 8 and above. Only referring to intent which has to do some processing when app is in the background.

    The below steps have to be followed:

    1. Above mentioned intent should be using JobIntentService instead of IntentService.
    2. The class which extends JobIntentService should implement the - onHandleWork(@NonNull Intent intent) method and should have below the method, which will invoke the onHandleWork method:

      public static void enqueueWork(Context context, Intent work) {
          enqueueWork(context, xyz.class, 123, work);
      }
      
    3. Call enqueueWork(Context, intent) from the class where your intent is defined.

      Sample code:

      Public class A {
      ...
      ...
          Intent intent = new Intent(Context, B.class);
          //startService(intent); 
          B.enqueueWork(Context, intent);
      }
      

    The below class was previously extending the Service class

    Public Class B extends JobIntentService{
    ...
    
        public static void enqueueWork(Context context, Intent work) {
            enqueueWork(context, B.class, JobId, work);
        }
    
        protected void onHandleWork(@NonNull Intent intent) {
            ...
            ...
        }
    }
    
    1. com.android.support:support-compat is needed for JobIntentService - I use 26.1.0 V.

    2. Most important is to ensure the Firebase libraries version is on at least 10.2.1, I had issues with 10.2.0 - if you have any!

    3. Your manifest should have the below permission for the Service class:

      service android:name=".B"
      android:exported="false"
      android:permission="android.permission.BIND_JOB_SERVICE"
      

    Hope this helps.

    0 讨论(0)
  • 2020-11-22 09:35

    The best way is to use JobIntentService which uses the new JobScheduler for Oreo or the old services if not available.

    Declare in your manifest:

    <service android:name=".YourService"
             android:permission="android.permission.BIND_JOB_SERVICE"/>
    

    And in your service you have to replace onHandleIntent with onHandleWork:

    public class YourService extends JobIntentService {
    
        public static final int JOB_ID = 1;
    
        public static void enqueueWork(Context context, Intent work) {
            enqueueWork(context, YourService.class, JOB_ID, work);
        }
    
        @Override
        protected void onHandleWork(@NonNull Intent intent) {
            // your code
        }
    
    }
    

    Then you start your service with:

    YourService.enqueueWork(context, new Intent());
    
    0 讨论(0)
提交回复
热议问题