In package com.example.project.packageA I have a class which extends the JobIntentService defines as follows:
public class MyService extends JobIntentService {
static final int JOB_ID = 1000;
static final String TAG =MyService.class.getSimpleName();
public static void enqueueWork(Context context, Intent work) {
enqueueWork(context, MyService.class, JOB_ID, work);
}
@Override
public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onDestroy() {
super.onDestroy();
}
/**
* We have received work to do. The system or framework is already
* holding a wake lock for us at this point, so we can just go.
*/
@Override
protected void onHandleWork(Intent intent) {
Log.i(TAG, "Started onHandleWork");
String value = intent.getExtras().getString("Key");
Log.i(TAG, value);
}
}
In another package: com.example.project.packageB; I want to call this service to start in the background, so I did it as:
Intent it = new Intent();
it.setComponent(new ComponentName("com.example.project", "com.example.project.packageA.MyService"));
it.putExtra("Key", "Value");
MyService.enqueueWork(context, it);
Log.d(TAG, "Call successful");
I also included in the manifest file the following permission:
<service
android:name="com.example.project.MyService"
android:permission="android.permission.BIND_JOB_SERVICE"
android:exported="true"/>
However, when I run my program it doesn't look like the service is started. I can see the log message "Call successful", but I can't see the log messages from the onHandleWork function. Am I starting the service in a wrong way?