4

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?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Ziva
  • 3,181
  • 15
  • 48
  • 80

4 Answers4

1

In later versions of Android, Google introduced Doze feature to optimize battery life of the device. (not sure from which version they introduced it) you need to add this to your intent to ignore battery optimizations

//disabling battery optimizations

Intent intent = new Intent(this,SeriesService.class);
String packageName = getPackageName();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    if (pm.isIgnoringBatteryOptimizations(packageName))
            intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
    else {      intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
            intent.setData(Uri.parse("package:" + packageName));
    }
startService(intent);

I tried this on my OnePlus 5 (running Android pie) and it works

EDIT

Just in case I'm adding how I'm calling to my service after I already started it

Context con = getView().getContext();//activity context
Intent intent = new Intent(con,SeriesService.class);
intent.setAction(SOME_ACTION);
intent.putExtra(KEY,data);
SeriesService.enqueueWork(con,intent);
Anton Makov
  • 825
  • 2
  • 9
  • 25
0

You don't need to use a component name within your intent. Try removing this line:

it.setComponent(new ComponentName("com.example.project", "com.example.project.packageA.MyService"));

Also your code should be like this:

Intent it = new Intent();
it.putExtra("Key", "Value");
MyService.enqueueWork(context, it);
Log.d(TAG, "Call successful");
Tano
  • 3,620
  • 1
  • 29
  • 31
0

Consider starting it like this;

Intent it = new Intent(context, MyService.class);
it.putExtra("key", "value");
MyService.enqueueWork(context, it);
Ugokoli
  • 91
  • 1
  • 4
0

If you override the method onCreate(), the method onHandleWork() is never called. Just erase the onCreate() method.

Bazouk55555
  • 557
  • 6
  • 24