【问题标题】:Not allowed to bind to service Intent不允许绑定到服务 Intent
【发布时间】:2012-08-17 13:50:53
【问题描述】:

我写了一个获取天气的android服务,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

【问题讨论】:

  • 看起来不错。您是否尝试过清理 WeatherService 项目并重新安装它?
  • 亲爱的大卫,我清理、重建和重新安装时也是如此。

标签: android service


【解决方案1】:

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

更新:为避免问题,两个应用都应在其清单中定义权限。

【讨论】:

  • 什么权限?
  • @user9599745 在中声明的自定义服务权限
【解决方案2】:

Here 从文档中说:

如果我们想让这个服务在远程进程中运行(而不是 其.apk 的标准),我们可以在其中使用android:process manifest标签指定一个:

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

还要注意,这里选择的名称remote是任意的,如果您需要额外的进程,您可以使用其他名称。 : 前缀将名称附加到包的标准进程名称。完成后,客户端现在可以绑定到服务并向其发送消息。请注意,这允许客户端向其注册以接收返回的消息。

编辑1
其次,如果服务元素(在清单中)包含操作字符串,请使用它。例如,如果您的服务声明如下:

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

我在这个问题中看到了:
Unable to start Service Intent

编辑2
我又看到了您的清单。您的清单似乎没有Main/Launcher Activity。在android 3.1 and later 中导致没有可用的服务。In fact forsecurityreason all services,receivers,... that you declare in manifest,will not register unless your App run explicitly by user 这需要一个 Main/Launcher Activity。所以你必须将这样的 Activity 添加到你的应用程序中,并注意它已经被执行了。

【讨论】:

  • 将服务的 onCreate 更改为活动。无论如何这根本没有解决我的问题。
【解决方案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")

【讨论】:

  • 这应该可以工作,(在这种情况下,您不需要android:exported="true",因为如果存在意图过滤器,exported 的默认值为true)。但是,发布的代码没有理由不起作用。可以使用显式意图启动服务。
  • 我收到警告消息:W/ActivityManager(131): Unable to start service Intent { act=com.my.weather.WeatherService }: not found
  • @David Wasser 使用导出的属性救了我的命。现在我必须说明为什么 Activity of Service 崩溃了。
【解决方案4】:

在你的活动类中...假设 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);

【讨论】:

  • 这仅在服务和活动在同一进程中时才有效。只有当它们属于同一个应用程序时才有可能。
【解决方案5】:

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

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

【讨论】:

    【解决方案6】:

    David Wasser 在评论中解决了我的问题,所以我想我分享一下。

    在 Manifest 中,您可以将导出的属性设置为 true。这使得其他应用可以访问该服务。

    Manifest.xml

    <service android:name=".MyUsefulService_"
                    android:exported="true"/>
    

    MyActivity.java

    Intent intent = new Intent();
    intent.setComponent(new ComponentName("jav.android.app",jav.android.app.MyUsefulService_");
    
    bindService(this,MyServiceConnection,0);
    

    【讨论】:

      猜你喜欢
      • 2022-06-26
      • 1970-01-01
      • 2018-12-23
      • 1970-01-01
      • 2021-12-13
      • 2018-03-08
      • 1970-01-01
      • 2012-05-06
      • 1970-01-01
      相关资源
      最近更新 更多