android.app.RemoteServiceException:Context.startForegroundService()然后未在React本机中调用Service.startForeground() [英] android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground( ) in React native

查看:836
本文介绍了android.app.RemoteServiceException:Context.startForegroundService()然后未在React本机中调用Service.startForeground()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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); 
}

推荐答案

我找到了react-native-background-job

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

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);
      }
    }
  }
}

这篇关于android.app.RemoteServiceException:Context.startForegroundService()然后未在React本机中调用Service.startForeground()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆