Android                
            ジョブスケジューリング
        
        
            
    サーチ…
備考
 onStartJob()ように、多くのコードを実行したり、 JobService内で大量の作業をしたりすることには注意してください。このコードはメイン/ UIスレッドで実行されるため、ブロックされたUI、応答していないアプリケーション、またはアプリケーションのクラッシュにつながる可能性があります。 
そのため、たとえばThreadまたはAsyncTaskを使用して作業をオフロードする必要があります。 
基本的な使用法
新しいJobServiceを作成する
これは、 JobServiceクラスを拡張し、必要なメソッドをonStartJob()およびonStopJob()実装またはオーバーライドすることによって行われます。 
public class MyJobService extends JobService
{
    final String TAG = getClass().getSimpleName();
    
    @Override
    public boolean onStartJob(JobParameters jobParameters) {
        Log.i(TAG, "Job started");
        // ... your code here ...
        
        jobFinished(jobParameters, false);  // signal that we're done and don't want to reschedule the job
        return false;                       // finished: no more work to be done
    }
    @Override
    public boolean onStopJob(JobParameters jobParameters) {
        Log.w(TAG, "Job stopped");
        return false;
    }
}
 AndroidManifest.xmlに新しいJobServiceを追加します。
次の手順は必須です 。そうしないと、ジョブを実行できなくなります。
 AndroidManifest.xmlの <application> </application>間に新しい<service>要素としてMyJobServiceクラスを宣言します。 
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        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:permission="android.permission.BIND_JOB_SERVICE" />
    </application>
</manifest>
 ジョブのセットアップと実行
新しいJobServiceを実装してAndroidManifest.xmlに追加したら、最後の手順を続行できます。
-  
onButtonClick_startJob()は、定期的なジョブを準備して実行します。JobInfo.Builderでは、定期的なジョブ以外にも、他の多くの設定や制約を指定できます。たとえば、ジョブを実行するために充電器またはネットワーク接続が差し込まれていることを定義できます。 -  
onButtonClick_stopJob()は、実行中のすべてのジョブをキャンセルします。 
public class MainActivity extends AppCompatActivity
{
    final String TAG = getClass().getSimpleName();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void onButtonClick_startJob(View v) {
        // get the jobScheduler instance from current context
        JobScheduler jobScheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
        // MyJobService provides the implementation for the job
        ComponentName jobService = new ComponentName(getApplicationContext(), MyJobService.class);
        // define that the job will run periodically in intervals of 10 seconds
        JobInfo jobInfo = new JobInfo.Builder(1, jobService).setPeriodic(10 * 1000).build();
        // schedule/start the job
        int result = jobScheduler.schedule(jobInfo);
        if (result == JobScheduler.RESULT_SUCCESS)
            Log.d(TAG, "Successfully scheduled job: " + result);
        else
            Log.e(TAG, "RESULT_FAILURE: " + result);
    }
    public void onButtonClick_stopJob(View v) {
        JobScheduler jobScheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
        Log.d(TAG, "Stopping all jobs...");
        jobScheduler.cancelAll(); // cancel all potentially running jobs
    }
}
  onButtonClick_startJob()呼び出した後、アプリケーションは一時停止状態(ユーザーがホームボタンを押してアプリケーションが表示されなくなっても)であっても、約10秒間隔で実行されます。 
 onButtonClick_stopJob()内の実行中のジョブをすべてキャンセルする代わりに、 jobScheduler.cancel()を呼び出して、ジョブIDに基づいて特定のジョブをキャンセルすることもできます。 
Modified text is an extract of the original Stack Overflow Documentation
        ライセンスを受けた CC BY-SA 3.0
        所属していない Stack Overflow