2

我已经绝望了,两天来我正在寻找如何在后台为android O安排服务,但没有任何效果,每周一次Alarm manger,我读了很多文章,我认为JS中的所有解决方案都不是很多关于它的问题,因为它的新操作系统和主题。我知道 android O 进行优化并杀死/销毁服务,但我知道如果我使用 JobIntentService 调度服务,它应该可以工作。这是我的代码

显现:

<service
            android:name=".task.UpdateJobScheduler"
            android:exported="false"
            android:permission="android.permission.BIND_JOB_SERVICE" />

在此之后我创建了 JobScheduler:

    JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
    JobInfo.Builder builder = new JobInfo.Builder(1, new ComponentName(context.getPackageName(),
            UpdateJobScheduler.class.getName()))
            .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
            .setMinimumLatency(1000) //just for test the number
            .setOverrideDeadline(3000) //just for test the number
            .setRequiresDeviceIdle(false)
            .setRequiresCharging(false)
            .setPersisted(false);

    jobScheduler.schedule(builder.build());

求职意向服务

public class UpdateJobScheduler extends JobIntentService {

private final static String TAG = UpdateJobScheduler.class.getSimpleName();
public static final int JOB_ID = 0X32421;

@Inject
AttributesModel attributesModel;
@Inject
CategoriesModel categoriesModel;
private CompositeDisposable disposable;


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

    DaggerUpdateBroadcastReceiverComponent
            .builder()
            .appComponent(((MyApplication) getApplicationContext()).getAppComponent())
            .build()
            .inject(this);
    disposable = new CompositeDisposable();

    Log.e(TAG, "ON create");
}

public static void enqueueWork(Context context, Intent work) {
    try {
        if(work.getIntExtra("test",0) == JOB_ID ){
            enqueueWork(context, UpdateJobScheduler.class, JOB_ID, work);
        } else {
            Log.e(TAG,"INTENT DATA UPDATE WRONG");
        }
    } catch (Exception e){
        Log.e(TAG,"Service collapse");
    }
}

@Override
protected void onHandleWork(@NonNull Intent intent) {
    Log.e(TAG, "working workingg!!!");
}

@Override
public boolean onStopCurrentWork() {
    disposable.clear();
    return super.onStopCurrentWork();
}

我也尝试enqueueWork在我的JobInfo课堂上设置它,但它不起作用。此外,我读到没有必要将 JobScheduler 和 jobIntentService 结合在一起,可以在 上安排服务enqueueWork,但我真的试图找到解决方案,但我真的没有找到任何适用于谷歌的示例。在控制台上,我有一次看到它可以工作,但什么也没有。

4

0 回答 0