Android 8.0以后使用后台Service服务JobIntentService的使用

Android8.0对系统资源的管控更加严格,添加了后台限制规则。

如果满足以下任意条件,应用将被视为处于前台:

具有可见 Activity(不管该 Activity 已启动还是已暂停)。
  具有前台服务。
  另一个前台应用已关联到该应用(不管是通过绑定到其中一个服务,还是通过使用其中一个内容提供程序)。 例如,如果另一个应    用绑定到该应用的服务,那么该应用处于前台:
  IME
  壁纸服务
  通知侦听器
  语音或文本服务
如果以上条件均不满足,应用将被视为处于后台

由于Android8.0以后不能使用后台服务,使用Service需要使用ContextCompat.startForegroundService启动前台服务,而且通知栏有Notification显示该Service正在运行。
如果还是希望使用服务在后台工作,通过使用服务开启子进程等等,不建议继续使用IntentService,可以使用JobIntentService。下面的具体的代码:

public class AlarmIntentService extends JobIntentService {

    private static final String TAG = "AlarmIntentService";

    private static final int JOB_ID = 1000;

    public AlarmIntentService() {
        super(/*"AlarmIntentService"*/);
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);//JobIntentService 重写onStartCommand方法时要返回super.onStartCommand()
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    public static void enqueueWork(Context context, Intent work) {
        enqueueWork(context, AlarmIntentService.class, JOB_ID, work);
    }

    /**
     * Called serially for each work dispatched to and processed by the service.  This
     * method is called on a background thread, so you can do long blocking operations
     * here.  Upon returning, that work will be considered complete and either the next
     * pending work dispatched here or the overall service destroyed now that it has
     * nothing else to do.
     *
     * <p>Be aware that when running as a job, you are limited by the maximum job execution
     * time and any single or total sequential items of work that exceeds that limit will
     * cause the service to be stopped while in progress and later restarted with the
     * last unfinished work.  (There is currently no limit on execution duration when
     * running as a pre-O plain Service.)</p>
     *
     * @param intent The intent describing the work to now be processed.
     */
    @Override
    protected void onHandleWork(@NonNull Intent intent) {
        
        
    }

    @Nullable
//    @Override
//    public IBinder onBind(Intent intent) {
//        return super.onBind(intent);
//    }


}
<uses-permission android:name="android.permission.WAKE_LOCK" />
<service
    android:name=".alarmnotification.AlarmIntentService"
    android:exported="true"
    android:permission="android.permission.BIND_JOB_SERVICE"/>

.需要添加android.permission.WAKE_LOCK权限,JobIntentService处理了亮屏/锁屏,因此要此权限。 

@Nullable
//    @Override
//    public IBinder onBind(Intent intent) {
//        return super.onBind(intent);
//    }

重写onBind方法,需返回super.onBind(),否则onHandleWork不会回调。

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return super.onStartCommand(intent, flags, startId);//JobIntentService 重写onStartCommand方法时要返回super.onStartCommand()
}

重写onStartCommand方法时要返回super.onStartCommand()

private void startAlarmIntentService() {
        Intent intent = new Intent(mContext, AlarmIntentService.class);
        intent.setAction(AlarmIntentService.ACTION_ALARMINTENTSERVICE);
//        mContext.startService(intent);
        AlarmIntentService.enqueueWork(mContext,intent);
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值