如何绑定到运行Android服务?绑定、Android

2023-09-12 04:40:48 作者:后来烂人醉

我希望这是更大的问题,code的比什么都重要,我希望有人在那里可以帮助追查问题。

我有其他的code,它开始startService(服务),我可以验证该服务已启动的调试器命中德coderService的OnCreate()函数。

然而,bindService从未绑定到正在运行的服务。这是一个异步调用,我不得不等待事情完成?

 公共类ResultsActivity延伸活动{

保护无效onResume(){
    // TODO自动生成方法存根
    super.onResume();

    意图去coderIntent =新的意图(这一点,德coderService.class);
    _isBound = bindService(德coderIntent,mConnection,Context.BIND_AUTO_CREATE);
    _textView.start(_mBoundService);
}

私人布尔_isBound = FALSE;
私人德coderService _mBoundService;

私人ServiceConnection mConnection =新ServiceConnection(){
    公共无效onServiceConnected(组件名的className,服务的IBinder){
        _mBoundService =((德coderService.LocalBinder)服务).getService();
    }

    公共无效onServiceDisconnected(组件名的className)
        _mBoundService = NULL;
    }
};
}

    公共类德coderService延伸服务{


保护无效onHandleIntent(意向意图){
    // TODO自动生成方法存根
    _numCompleted = 5;
    _numTotal = 100;
}

保护INT _numCompleted = 0;
保护INT _numTotal = 0;


公共无效的onCreate(){
    onHandleIntent(空);
}


@覆盖
公众的IBinder onBind(意向意图){
    返回mBinder;
}


//这是接收来自客户端的交互的对象。看到
// RemoteService一个更完整的例子。
私人最终的IBinder mBinder =新LocalBinder();

公共类LocalBinder扩展粘合剂{
    德coderService _ref = NULL;
    公共LocalBinder()
    {
        _ref =德coderService.this;
    }
    德coderService的getService(){
        返回德coderService.this;
    }

}
}
 

解决方案   

这是一个异步调用,我   要等待的东西   完成?

的结合还没有准备好,直到 onServiceConnected()叫上你的 ServiceConnection 对象。

Android 服务启动和绑定

I'm hoping this is more of an issue with code than anything else and I'm hoping that someone out there can help track down the issue.

I have other code that starts the service with startService() and I can verify that the service is started as the debugger hits the onCreate() function of DecoderService.

However, the bindService never binds to the running service. Is this an asynchronous call and I have to wait for something to complete?

public class ResultsActivity extends Activity {

protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();

    Intent decoderIntent = new Intent(this,DecoderService.class);
    _isBound = bindService(decoderIntent,mConnection,Context.BIND_AUTO_CREATE);
    _textView.start(_mBoundService);
}

private boolean _isBound = false;
private DecoderService _mBoundService;

private ServiceConnection mConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder service) {
        _mBoundService = ((DecoderService.LocalBinder)service).getService();
    }

    public void onServiceDisconnected(ComponentName className)
        _mBoundService = null;
    }
};
}

    public class DecoderService extends Service {


protected void onHandleIntent(Intent intent) {
    // TODO Auto-generated method stub
    _numCompleted = 5;
    _numTotal = 100;
}

protected int _numCompleted = 0;
protected int _numTotal = 0;


public void onCreate() {
    onHandleIntent(null);
}


@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}


// This is the object that receives interactions from clients.  See
// RemoteService for a more complete example.
private final IBinder mBinder = new LocalBinder();

public class LocalBinder extends Binder {
    DecoderService _ref = null;
    public LocalBinder()
    {
        _ref = DecoderService.this;
    }
    DecoderService getService() {
        return DecoderService.this;
    }

}
}

解决方案

Is this an asynchronous call and I have to wait for something to complete?

The binding is not ready until onServiceConnected() is called on your ServiceConnection object.