java– 如何停止JobService计划到从后台删除应用程序?

java– 如何停止JobService计划到从后台删除应用程序?,第1张

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

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

当用户按下后退按钮然后在onDestroy方法内部我已经取消了调度程序.但是当通过滑动或按下十字图标从后台移除应用程序时,JobService会继续运行,因为当从后台删除时, *** 作系统可以调用或不调用onDestroy方法.当应用程序从后台删除时,如何停止预定作业?

AndroIDManifest.xml中

<?xml version="1.0" enCoding="utf-8"?><manifest xmlns:androID="@R_403_6822@://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;    }}

这是我的MainActivity

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("@R_403_6822@s://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.

@OverrIDeprotected 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计划到从后台删除应用程序?全部内容,希望文章能够帮你解决java – 如何停止JobService计划到从后台删除应用程序?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1116719.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存