不允许结合服务意图 [英] Not allowed to bind to service Intent

查看:150
本文介绍了不允许结合服务意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写的获取天气的机器人服务和AndroidManifest.xml中是:

 < XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.my.weather
    安卓版code =1
    机器人:VERSIONNAME =1.0>

    <使用-SDK安卓的minSdkVersion =15/>

    <使用-权限的Andr​​oid:名称=android.permission.INTERNET对/>

    <应用
        机器人:图标=@可绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME>
        <服务
            机器人:名称=的WeatherService
            机器人:出口=真正的>
        < /服务>
    < /用途>

< /舱单>
 

现在,我希望让另一个APK启动此服务:

 意图的服务=新意图();
service.setClassName(com.my.weather,com.my.weather.WeatherService);
context.bindService(服务,weatherServiceConnection,Context.BIND_AUTO_CREATE);
 

和我得到的错误信息:

  E / AndroidRuntime(14068):java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.my.test / com.my.test.MainActivity}:java.lang中。 SecurityException异常:不允许绑定到服务意向{CMP = com.my.weather / .WeatherService}
 

我怎样才能解决这个问题?

其他APK是使用Messenger的方法来沟通与气象服务。

=============================================== =================================

谢谢大家!

现在我修改我的全天候服务的Andr​​oidManifest.xml中:

 < XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.my.weather
    安卓版code =1
    机器人:VERSIONNAME =1.0>

    <使用-SDK安卓的minSdkVersion =15/>

    <使用-权限的Andr​​oid:名称=android.permission.INTERNET对/>

    <应用
        机器人:图标=@可绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME>
        <服务
            机器人:名称=的WeatherService
            机器人:出口=真
            机器人:工艺=:远程>
            <意向滤光器>
                <作用机器人:名称=com.my.weather.WeatherService>< /作用>
            &所述; /意图滤光器>
        < /服务>
    < /用途>

< /舱单>
 

和我用这种方法来启动:

 意图的服务=新的意向书(com.my.weather.WeatherService);
context.bindService(服务,weatherServiceConnection,Context.BIND_AUTO_CREATE);
 

然后,我得到的警告消息:

  W / ActivityManager(131):无法启动服务意向{行为= com.my.weather.WeatherService}:未找到
 

解决方案

<一href="http://stackoverflow.com/questions/2880450/providing-a-background-service-for-other-apps/2881548#2881548">here从文档说:
如果我们想使这个服务的远程过程(而不是标准之一的.apk文件)中运行,我们可以用安卓的过程中它明显的标记,指定之一:

 &LT;服务机器人:名称=。app.MessengerService
        机器人:工艺=:远程/&GT;
 

另外请注意,该名称远程在这里选择的是任意的,如果你想要更多的流程,您可以使用其他名称。该 preFIX追加名到您的包的标准过程name.With做完,客户现在可以绑定到服务,并发送消息给它。请注意,这使客户能够在其上注册接收返回的消息也是如此。

EDIT1
其次,如果元素(在清单)包含一个操作字符串,使用it.For例如,如果你的服务声明是这样的:

 &LT;服务机器人:名称=com.sample.service.serviceClass
            机器人:出口=真正的机器人:标签=@字符串/ APP_NAME
            机器人:工艺=:远程&GT;
   &LT;意向滤光器&gt;&lt;作用机器人:名称=com.sample.service.serviceClass&GT;&LT; /作用&gt;
   &所述; /意图滤光器&gt;
&LT; /服务&GT;
 

所以这样做是你的服务的的onCreate()方法:

 公共无效的onCreate(包savedInstanceState){
      super.onCreate(savedInstanceState);
      意向意图=新的意图(com.sample.service.serviceClass);
      this.startService(意向);
}
 

我看到,在这样一个问题:
<一href="http://stackoverflow.com/questions/3439356/unable-to-start-service-intent/3439838#3439838">Unable启动服务意向

EDIT2
我看到你的清单again.It看来你的表现没有主/启动活动。在 Android的3.1和更高版本它导致没有服务是可用的。 其实对 安全 原因,所有的服务,接收器,...您在清单中声明,将不被记录,除非你的应用程序被用户显式运行 的,这需要一个主/启动Activity.So您有这样的活动添加到您的应用程序,并照顾它已经被执行。

I write an android service of get weather, and the AndroidManifest.xml is:

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

Now, I want let another apk to start this service:

Intent service = new Intent();
service.setClassName("com.my.weather", "com.my.weather.WeatherService");
context.bindService(service, weatherServiceConnection, Context.BIND_AUTO_CREATE);

And I got the error message:

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 }

How can I solved this issue?

Other apk is use the Messenger method to communication with weather service.

================================================================================

Thanks all!

Now I modify my weather service's 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>

And I use this method to start:

Intent service = new Intent("com.my.weather.WeatherService");
context.bindService(service, weatherServiceConnection, Context.BIND_AUTO_CREATE);

Then I got the warning message:

W/ActivityManager(131): Unable to start service Intent { act=com.my.weather.WeatherService }: not found

解决方案

hara here says from documentation:
If we want to make this service run in a remote process (instead of the standard one for its .apk), we can use android:process in its manifest tag to specify one:

<service android:name=".app.MessengerService"
        android:process=":remote" />

Also note that the name remote chosen here is arbitrary, and you can use other names if you want additional processes. The : prefix appends the name to your package's standard process name.With that done, clients can now bind to the service and send messages to it. Note that this allows clients to register with it to receive messages back as well.

Edit1:
Second, if the element(in manifest) contains an action string, use it.For example if your service declared like this:

<service android:name="com.sample.service.serviceClass"  
            android:exported="true" android:label="@string/app_name" 
            android:process=":remote">
   <intent-filter><action android:name="com.sample.service.serviceClass"></action>
   </intent-filter>
</service>         

So do this in onCreate() method of your service:

public void onCreate(Bundle savedInstanceState) {    
      super.onCreate(savedInstanceState);  
      Intent intent=new Intent("com.sample.service.serviceClass");  
      this.startService(intent);
}

I saw that in this question:
Unable to start Service Intent

Edit2:
I saw your manifest again.It seems that your manifest has no Main/Launcher Activity.In android 3.1 and later it causes no service be available.In fact forsecurityreason all services,receivers,... that you declare in manifest,will not register unless your App run explicitly by user and this needs to a Main/Launcher Activity.So you have to add such Activity to your App and be care that it has already been performed.

这篇关于不允许结合服务意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆