Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start android service from another Android app

Tags:

I'm having a problem starting a service from another Android app (API 17). However, if I do run 'am' from the shell, the service starts fine.

# am startservice com.xxx.yyy/.SyncService Starting service: Intent { act=android.intent.action.MAIN cat= [android.intent.category.LAUNCHER] cmp=com.xxx.yyy/.SyncService } (service starts fine at this point) # am to-intent-uri com.xxx.yyy/.SyncService intent:#Intent;action=android.intent.action.MAIN; category=android.intent.category.LAUNCHER; component=com.xxx.yyy/.SyncService;end 

So, it doesn't look like I'm missing anything from the intent when I do the same in the code:

Intent i = new Intent(); i.setAction(Intent.ACTION_MAIN); i.addCategory(Intent.CATEGORY_LAUNCHER); i.setComponent(new ComponentName("com.xxx.yyy", ".SyncService")); ComponentName c = ctx.startService(i); if (c == null) { Log.e(TAG, "failed to start with "+i); } 

What I get is (the service is not running at that time):

E/tag( 4026): failed to start with Intent {  act=android.intent.action.MAIN  cat=[android.intent.category.LAUNCHER]  cmp=com.xxx.yyy/.SyncService } 

I don't have an intent filter on the service, and I don't want to set one up, I'm really trying to understand what am I doing wrong starting it through its component name, or what may be making it impossible to do so.

like image 469
Pawel Veselov Avatar asked Jun 26 '13 09:06

Pawel Veselov


People also ask

How do I start a service from another app?

You should be able to start your service like this: Intent i = new Intent(); i. setComponent(new ComponentName("com. xxx.

Can an Android app launch another app?

Using intents even allows your app to start an activity that is contained in a separate app. An Intent can be explicit in order to start a specific component (a specific Activity instance) or implicit in order to start any component that can handle the intended action (such as "capture a photo").

How do I redirect an app to another Android?

You need to raise a intent, try like this: Uri mUri = Uri. parse("market://details?id=" + packageName); Intent mIntent = new Intent(Intent. ACTION_VIEW, mUri); startActivity(marketIntent);


1 Answers

You should be able to start your service like this:

Intent i = new Intent(); i.setComponent(new ComponentName("com.xxx.yyy", "com.xxx.yyy.SyncService")); ComponentName c = ctx.startService(i); 

You don't need to set ACTION or CATEGORY if you are specifying a specific component. Make sure that your service is properly defined in the manifest.

like image 81
David Wasser Avatar answered Sep 24 '22 00:09

David Wasser