I'm learning Android programming by creating an android app. But whenever I kill the application service also gets killed. I'm using JobIntentService
. To make that app work in the background.
JobIntentService Class
public class BackGroundDistanceCalculate extends JobIntentService {
final public static String TAG = "BackGroundDistance";
static void enqueueWork(Context context, Intent job) {
enqueueWork(context, BackGroundDistanceCalculate.class, 1, job);
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "Service Start");
}
@Override
protected void onHandleWork(@NonNull Intent intent) {
Log.d(TAG, "Execution Started");
int i=0;
for(i=0; i<1000; i++) {
SystemClock.sleep(1000);
}
}
@Override
public boolean onStopCurrentWork() {
Log.d(TAG,"Now It's Stopped");
return super.onStopCurrentWork();
}
@Override
public void onDestroy() {
Log.d(TAG,"Now It's Destroyed");
super.onDestroy();
}
}
MainActivity
Intent backCheck = new Intent(MapsActivity.this, BackGroundDistanceCalculate.class);
BackGroundDistanceCalculate.enqueueWork(this, backCheck);
Created this intent to start Activity
in onCreateMethod
Manifest
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<service
android:name=".BackGroundDistanceCalculate"
android:permission="android.permission.BIND_JOB_SERVICE" />
What are the changes that I can make to make service run successfully even when the app is killed? Thanks in advance.