android.app.RemoteServiceException:Context.startForegroundService()然后未在React native中调用Service.startForeground()

时间:2018-08-11 09:49:20

标签: javascript android react-native service backgroundworker

我正在尝试使用react-native-background-job在Android Oreo中创建后台服务,但是当我启动该服务时,呼叫成功,但显示崩溃对话框服务已停止。请提供针对此问题的解决方案。

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

2 个答案:

答案 0 :(得分:1)

不允许您使用mylist <- split(iris, c("setosa", "virginica")) # a list of two data frames启动后台服务-如果服务在5秒内没有通过startForegroundService和通知提升到前台,则该服务将被系统杀死。

看看related question and answers

答案 1 :(得分:0)

我找到了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);
      }
    }
  }
}