Android 不允许绑定到服务意图
我编写了一个android服务get weather,AndroidManifest.xml是:Android 不允许绑定到服务意图,android,service,Android,Service,我编写了一个android服务get weather,AndroidManifest.xml是: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.my.weather" android:versionCode="1" android:versionName="1.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.weather"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<service
android:name=".WeatherService"
android:exported="true" >
</service>
</application>
</manifest>
我得到了错误信息:
E/AndroidRuntime(14068): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my.test/com.my.test.MainActivity}: java.lang.SecurityException: Not allowed to bind to service Intent { cmp=com.my.weather/.WeatherService }
我怎样才能解决这个问题
另一个apk是使用Messenger方法与气象服务进行通信
================================================================================
谢谢大家
现在我修改天气服务的AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.weather"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<service
android:name=".WeatherService"
android:exported="true"
android:process=":remote" >
<intent-filter>
<action android:name="com.my.weather.WeatherService"></action>
</intent-filter>
</service>
</application>
</manifest>
然后我得到了警告信息:
W/ActivityManager(131): Unable to start service Intent { act=com.my.weather.WeatherService }: not found
从文件中可以看出:
如果我们希望在远程进程中运行此服务(而不是
我们可以在its中使用android:process
清单标记以指定一个:
<service android:name=".app.MessengerService"
android:process=":remote" />
因此,请在服务的onCreate()
方法中执行此操作:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent=new Intent("com.sample.service.serviceClass");
this.startService(intent);
}
我在这个问题上看到了:Edit2:
我又看到了你的清单。你的清单似乎没有主/启动器活动。在android 3.1及更高版本中,它导致没有可用的服务。
事实上,出于
安全
原因,所有服务、接收者,。。。您在清单中声明的将不会注册,除非您的应用程序由用户显式运行,并且这需要一个主/启动器活动。因此,您必须将此类活动添加到您的应用程序中,并注意它已被执行。向您的WeatherService添加带有操作的意图筛选器:
<service
android:name=".WeatherService"
android:exported="true" >
<intent-filter>
<action android:name="this.is.my.custom.ACTION" />
</intent-filter>
</service>
我也有同样的问题。对我来说,问题是我首先使用API安装应用程序,然后是提供API的应用程序。卸载/重新安装第一个应用程序解决了问题
更新:为了避免问题两个应用程序都应该在其清单中定义权限。在您的活动类中不。。。假设BothButton是活动类名,CleverTexting是服务类名,并且您希望从服务调用活动,而不是从活动调用服务。如果你的代码在超过4.0的android版本中运行良好,那么你就可以这样做,这不会有任何问题
CleverTexting mService ;
public void setService(CleverTexting listener) {
// TODO Auto-generated method stub
mService = listener;
}
在你的服务课上做。。。你想把你的活动称为什么
BothButton mBothButton;
mBothButton = new BothButton(this);
mBothButton.setService(this);
当您为远程服务调用bindService时,也应该设置packageName
Intent intent = new Intent("com.my.weather.WeatherService");
intent.setPackage("com.my.weather");
bindService(intent, serConn, Context.BIND_AUTO_CREATE);
David Wasser在一篇评论中解决了我的问题,所以我想我和大家分享一下
在清单中,可以将导出属性设置为true。这使得其他应用程序可以访问该服务
Manifest.xml
<service android:name=".MyUsefulService_"
android:exported="true"/>
看起来不错。您是否尝试过清理WeatherService项目并重新安装它?亲爱的David,我清理、重建和重新安装时也是如此。这应该可以工作(在这种情况下,您不需要android:exported=“true”
,因为exported
的默认值是true
,如果存在意图过滤器)。然而,发布的代码没有理由不起作用。可以使用明确的意图启动服务。我收到警告消息:W/ActivityManager(131):无法启动服务意图{act=com.my.weather.WeatherService}:未启动found@David使用导出属性的Wasser救了我的命。现在我必须说明服务活动崩溃的原因。将onCreate服务更改为活动。这根本解决不了我的问题。什么权限?@user9599745 custom service permission在此声明,仅当服务和活动处于同一进程中时才有效。这只有在它们属于同一个应用程序时才可能发生。
CleverTexting mService ;
public void setService(CleverTexting listener) {
// TODO Auto-generated method stub
mService = listener;
}
BothButton mBothButton;
mBothButton = new BothButton(this);
mBothButton.setService(this);
Intent intent = new Intent("com.my.weather.WeatherService");
intent.setPackage("com.my.weather");
bindService(intent, serConn, Context.BIND_AUTO_CREATE);
<service android:name=".MyUsefulService_"
android:exported="true"/>
Intent intent = new Intent();
intent.setComponent(new ComponentName("jav.android.app",jav.android.app.MyUsefulService_");
bindService(this,MyServiceConnection,0);