Java Android服务不绑定
当我启动一个新的活动时,我也想启动一个服务并绑定它,这样我就可以使用该服务的一些方法。在onStart()方法中,我启动了服务并尝试绑定它。正如你所看到的,当它被绑定时,它会在日志中显示给我。然而,它从来没有 服务本身将始终被创建和启动(我在这两种方法中都添加了Java Android服务不绑定,java,android,service,Java,Android,Service,当我启动一个新的活动时,我也想启动一个服务并绑定它,这样我就可以使用该服务的一些方法。在onStart()方法中,我启动了服务并尝试绑定它。正如你所看到的,当它被绑定时,它会在日志中显示给我。然而,它从来没有 服务本身将始终被创建和启动(我在这两种方法中都添加了Log.d(..)) 清单文件: @Override protected void onStart(){ super.onStart(); Intent musicIntent = new Intent( this, Mu
Log.d(..)
)
清单文件:
@Override
protected void onStart(){
super.onStart();
Intent musicIntent = new Intent( this, MusicService.class );
startService(musicIntent);
getApplicationContext().bindService( musicIntent, mConnection, BIND_AUTO_CREATE);
}
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
mBound = false;
mService = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mBound = true;
LocalBinder binder = (LocalBinder) service;
mService = binder.getService();
Log.d(TAG, "Bound");
}
};
问题出在onBind()
方法中。为了绑定到服务,您需要将binder的实例(不是null
)返回为
问题出在onBind()
方法中。为了绑定到服务,您需要将binder的实例(不是null
)返回为
问题出在onBind()
方法中。为了绑定到服务,您需要将binder的实例(不是null
)返回为
问题出在onBind()
方法中。为了绑定到服务,您需要将binder的实例(不是null
)返回为
您返回的活页夹为空。将此添加到您的服务中
public class LocalService extends Service {
// Binder given to clients
private final IBinder mBinder = new LocalBinder();
/**
* Class used for the client Binder. Because we know this service always
* runs in the same process as its clients, we don't need to deal with IPC.
*/
public class LocalBinder extends Binder {
LocalService getService() {
// Return this instance of LocalService so clients can call public methods
return LocalService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
}
文档链接:您返回的活页夹为空。将此添加到您的服务中
public class LocalService extends Service {
// Binder given to clients
private final IBinder mBinder = new LocalBinder();
/**
* Class used for the client Binder. Because we know this service always
* runs in the same process as its clients, we don't need to deal with IPC.
*/
public class LocalBinder extends Binder {
LocalService getService() {
// Return this instance of LocalService so clients can call public methods
return LocalService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
}
文档链接:您返回的活页夹为空。将此添加到您的服务中
public class LocalService extends Service {
// Binder given to clients
private final IBinder mBinder = new LocalBinder();
/**
* Class used for the client Binder. Because we know this service always
* runs in the same process as its clients, we don't need to deal with IPC.
*/
public class LocalBinder extends Binder {
LocalService getService() {
// Return this instance of LocalService so clients can call public methods
return LocalService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
}
文档链接:您返回的活页夹为空。将此添加到您的服务中
public class LocalService extends Service {
// Binder given to clients
private final IBinder mBinder = new LocalBinder();
/**
* Class used for the client Binder. Because we know this service always
* runs in the same process as its clients, we don't need to deal with IPC.
*/
public class LocalBinder extends Binder {
LocalService getService() {
// Return this instance of LocalService so clients can call public methods
return LocalService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
}
文档链接:LogCat中是否有任何内容?请注意,有时,诸如服务绑定之类的“后台”问题会记录为警告严重性,而不是错误。还有,为什么您同时使用
startService()
和bindService()
?@commonware我在某处读到,我必须先启动服务,然后再创建它。LogCat中没有任何内容:/“我在某处读到我必须先启动服务,然后再创建它”--不,bindService()
withBIND\u AUTO\u create
将创建尚未运行的服务。service.onBind()方法中的代码是什么?@beworker我发布了LogCat中有什么内容吗?请注意,有时,诸如服务绑定之类的“后台”问题会记录为警告严重性,而不是错误。还有,为什么您同时使用startService()
和bindService()
?@commonware我在某处读到,我必须先启动服务,然后再创建它。LogCat中没有任何内容:/“我在某处读到我必须先启动服务,然后再创建它”--不,bindService()
withBIND\u AUTO\u create
将创建尚未运行的服务。service.onBind()方法中的代码是什么?@beworker我发布了LogCat中有什么内容吗?请注意,有时,诸如服务绑定之类的“后台”问题会记录为警告严重性,而不是错误。还有,为什么您同时使用startService()
和bindService()
?@commonware我在某处读到,我必须先启动服务,然后再创建它。LogCat中没有任何内容:/“我在某处读到我必须先启动服务,然后再创建它”--不,bindService()
withBIND\u AUTO\u create
将创建尚未运行的服务。service.onBind()方法中的代码是什么?@beworker我发布了LogCat中有什么内容吗?请注意,有时,诸如服务绑定之类的“后台”问题会记录为警告严重性,而不是错误。还有,为什么您同时使用startService()
和bindService()
?@commonware我在某处读到,我必须先启动服务,然后再创建它。LogCat中没有任何内容:/“我在某处读到我必须先启动服务,然后再创建它”--不,bindService()
withBIND\u AUTO\u create
将创建尚未运行的服务。service.onBind()方法中的代码是什么?@beworker我发布了它