2016-04-26 52 views
1

我试图做一个RemoteService,我按照这个指南: http://www.techotopia.com/index.php/Android_Remote_Bound_Services_%E2%80%93_A_Worked_Example服务意向必须是明确的:意向

这是在清单我的服务宣言:

<service android:name=".RemoteService" 
     android:process=":InnolertRemoteProcess" 
     android:exported="true"> 
     <intent-filter> 
      <action android:name="myService.RemoteService"/> 
      <category android:name="android.intent.category.DEFAULT"/> 
     </intent-filter> 
    </service> 

这是怎么我绑定到服务,从我的客户端应用程序:

Intent intent = new Intent("myService.RemoteService"); 
bindService(intent, myConnection, Context.BIND_AUTO_CREATE); 

我得到这个异常:

java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=myService.RemoteService } 
+0

你必须使用一个'PackageManager'为了得到明确的'Intent',例如'PackageManager#resolveService'或'PackageManager #queryIntentServices' – pskink

回答

2

我用过这个。

public static Intent createExplicitFromImplicitIntent(Context context, Intent implicitIntent) { 
//Retrieve all services that can match the given intent 
PackageManager pm = context.getPackageManager(); 
List<ResolveInfo> resolveInfo = pm.queryIntentServices(implicitIntent, 0); 

//Make sure only one match was found 
    if (resolveInfo == null || resolveInfo.size() != 1) { 
    return null; 
    } 

//Get component info and create ComponentName 
ResolveInfo serviceInfo = resolveInfo.get(0); 
String packageName = serviceInfo.serviceInfo.packageName; 
String className = serviceInfo.serviceInfo.name; 
ComponentName component = new ComponentName(packageName, className); 

//Create a new intent. Use the old one for extras and such reuse 
Intent explicitIntent = new Intent(implicitIntent); 

//Set the component to be explicit 
explicitIntent.setComponent(component); 

return explicitIntent; 
} 
+0

解决了问题谢谢。 –

2

对于我来说,下一行帮助我: intent.setPackage( “myServicePackageName”);

例如:

Intent intent = new Intent("com.something.REQUEST_SOMETHING"); 
    intent.setPackage("com.something"); 
    ctx.startService(intent); 
2

试试这个:

Intent i = new Intent(); 
i.setAction("myService.RemoteService"); 
i.setPackage("com.your_service_package.name"); 
boolean ret = bindService(i, connection, Context.BIND_AUTO_CREATE); 
Log.d(TAG, "initService() bound with " + ret);