Android 不允许绑定到服务 Intent
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12007139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Not allowed to bind to service Intent
提问by Tony
I write an android service of get weather, and the AndroidManifest.xml is:
我写了一个获取天气的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>
Now, I want let another apk to start this service:
现在,我想让另一个 apk 来启动这个服务:
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.
其他 apk 是使用 Messenger 方法与天气服务通信。
================================================================================
================================================== ==============================
Thanks all!
谢谢大家!
Now I modify my weather service's AndroidManifest.xml:
现在我修改我的天气服务的 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
采纳答案by hasanghaforian
Heresays 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" />
如果我们想让这个服务在一个远程进程中运行(而不是它的 .apk 的标准进程),我们可以
android:process
在它的 manifest 标签中使用来指定一个:<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.
另请注意,remote
此处选择的名称是任意的,如果需要其他进程,可以使用其他名称。该:
前缀追加名到您的包的标准进程名。完成后,客户端现在可以绑定到服务并向其发送消息。请注意,这允许客户端向它注册以接收消息。
Edit1:
Second, if the service element (in manifest) contains an action string, use it. For example if your service declared like this:
Edit1:
第二,如果 service 元素(在清单中)包含操作字符串,请使用它。例如,如果您的服务声明如下:
<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:
所以在onCreate()
你的服务方法中这样做:
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 for
security
reason 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.
Edit2:
我又看到了你的清单。你的清单似乎没有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
回答by plaisthos
I had the same problem. For me the problem was that I first installed the app using the API and then app that provided the API. Uninstalling/Reinstalling the first app solved the problem.
我有同样的问题。对我来说,问题是我首先使用 API 安装了应用程序,然后是提供 API 的应用程序。卸载/重新安装第一个应用程序解决了这个问题。
Update: To avoid the problem bothapps should define the permission in their manifest.
更新:为避免该问题,两个应用程序都应在其清单中定义权限。
回答by CompEng88
Add an intent-filter with an action to your WeatherService:
添加一个带有操作的意图过滤器到您的 WeatherService:
<service
android:name=".WeatherService"
android:exported="true" >
<intent-filter>
<action android:name="this.is.my.custom.ACTION" />
</intent-filter>
</service>
Then, when you go to bind with bindService() in your other app, use this intent:
然后,当您在其他应用程序中使用 bindService() 进行绑定时,请使用以下意图:
new Intent("this.is.my.custom.ACTION")
回答by Rajeev Kashyap
In ur activity class do ... suppose BothButton is Activity class name and CleverTexting is Service class name, And u want to call an activity from a service and than service from activity . If ur code is working fine in more than 4.0 android version than u can do like this there will not be any problem.
在你的活动类中做...假设 BothButton 是 Activity 类名,CleverTexting 是 Service 类名,你想从服务中调用一个活动,而不是从活动中调用服务。如果您的代码在 4.0 以上的 android 版本中运行良好,那么您可以这样做,就不会有任何问题。
CleverTexting mService ;
public void setService(CleverTexting listener) {
// TODO Auto-generated method stub
mService = listener;
}
and in ur Service class do... where u want to call ur activity
在你的服务课上做...你想在那里打电话给你的活动
BothButton mBothButton;
mBothButton = new BothButton(this);
mBothButton.setService(this);
回答by YikFung
When you call bindService for a remote service, you should set your packageName too.
当您为远程服务调用 bindService 时,您也应该设置您的 packageName。
Intent intent = new Intent("com.my.weather.WeatherService");
intent.setPackage("com.my.weather");
bindService(intent, serConn, Context.BIND_AUTO_CREATE);
回答by YikFung
My problem was solved by David Wasser in a comment so I thought I share.
David Wasser 在评论中解决了我的问题,所以我想分享一下。
In the Manifest you can set exported attribute to true. This makes the service accessible to other apps.
在清单中,您可以将导出的属性设置为 true。这使得其他应用程序可以访问该服务。
Manifest.xml
清单文件
<service android:name=".MyUsefulService_"
android:exported="true"/>
MyActivity.java
我的活动.java
Intent intent = new Intent();
intent.setComponent(new ComponentName("jav.android.app",jav.android.app.MyUsefulService_");
bindService(this,MyServiceConnection,0);