Not allowed to bind to service Intent

匿名 (未验证) 提交于 2019-12-03 01:48:02

问题:

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

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:

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 

回答1:

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:

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:

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.



回答2:

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.

Update: To avoid the problem both apps should define the permission in their manifest.



回答3:

Add an intent-filter with an action to your WeatherService:

Then, when you go to bind with bindService() in your other app, use this intent:

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


回答4:

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.

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


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!