gpt4 book ai didi

android - 牛轧糖/奥利奥的 JobScheduler

转载 作者:行者123 更新时间:2023-11-29 16:44:16 27 4
gpt4 key购买 nike

我遵循了一些关于 android 中的作业调度的在线教程,我尝试这样做,发现下面的代码编译没有错误。但问题是 onPostExecute/doInBackground 返回消息中没有 toast。有什么可能需要的吗?就像我只是运行代码并 toast ,并注意到 myScheduler.schedule(myjobInfo); 工作,后台任务正在执行。

我的 gradle 文件根本没有动过

compileSdkVersion 26
defaultConfig {
applicationId "com.example.umarmalik.myapplication"
minSdkVersion 21
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

这是我的 JobScheduler 类

  public class MJobScheduler extends JobService {

private MJobExecutor executor;


@Override
public boolean onStartJob(final JobParameters params) {

executor = new MJobExecutor(){
@Override
protected void onPostExecute(String s) {
Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();
jobFinished(params,true);
}
};
executor.execute();
return true;
}

@Override
public boolean onStopJob(JobParameters params) {
executor.cancel(true);
return false;
}
}

这是我的作业执行器

public class MJobExecutor extends AsyncTask<Void,Void,String> {

@Override
protected String doInBackground(Void... voids) {
return "Background task running";
}
}

我的主要 Activity 类如下:

public class MainActivity extends AppCompatActivity {


private static final int JOBID = 110;
private JobScheduler myScheduler;
private JobInfo myjobInfo;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ComponentName myComp = new ComponentName(this,MJobScheduler.class);
JobInfo.Builder myBuilder = new JobInfo.Builder(JOBID,myComp);

myBuilder.setPeriodic(6000);
myBuilder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
myBuilder.setPersisted(true);


myScheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
myjobInfo = myBuilder.build();


myScheduler.schedule(myjobInfo);
}
}

我的 list

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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<service
android:name=".MJobScheduler"
android:permission="android.permission.BIND_JOB_SERVICE"
android:exported="true" />

</application>

最佳答案

根据此答案,定期作业的最短可能时间为 15 分钟,这就是它对您不起作用的原因。 https://stackoverflow.com/a/48337856/5604896

作业完成后更新 Activity 的更好方法是使用广播接收器。如果 Activity 正在运行,它将收到通知。

public class MJobScheduler extends JobService {

private MJobExecutor executor;


@Override
public boolean onStartJob(final JobParameters params) {

executor = new MJobExecutor(this, params);
executor.execute();
return true;
}

@Override
public boolean onStopJob(JobParameters params) {
executor.cancel(true);
return false;
}

private static class MJobExecutor extends AsyncTask<Void, Void, String> {
private WeakReference<JobService> jobServiceReference;
private JobParameters params;

MJobExecutor(JobService jobService, JobParameters params) {
jobServiceReference = new WeakReference<>(jobService);
this.params = params;
}

@Override
protected String doInBackground(Void... voids) {
return "Background task running";
}

@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
JobService jobService = jobServiceReference.get();
if (jobService != null) {
Intent intent = new Intent("JOB_FINISHED");
intent.putExtra("result", s);
LocalBroadcastManager.getInstance(jobService).sendBroadcast(intent);
jobService.jobFinished(params, true);
}
}
}
}

以及您的 Activity :

public class MainActivity extends AppCompatActivity {


private static final int JOBID = 110;
private JobScheduler myScheduler;
private JobInfo myjobInfo;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ComponentName myComp = new ComponentName(this, MJobScheduler.class);
JobInfo.Builder myBuilder = new JobInfo.Builder(JOBID, myComp);
myBuilder.setPeriodic(15 * 60 * 1000);

myBuilder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
myBuilder.setPersisted(true);

myScheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
myjobInfo = myBuilder.build();


myScheduler.schedule(myjobInfo);

LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, new IntentFilter("JOB_FINISHED"));
}

private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, intent.getStringExtra("result"), Toast.LENGTH_LONG).show();
}
};

@Override
protected void onDestroy() {
super.onDestroy();
LocalBroadcastManager.getInstance(this).unregisterReceiver(broadcastReceiver);
}
}

关于android - 牛轧糖/奥利奥的 JobScheduler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49153076/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com