你能给我一个带有后台服务的应用程序的简单示例,它使用绑定(bind)/取消绑定(bind)方法来启动和停止它吗?我在谷歌上搜索了半个小时,但这些示例使用 startService/stopService 方法或者对我来说非常困难。谢谢。
最佳答案
您可以尝试使用此代码:
protected ServiceConnection mServerConn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
Log.d(LOG_TAG, "onServiceConnected");
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d(LOG_TAG, "onServiceDisconnected");
}
}
public void start() {
// mContext is defined upper in code, I think it is not necessary to explain what is it
mContext.bindService(intent, mServerConn, Context.BIND_AUTO_CREATE);
mContext.startService(intent);
}
public void stop() {
mContext.stopService(new Intent(mContext, ServiceRemote.class));
mContext.unbindService(mServerConn);
}
关于android - 绑定(bind)/取消绑定(bind)服务示例 (android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8341667/