How to use an implicit intent to start a service from another package/app?

Viewed 76

I'm trying to introduce code to the open source app Gadgetbridge for starting services from other packages/apps.

I'm trying to get it working with sending implicit intents to play/pause the music app Poweramp:

Action: com.maxmpz.audioplayer.API_COMMAND Extra: cmd:1 Target: Service

From this page.

I have verified that using the intent info above works by launching it with the automation app Tasker. But so far I have not managed to implement it in Gadgetbridge.

This is part of the code I'm working on now:

case "servicetest": // only for trying to get service intents going,
        Intent inServiceTest = new Intent();
        inServiceTest.setAction("com.maxmpz.audioplayer.API_COMMAND");
        inServiceTest.putExtra("cmd", (int)1); //for trying poweramp service intent play/pause
        this.getContext().getApplicationContext().startService(inServiceTest);
        break;
case "foregroundservicetest": // only for trying to get service intents going,
        Intent inServiceTestFg = new Intent();
        inServiceTestFg.setAction("com.maxmpz.audioplayer.API_COMMAND");
        inServiceTestFg.putExtra("cmd", (int)1); //for trying poweramp service intent play/pause
        this.getContext().getApplicationContext().startForegroundService(inServiceTestFg);
        break;

I've added <uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" /> to AndroidManifest.xml.

The code above doesn't play or pause Poweramp. I initiate the respective parts of the code block by sending a message from my Bangle.js 2 watch to Gadgetbridge containing the code word "servicetest" or "foregroundservicetest".

I'm testing on a Xiaomi Mi A2 Lite running Android 10.

I've taken some pointers from this stackoverflow question. And I've also tried suggestions from here.

How can I modify the code to make it start the service for controlling Poweramp with the mentioned implicit intent?

EDIT: By specifying a package for the intent, i.e. making it explicit, it can control Poweramp. I add this:

inServiceTestFg.setPackage("com.maxmpz.audioplayer");

I however still wonder how I can achieve this with an implicit intent like Tasker seems to be able to. Or is it that Tasker sets package information without the user specifying it?

0 Answers
Related