如果应用程序被杀死,作业调度程序将停止工作(Android)。

3

嗨,我在我的应用程序中对 JobScheduler 进行了一个示例。这是我如何启动它的:

jobScheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
ComponentName jobService = new ComponentName(getPackageName(),
        MyJobService.class.getName());
JobInfo jobInfo = new JobInfo.Builder(MYJOBID, jobService)
        .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
        .setExtras(bundle).build();
jobScheduler.schedule(jobInfo);

我在JobService中显示了一个提示:

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class MyJobService extends JobService {

public MyJobService() {
}

@Override
public boolean onStartJob(JobParameters params) {

   // UtilityMethods.showToast(this,params.getExtras().getString("json"));
    Toast.makeText(this,"test",Toast.LENGTH_SHORT).show();

    return false;
}

@Override
public boolean onStopJob(JobParameters params) {
    UtilityMethods.showToast(this,"onStop()");
    return false;
}

}

即使我尝试关闭互联网并从后台杀死应用程序,这仍然可以完美地工作。

然后,我尝试在我的一个库中构建类似的东西。我在库中编写了相同的代码,并从我的应用程序的MainActivity调用它。但是这一次,当我从后台杀死应用程序时,它停止工作。有人能告诉我为什么吗?

我初始化它的MainActivity

JobScheduler jobScheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
ComponentName jobService = new ComponentName(getPackageName(),
        MyJobService.class.getName());
JobInfo jobInfo = new JobInfo.Builder(MYJOBID, jobService)
        .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY).build();
jobScheduler.schedule(jobInfo);

当我从onCreate开始运行它时,它可以工作,但如果我从回调函数中开始运行它则无法工作。
非常感谢您的帮助。
2个回答

3

Android> 7自动节省电池电量。您必须启用应用程序的电池节省模式停止。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Intent intent = new Intent();
            String packageName = getPackageName();
            PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
            if (!pm.isIgnoringBatteryOptimizations(packageName)) {
                intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
                intent.setData(Uri.parse("package:" + packageName));
                startActivity(intent);
            }
        }

将此添加到AndroidManifest.xml文件中

<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>

是的,这只会起作用,而不是从销毁或其他狡猾的方法重新启动服务、作业、警报或任何东西,除此之外什么都不会起作用。 - aryanknp

2

使此返回true

@Override
public boolean onStartJob(JobParameters params) {

   // UtilityMethods.showToast(this,params.getExtras().getString("json"));
    Toast.makeText(this,"test",Toast.LENGTH_SHORT).show();

    return true;
}

并且从这里开始启动一个新线程(因为这是在主线程上执行的)。

onStartJob

added in API level 21
boolean onStartJob (JobParameters params)
Override this method with the callback logic for your job. Any such logic needs to be performed on a separate thread, as this function is executed on your application's main thread.

Parameters
params  JobParameters: Parameters specifying info about this job, including the extras bundle you optionally provided at job-creation time.
Returns
boolean True if your service needs to process the work (on a separate thread). False if there's no more work to be done for this job.

1
我不明白...那怎么可能工作?你能解释一下吗? - The Bat
我已经添加了来自谷歌页面的解释,请让我知道如果您不清楚。 - Ankit Khare
1
它在像红米这样的真实设备上运行正常。有人能解决这个问题吗? - Manku
如果您使用Workmanager、jobScheduler、Service或IntentService,它不会运行任何后台任务。因为中国制造商在用户从最近的应用程序中关闭应用程序时清除所有后台任务,但如果您使用定期任务,则会在一定时间间隔后工作。 - Nathani Software

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接