android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground( ) in React native

陌路散爱 提交于 2019-12-24 10:59:27

问题


I'm trying to create background service in Android Oreo using react-native-background-job but when I'll start the service is call success but Showing crash dialog service is stopped. please provide a solution for this problem.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 
  context.startForegroundService(starter); 
} 
else { 
  context.startService(starter); 
}

回答1:


You are not allowed to start background service using startForegroundService - if service doesn't elevate itself into the foreground with startForeground and notification within 5 seconds, it will be killed by the system.

Take a look at related question and answers.




回答2:


I got the solution in for react-native-background-job

android/src/main/java/com/pilloxa/backgroundjob/ReactNativeEventStarter.java

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;

public static class MyHeadlessJsTaskService extends HeadlessJsTaskService {
    private static final String LOG_TAG = MyHeadlessJsTaskService.class.getSimpleName();

    @Override
    @SuppressLint("WrongConstant")
    public void onCreate() {
      super.onCreate();
      if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        Context mContext = this.getApplicationContext();
        String CHANNEL_ID = "Background job";
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_ID, NotificationManager.IMPORTANCE_LOW);
        ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);
        Notification notification =
                new Notification.Builder(mContext, CHANNEL_ID)
                        .setContentTitle("Running background job")
                        .setContentText(mContext.getPackageName())
                        .setSmallIcon(R.drawable.ic_notification)
                        .build();
        startForeground(1, notification);
      }
    }

    @Nullable @Override protected HeadlessJsTaskConfig getTaskConfig(Intent intent) {
      Bundle extras = intent.getExtras();
      boolean allowExecutionInForeground = extras.getBoolean("allowExecutionInForeground", false);
      long timeout = extras.getLong("timeout", 2000);
      // For task with quick execution period additional check is required
      ReactNativeHost reactNativeHost =
          ((ReactApplication) getApplicationContext()).getReactNativeHost();
      boolean appInForeground = Utils.isReactNativeAppInForeground(reactNativeHost);
      if (appInForeground && !allowExecutionInForeground) {
        return null;
      }
      return new HeadlessJsTaskConfig(intent.getStringExtra("jobKey"), Arguments.fromBundle(extras),
          timeout, allowExecutionInForeground);
    }

    public static void start(Context context, Bundle jobBundle) {
      Intent starter = new Intent(context, MyHeadlessJsTaskService.class);
      starter.putExtras(jobBundle);
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        context.startForegroundService(starter);
      }else{
        context.startService(starter);
      }
    }
  }
}


来源:https://stackoverflow.com/questions/51798450/android-app-remoteserviceexception-context-startforegroundservice-did-not-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!