不允许绑定到服务意向(Not allowed to bind to service Intent)

2019-06-27 02:52发布

我写获取天气的机器人服务和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" >
        </service>
    </application>

</manifest>

现在,我希望让其他APK启动此服务:

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

而我得到的错误信息:

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>

我用这个方法来启动:

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

然后我得到的警告消息:

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

Answer 1:

原 在这里的文件说:
如果我们想使这个服务在远程过程(而不是标准之一的.apk文件)中运行,我们可以使用Android:过程在其清单标记来指定一个:

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

还要注意的是名remote此处选择是任意的,如果你想要更多的过程,你可以使用其他名称。 的:前缀追加名到您的包的标准过程name.With做完这些,客户现在可以绑定到服务和发送消息给它。 请注意,这允许客户端用它注册以接收消息发回也是如此。

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

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

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

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

我看到,在这个问题:
无法启动服务意向

EDIT2:
我看到您的清单again.It看来你的清单没有Main/Launcher Activity 。在android 3.1 and later它不会造成服务可用。 In fact for security reason all services,receivers,... that you declare in manifest,will not register unless your App run explicitly by user和Activity.So你有这样的活动添加到您的应用程序,这需要一个主/启动是因为它已经进行护理。



Answer 2:

我有同样的问题。 对我来说,问题是,我第一次使用所提供的API,该API,然后应用程序安装的应用程序。 卸载/重新安装第一个应用程序解决了这个问题。

更新:要避免该问题的应用程序应该定义在其清单的权限。



Answer 3:

加入意向过滤器的作用,以你的WeatherService:

<service
    android:name=".WeatherService"
    android:exported="true" >
    <intent-filter>
        <action android:name="this.is.my.custom.ACTION" />
    </intent-filter>
</service>

然后,当你去用bindService()在你的其他应用程序绑定,使用此意图:

new Intent("this.is.my.custom.ACTION")


Answer 4:

在乌尔活动课做...假设BothButton是活动类名和CleverTexting是服务类的名字,和U希望从服务和比活度服务调用的活动。 如果我们的代码在4.0以上版本的Android做工精细不是U可以这样做不会有任何问题。

CleverTexting mService ; 

public void setService(CleverTexting listener) {
    // TODO Auto-generated method stub
     mService = listener;
}

在乌拉圭回合Service类做...其中u想打电话给乌尔活动

BothButton mBothButton;

mBothButton = new BothButton(this);
    mBothButton.setService(this);


Answer 5:

当你调用bindService的远程服务,你应该设置你的packageName了。

Intent intent = new Intent("com.my.weather.WeatherService");
intent.setPackage("com.my.weather");
bindService(intent, serConn, Context.BIND_AUTO_CREATE);


文章来源: Not allowed to bind to service Intent