简体   繁体   English

Android Bind Service每次都返回false

[英]Android Bind Service returns false every time

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... 绑定服务总是为我返回假...有人能告诉我可能出现的错误......

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

} }

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). 通过这种方式,Android将了解该服务将包作为vn.abc.SocketService ,但事实上,在我的代码结构中,我的服务包含com.tung.SocketService包(这里的包只是示例)。 That is the reason why Android can not find the service I declared in the manifest file. 这就是Android无法找到我在清单文件中声明的服务的原因。

One very common case in which bindService() returns false is if the service was not declared in the Manifest. bindService()返回false的一个非常常见的情况是该服务未在Manifest中声明。 In that case you should declare your service in manifest file. 在这种情况下,您应该在清单文件中声明您的服务。

<manifest ... >
   ...
   <application ... >
      <service android:name=".MyService" />
      ...
   </application>
</manifest>

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. 我希望这有助于拯救别人花费我跟踪它的时间。

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-是serviceConnection对象模式

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... 它可能会工作..祝你有美好的一天......

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 包的名称:com.company.proj.server
  • name of the app: speedtest 应用程序名称:speedtest
  • name of the server: syncserver 服务器名称: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 包的名称:com.company.proj.server.speedtest
  • name of the app: speedtest 应用程序名称:speedtest
  • name of the server: syncserver 服务器名称: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”)

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 . 我在github上做了一个简单的例子。 https://github.com/wingsum93/Bind_Service_Example https://github.com/wingsum93/Bind_Service_Example

我有同样的错误,原因是我忘了启动服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM