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

Ani*_*Dey 5 java android google-maps jobservice

目前,我正在使用一个应用程序,并且我的应用程序具有用户可以单击“导航”按钮并且我的应用程序将启动Google Map的功能。到现在为止一切都很好,我已经做到了。但是,我被困的事实是我希望我的应用执行某些任务。为此,我使用了JobService并将其安排为每5秒运行一次,即使该应用程序处于后台也是如此。

当用户按下后退按钮时,则在onDestroy方法内部,我已取消了调度程序。但是,当通过滑动或按下十字图标将应用程序从后台删除时,JobService会继续运行,因为从后台删除应用程序时,os可以调用onDestroy方法。从后台删除应用程序后,如何停止计划的工作?

在此处输入图片说明

在此处输入图片说明

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>
Run Code Online (Sandbox Code Playgroud)

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的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("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();
    }

}
Run Code Online (Sandbox Code Playgroud)

May*_*hal 0

您可以创建一个新服务,例如

MyService.java

public class MyService extends Service {
public MyService() {
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}    
@Override
public void onTaskRemoved(Intent rootIntent) {
    super.onTaskRemoved(rootIntent);
    //stop you jobservice from here
    stopSelf();
}
 }
Run Code Online (Sandbox Code Playgroud)

并从 MainActivity.java 启动它

startService(new Intent(MainActivity.this,MyService.class));
Run Code Online (Sandbox Code Playgroud)