android按名称启动服务
我的目标是制作3个APK(用于非常特定的解决方案,而不是用于正常消费):android按名称启动服务,android,android-intent,android-service,Android,Android Intent,Android Service,我的目标是制作3个APK(用于非常特定的解决方案,而不是用于正常消费): 第一个包含启动时启动的服务 使用此服务的客户端 使用此服务的另一个客户端 根据该网站: 我只需知道服务的名称就可以启动它。但是,这不起作用: // works context.startService(new Intent(context, MyServiceMessenger.class)); // does not work Intent serviceIntent = new Intent(); serviceI
// works
context.startService(new Intent(context, MyServiceMessenger.class));
// does not work
Intent serviceIntent = new Intent();
serviceIntent.setAction(MyService.SERVICE_CLASS_NAME);
context.startService(serviceIntent);
要说出MyService.SERVICE\u CLASS\u名称=mypackage.myserviceessenger
我的问题是绑定——我需要绑定到类外的服务
// does not work
Intent intent = new Intent(context, MyService.SERVICE_CLASS_NAME);
// works
Intent intent = new Intent(context, MyServiceMessenger.class);
intent.putExtra("MESSENGER", messenger);
context.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
按名称绑定到服务时,我的serviceConnection
根本没有被使用,我无法向服务发送信息
我错过了什么
编辑1:
我在舱单上遗漏了一个出口。您需要“导出”它,并给它一个名称。我也同意了。我在意图中使用的字符串与@@中的字符串相同
<permission android:name="MyPermission"
android:permissionGroup="android.permission-group.SYSTEM_TOOLS"
android:protectionLevel="dangerous" />
<uses-permission android:name="MyPermission" />
<application ... >
<service
android:name="service.MyServiceMessenger"
android:exported="true"
android:permission="MyPermission" >
<intent-filter>
<action android:name="**platinum8.service.P2PServiceMessenger**"></action>
</intent-filter>
</service>
</application>
编辑2:
我添加了一个与服务名称相同的意图过滤器,现在我的代码似乎可以工作了 您的
元素上可能缺少一个
,您在该元素中声明了您试图让服务响应的
,例如:
<service android:name=".BshService">
<intent-filter>
<action android:name="com.commonsware.android.advservice.IScript" />
</intent-filter>
</service>
@elcuco:
android:exported
仅当您没有
时才需要。请添加符合您的意图的
。