2016-04-26 60 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);