Question

I have 2 different apps in 2 different Application Projects in Eclipse. I will call them H and G. Until now, I have been able to launch H from G by using

Intent intent = getPackageManager().getLaunchIntentForPackage("com.xxx.h");
startActivity(intent);

This has been working great, but in reality, I don't really need the H app to launch, I just need to do something with that app in the background, so I started looking into services. So now I am trying to use

Intent intent = new Intent();
intent.setClassName("com.xxx.h","com.xxx.h.MyService");
startService(intent);

But now I am getting the error saying

W/ActivityManager(1044): Unable to start service Intent { cmp=com.xxx.h/.MyService } U=0: not found

Very new to Services and even Intents so I am guessing it is something simple that I am missing and hoping you guys can help.

EDIT

I fixed the issue with is saying that I was unable to start service intent. That was fixed by including

service android:name=".MyService"

to the AndoirdManifest.xml in H. Now I am getting

E/AndroidRuntime(1022): java.lang.IllegalStateException: Could not execute method of the activity

EDIT 2

Found out it was not letting me start without permission so I had to include android:exported="true" in the AndroidManifest.xml as well

Was it helpful?

Solution 2

The initial problem of it being unable to start the service was because I did not include the <service> to the AndroidManifest.xml which fixed that issue, and then the could not execute error was caused by a permissions issue. So all in all, all it took to fix my issues was including

<service 
    android:name=".MyServiceHomework"
    android:exported="true"
/>

To the AndroidManifest.xml of my H (the service) project

OTHER TIPS

Try this:

private void startService(String aServiceName) {

    if (aServiceName.trim().length() > 0) {
        try {
            Context ctx = getApplicationContext();
            Intent iServiceIntent = this.ctx.getPackageManager().getLaunchIntentForPackage(aServiceName);
            ctx.startActivity(iServiceIntent);

            Thread.sleep(800);
        } catch (Exception e) {

        }           
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top