【问题标题】:android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground( ) in React nativeandroid.app.RemoteServiceException: Context.startForegroundService() 没有在 React native 中调用 Service.startForeground()
【发布时间】:2018-08-11 09:49:20
【问题描述】:

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

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

【问题讨论】:

    标签: javascript android react-native service backgroundworker


    【解决方案1】:

    你不能使用startForegroundService启动后台服务-如果服务没有在5秒内使用startForeground和通知将自己提升到前台,它将被系统杀死。

    看看related question and answers

    【讨论】:

      【解决方案2】:

      我为react-native-background-job找到了解决方案

      android/src/main/java/com/piloxa/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);
            }
          }
        }
      }

      【讨论】:

      猜你喜欢
      • 2017-11-09
      • 1970-01-01
      • 2019-01-09
      • 2018-03-29
      • 2018-03-04
      • 2019-09-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多