我希望这更多的是代码问题,而不是其他问题,我希望有人可以帮助解决这个问题。
我还有其他使用 startService() 启动服务的代码,并且当调试器点击 DecoderService 的 onCreate() 函数时,我可以验证该服务是否已启动。
但是,bindService 永远不会绑定到正在运行的服务。这是一个异步调用,我必须等待某些事情完成吗?
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;
}
}
}
这是一个异步调用吗?
必须等待某件事
完全的?
绑定尚未准备就绪,直到onServiceConnected()
被称为你的ServiceConnection
object.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)