How to start a service based on a ServiceInfo object?

In my application, I request a list of services that have a specific category in their intent filters. This is normal, I am returning a list containing ResolveInfo objects. In these ResolveInfos, I found the "serviceInfo" field, which should describe the details of the service found.

Now, how can I build an Intent from serviceInfo, which can start the found service?

My code now looks like this:

PackageManager pm = getApplicationContext().getPackageManager(); Intent i = new Intent(); i.setAction("<my custom action>"); i.addCategory("<my custom category>"); List<ResolveInfo> l = pm.queryIntentServices(i, 0); gatherAgentNum = l.size(); if(gatherAgentNum > 0){ for(ResolveInfo info : l){ Intent i2 = new Intent(this, info.serviceInfo.getClass()); i2.putExtra(BaseAgent.KEY_RESULT_RECEIVER, new GatherResult(mHandler)); startService(i2); } } 

This is obviously wrong, "info.serviceInfo.getClass ()" just returns the class of the serviceInfo object. Can anyone help me with this?

thanks

Edit: solution (at least the one I used):

 PackageManager pm = getApplicationContext().getPackageManager(); Intent i = new Intent(); i.setAction("<my action>"); i.addCategory("<my category>"); List<ResolveInfo> l = pm.queryIntentServices(i, 0); if(l.size() > 0){ for(ResolveInfo info : l){ ServiceInfo servInfo = info.serviceInfo; ComponentName name = new ComponentName(servInfo.applicationInfo.packageName, servInfo.name); Intent i2 = new Intent(); i2.setComponent(name); startService(i2); } } 
+6
android service
source share
1 answer
+4
source share

All Articles