服务意图必须明确:意图

时间:2016-04-26 12:44:18

标签: android

我正在尝试制作RemoteService,我已按照本指南操作: http://www.techotopia.com/index.php/Android_Remote_Bound_Services_%E2%80%93_A_Worked_Example

这是我在Manifest中的服务声明:

 <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 }

3 个答案:

答案 0 :(得分: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;
 }

答案 1 :(得分:2)

对我来说,下一行帮助我: intent.setPackage(&#34; myServicePackageName&#34);

例如:

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

答案 2 :(得分: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);