【问题标题】:Start service with JobScheduler on Android O在 Android O 上使用 JobScheduler 启动服务
【发布时间】:2018-04-23 16:35:34
【问题描述】:

我正在尝试启动 IntentService 以在 Android O 上注册到 Firebase 云消息传递。

在 Android O 上,不允许“在不允许的情况下”启动 Intent Service,每个人都告诉我使用 JobService,但不告诉我如何使用它。

JobInfo.Builder 应该有什么限制才能拥有“允许的情况”,我不断收到相同的 IllegalStateException

这是我的 JobService

@Override
public boolean onStartJob(JobParameters params) {
    Intent intent = new Intent(this, RegistrationIntentService.class);
    getApplicationContext().startService(intent);
    return false;
}

@Override
public boolean onStopJob(JobParameters params) {
    return false;
}

public static void scheduleJob(Context context) {
    ComponentName serviceComponent = new ComponentName(context, MyJobService.class);
    JobInfo.Builder builder = new JobInfo.Builder(MyJobService.JOB_ID, serviceComponent);
    builder.setMinimumLatency(1 * 1000); // wait at least
    JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
    if(jobScheduler != null) jobScheduler.schedule(builder.build());
}

【问题讨论】:

    标签: android firebase android-intentservice android-jobscheduler


    【解决方案1】:

    如果您使用的是支持库版本 26.1.0 或更高版本,则您可以访问类似于 Intent ServiceJobIntentService 并具有作业调度程序的额外好处,您无需管理任何东西,只需启动它。

    根据文档

    帮助处理已为作业/服务排队的工作。在 Android O 或更高版本上运行时,工作将通过 JobScheduler.enqueue 作为作业分派。在旧版本平台上运行时,它将使用 Context.startService。

    你可以在这里JobIntentService找到更多细节。

    import android.content.Context;
    import android.content.Intent;
    import android.support.annotation.NonNull;
    import android.support.v4.app.JobIntentService;
    
    public class JobIntentNotificationService extends JobIntentService {
    
        public static void start(Context context) {
            Intent starter = new Intent(context, JobIntentNotificationService.class);
            JobIntentNotificationService.enqueueWork(context, starter);
        }
    
        /**
         * Unique job ID for this service.
         */
        static final int JOB_ID = 1000;
    
        /**
         * Convenience method for enqueuing work in to this service.
         */
        private static void enqueueWork(Context context, Intent intent) {
            enqueueWork(context, JobIntentNotificationService.class, JOB_ID, intent);
        }
    
        @Override
        protected void onHandleWork(@NonNull Intent intent) {
            // do your work here
        }
    }
    

    你叫它的方式是

    JobIntentNotificationService.start(getApplicationContext());
    

    您需要为 Pre Oreo 设备添加此权限

    <!-- used for job scheduler pre Oreo -->
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    

    【讨论】:

    • 感谢您的帮助。在所有 android 版本上都能成功运行
    • 我有一个 MainActivity.java,如何从 MainActivity.java 启动 JobIntentNotificationService ?
    【解决方案2】:

    Firebase 实际上有一个专门用于接收消息的服务,称为FirebaseMessagingServiceThis Firebase page 应该包含帮助您开始这方面的所有信息。

    除此之外,您正在尝试从服务访问应用程序上下文,而您应该使用父服务的基本上下文:

        getApplicationContext().startService(intent);
    

        startService(intent);
    

    如果您想从 FirebaseMessagingService 启动某些工作,请查看他们的 JobDispatcher 库,这非常棒。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-23
      • 2018-02-09
      • 2018-12-20
      • 2017-09-01
      • 1970-01-01
      • 2011-02-16
      • 1970-01-01
      相关资源
      最近更新 更多