The JobIntentService
might be the choice. It's not activity-related, will not be destroyed with an activity.
How to use:
- Manifest,
<uses-permission android:name="android.permission.WAKE_LOCK" />
<service android:name=".YourService" android:permission="android.permission.BIND_JOB_SERVICE" />
- Create the service:
public class YourService extends JobIntentService {
public static final int JOB_ID = 1;
public static void enqueueWork(Context context, Intent work) {
enqueueWork(context, YourService.class, JOB_ID, work);
}
@Override
protected void onHandleWork(@NonNull Intent intent) {
// Your jobs are here
}
}
- Start the job somewhere:
YourService.enqueueWork(context, new Intent());
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…