gpt4 book ai didi

java - 当应用程序从后台删除时如何停止 JobService Scheduled?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:21:29 25 4
gpt4 key购买 nike

目前我正在使用一个应用程序,我的应用程序有一个功能,用户可以点击导航按钮,我的应用程序将启动谷歌地图。到现在为止都很好,我已经做到了。但我被卡住的事实是我希望我的应用程序执行一些任务。为了实现这一点,我使用了 JobService 并安排它每 5 秒运行一次,即使应用程序在后台也是如此。

当用户按下后退按钮然后在 onDestroy 方法中我取消了调度程序。但是,当应用程序通过滑动或按下十字图标从后台删除时,JobService 会继续运行,因为当它从后台删除时,操作系统可以调用或不调用 onDestroy 方法。当应用程序从后台删除时,如何停止计划的作业?

enter image description here

enter image description here

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="javarank.com.serviceinbackground">

<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=".MyJobService" android:exported="true" android:permission="android.permission.BIND_JOB_SERVICE" />

</application>

</manifest>

MyJobService 类

public class MyJobService extends JobService {

@Override
public boolean onStartJob(final JobParameters jobParameters) {
Toast.makeText(getApplicationContext(), "Doing job", Toast.LENGTH_SHORT).show();
jobFinished(jobParameters, true);
return false;
}

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

这是我的主 Activity

public class MainActivity extends AppCompatActivity {

private static final int JOB_ID = 1;

private JobInfo jobInfo;
private JobScheduler scheduler;

private Button navigateButton;

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

ComponentName componentName = new ComponentName(this, MyJobService.class);
JobInfo.Builder builder = new JobInfo.Builder(JOB_ID, componentName);
builder.setPeriodic(5000);
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
// if true this job exists even after a system reboot...
builder.setPersisted(false);


jobInfo = builder.build();

scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
scheduler.schedule(jobInfo);

navigateButton = (Button) findViewById(R.id.navigate_button);

navigateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
StringBuffer url = new StringBuffer("https://www.google.com/maps/dir/?api=1");
url.append("&origin=23.755736,90.374627");
url.append("&destination=23.754047,90.371682");
url.append("&travelmode=driving");
Uri gmmIntentUri = Uri.parse(url.toString());
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
}
});

}

@Override
protected void onDestroy() {
Toast.makeText(getApplicationContext(), "Destroy called.", Toast.LENGTH_SHORT).show();
scheduler.cancel(JOB_ID);
super.onDestroy();
}

}

最佳答案

我认为您需要覆盖以下 onStop() 方法并放置 stopService() 命令来停止 JobService。

@Override
protected void onStop() {
// A service can be "started" and/or "bound". In this case, it's "started" by this Activity
// and "bound" to the JobScheduler (also called "Scheduled" by the JobScheduler). This call
// to stopService() won't prevent scheduled jobs to be processed. However, failing
// to call stopService() would keep it alive indefinitely.
stopService(new Intent(this, MyJobService.class));
super.onStop();
}

关于java - 当应用程序从后台删除时如何停止 JobService Scheduled?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48200545/

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