java Android Bind Service 每次都返回false
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5307903/
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
Android Bind Service returns false every time
提问by Arun Abraham
boolean isBound = bindService(new Intent(SocketServiceController.this, SocketService.class), mConnection, Context.BIND_AUTO_CREATE);
Bind service always returns false for me... Could anyone tell me the possible errors that i could have made...
绑定服务总是为我返回 false... 谁能告诉我我可能犯的可能错误...
Service code is as follows
服务代码如下
public class SocketService extends Service{
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return myBinder;
}
private final IBinder myBinder = new LocalBinder();
public class LocalBinder extends Binder {
public SocketService getService() {
return SocketService.this;
}
}
@Override
public void onCreate() {
super.onCreate();
}
public void IsBoundable(){
Toast.makeText(this,"Is bound", Toast.LENGTH_LONG).show();
}
public void onStart(Intent intent, int startId){
super.onStart(intent, startId);
Toast.makeText(this,"Service created ...", Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
}
Service Controller code is as follows:
服务控制器代码如下:
public class SocketServiceController extends Activity{
private SocketService mBoundService;
private Boolean mIsBound;
public SocketServiceController ssc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ssc = this;
setContentView(R.layout.telnet);
Button startButton = (Button)findViewById(R.id.button1);
Button endButton = (Button)findViewById(R.id.button2);
Button bindButton = (Button)findViewById(R.id.button3);
startButton.setOnClickListener(startListener);
endButton.setOnClickListener(stopListener);
//bindButton.setOnClickListener(this);
TextView textView = (TextView)findViewById(R.id.textView1);
}
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
mBoundService = ((SocketService.LocalBinder)service).getService();
}
@Override
public void onServiceDisconnected(ComponentName className) {
mBoundService = null;
}
};
private void doBindService() {
boolean isBound = bindService(new Intent(SocketServiceController.this, SocketService.class), mConnection, Context.BIND_AUTO_CREATE);
mIsBound = true;
//mBoundService.IsBoundable();
}
private void doUnbindService() {
if (mIsBound) {
// Detach our existing connection.
unbindService(mConnection);
mIsBound = false;
}
}
private OnClickListener startListener = new OnClickListener() {
public void onClick(View v){
startService(new Intent(SocketServiceController.this,SocketService.class));
doBindService();
}
};
private OnClickListener stopListener = new OnClickListener() {
public void onClick(View v){
stopService(new Intent(SocketServiceController.this,SocketService.class));
}
};
@Override
protected void onDestroy() {
super.onDestroy();
doUnbindService();
}
}
}
采纳答案by JoshuaBlr
I think the problem might be while binding the service.I m using the following code to bind the service.Its returning true properly.
我认为问题可能出在绑定服务时。我正在使用以下代码绑定服务。它正确返回 true。
boolean flag=bindService(mService, mConnection, MODE_PRIVATE);
mService -is the service object, mConnection- is serviceConnection object Mode
mService - 是服务对象,mConnection- 是服务连接对象模式
There might be a small change in your code
您的代码可能有一些小的变化
boolean isBound = bindService(mBoundService, mConnection, Context.BIND_AUTO_CREATE);
It might work.. Have a great day...
它可能会起作用..祝你有美好的一天......
回答by Xuan Tung Vu
I had the same problem. After a time of studying, I found out that our application does not know which service to be bound. This is because either we didn't declare the service in the manifest file, or we declared it in the wrong way.
我有同样的问题。经过一段时间的研究,我发现我们的应用程序不知道要绑定哪个服务。这是因为我们没有在清单文件中声明服务,或者我们以错误的方式声明了它。
In my case, I declare it as:
就我而言,我将其声明为:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="vn.abc"
.................
<service android:name=".SocketService" >
</service>
By this way, Android will understand that the service has the package as vn.abc.SocketService
, but in fact, in my code structure, my service has the package com.tung.SocketService
(packages here are just examples). That is the reason why Android can not find the service I declared in the manifest file.
这样,Android 就会明白服务有包为vn.abc.SocketService
,但实际上在我的代码结构中,我的服务有包com.tung.SocketService
(这里的包只是示例)。这就是为什么 Android 找不到我在清单文件中声明的服务的原因。
回答by DesigningLives
One very common case in which bindService()returns falseis if the service was not declared in the Manifest. In that case you should declare your service in manifest file.
bindService()返回false 的一种非常常见的情况是服务未在 Manifest 中声明。在这种情况下,您应该在清单文件中声明您的服务。
<manifest ... >
...
<application ... >
<service android:name=".MyService" />
...
</application>
</manifest>
回答by David Clamage
I had a similar error. It turned out to be due to the difference between these two blocks of code:
我有一个类似的错误。原来是因为这两个代码块的不同:
@Override
public IBinder onBind(Intent arg0)
{
return new MyBinder();
}
and:
和:
private final IBinder mBinder = new MyBinder();
@Override
public IBinder onBind(Intent arg0)
{
return mBinder;
}
The first block doesn't work, the second block does.
第一个块不起作用,第二个块起作用。
I hope this helps save someone else the hours it took me to track this down.
我希望这有助于节省其他人花费的时间来跟踪它。
回答by AndrewBloom
I solved the problem doing a new project and copying the code in it. Probably the problem was related to package name.
我解决了做一个新项目并复制其中的代码的问题。问题可能与包名称有关。
Strangely on Android Studio I had the following situation (project not working):
奇怪的是,在 Android Studio 上,我遇到了以下情况(项目不起作用):
- name of the package: com.company.proj.server
- name of the app: speedtest
- name of the server: syncserver
- 包名:com.company.proj.server
- 应用名称:speedtest
- 服务器名称:syncserver
This strangely blocked the server to be seen outside the app
这奇怪地阻止了服务器在应用程序之外被看到
In the new project I had (working):
在我有(工作)的新项目中:
- name of the package: com.company.proj.server.speedtest
- name of the app: speedtest
- name of the server: syncserver
- 包名:com.company.proj.server.speedtest
- 应用名称:speedtest
- 服务器名称:syncserver
notice how the name of the app is the last element of the package.
注意应用程序的名称是包的最后一个元素。
The first situation allowed the app to be executed correctly, to send messages to the service but not to access the service from a different app (the flag android:exported="true" was correctly set in both cases)
第一种情况允许应用程序正确执行,向服务发送消息但不能从不同的应用程序访问服务(标志 android:exported="true" 在两种情况下都正确设置)
回答by Eric Ho
i think the bound service flag is setting wrong. you should set the flag in you service connection .
我认为绑定服务标志设置错误。你应该在你的服务连接中设置标志。
private ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
mServiceBound = false;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
BoundService.MyBinder myBinder = (BoundService.MyBinder) service;
mBoundService = myBinder.getService();
mServiceBound = true;
}
};
};
i had done a simple exampl in github . https://github.com/wingsum93/Bind_Service_Example
我在 github 上做了一个简单的例子。 https://github.com/wingsum93/Bind_Service_Example
回答by Arthur
I had the same error and the reason was that I forgot to start the service.
我有同样的错误,原因是我忘记启动服务。