我在编写服务时遇到问题,应该可以处理多个活动。 我编写了一个简单的服务和一个mediator类,它可以生成绑定并返回一个服务对象。这是简单的服务类:
public class ServerConnectionService extends Service{
private static final String TAG = "ServerConnectionService";
private final Binder binder=new LocalBinder();
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onDestroy(){
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
return binder;
}
public class LocalBinder extends Binder {
ServerConnectionService getService() {
return ServerConnectionService.this;
}
}
}
这是调解员类:
public class ServiceConnectionBinder{
private ServerConnectionService m_SrvConnection=null;
private ServiceConnection m_OnService;
private boolean m_IsBound;
private Activity m_Client;
public ServiceConnectionBinder(Activity i_Activity)
{
m_IsBound = false;
this.m_Client = i_Activity;
this.m_OnService=new ServiceConnection() {
public void onServiceConnected(ComponentName className,IBinder rawBinder) {
m_SrvConnection=((ServerConnectionService.LocalBinder)rawBinder).getService();
}
public void onServiceDisconnected(ComponentName className) {
m_SrvConnection=null;
}
};
doBindService();
Log.d("ServiceConnectionBinder", "finished Ctor");
}
private void doBindService() {
if(!m_IsBound)
{
m_Client.bindService(new Intent(m_Client, ServerConnectionService.class), m_OnService, Context.BIND_AUTO_CREATE);
m_IsBound = true;
}
if(m_SrvConnection == null)
{
Log.d("ServiceConnectionBinder",".doBindService cannot bind " + ServerConnectionService.class.toString() + " to " + this.toString());
}
}
public void doUnbindService() {
if (m_IsBound) {
// Detach our existing connection.
m_Client.unbindService(m_OnService);
m_IsBound = false;
}
}
public ServerConnectionService getServerConnectionService()
{
if(m_IsBound)
{
Log.d("ServiceConnectionBinder", "getServerConnectionService m_IsBound = " + m_IsBound);
}
return m_SrvConnection;
}
}
客户端Activity具有以下数据成员:
private ServiceConnectionBinder m_SrvcConnectionBinder=null;
private ServerConnectionService m_SrvConnection=null;
在onCreate()中有以下代码:
m_SrvcConnectionBinder = new ServiceConnectionBinder(this);
m_SrvConnection = m_SrvcConnectionBinder.getServerConnectionService();
问题是在onCreate()之后, m_SrvConnection始终为空。
如果您有任何其他方法可以实现这一点,欢迎您分享..
答案 0 :(得分:0)
问题是在onCreate()之后,m_SrvConnection始终为null。
当然。绑定请求甚至不会开始,直到主应用程序线程再次获得控制权(即,您将控制权返回给操作系统)。
在调用m_SrvConnection
之前,您无法使用onServiceConnected()
。
答案 1 :(得分:0)
因为我有类似的问题而复活旧帖子,但这里没有明确的答案。 解决这个问题的方法之一是:
protected void onCreate(Bundle savedInstanceState){
//... some stuff #1...
new AsyncTask<Void, Void, Integer>(){
protected void onPreExecute() { }
protected Integer doInBackground(Void... params) {
while(m_SrvConnection==null);
return new Integer(1);
}
protected void onPostExecute(Integer result) {
// service is up, m_SrvConnection is set
// what you wanted to do with the service in onCreate() goes here
}
}.execute();
//...some stuff #2...
}
请注意,“some stuff#1”将在调用onCreate()时正确运行,“some stuff#2”将在此之后几乎执行,但是你在onPostExecute()中放置的内容将会运行得更晚。 / p>
这样做而不仅仅是将代码放入onServiceConnected()的原因是ServiceConnectionBinder现在可以放在Activity之外(例如在某些单例或Application中),并且可以被多个活动使用而不需要需要每个人都绑定到服务。
注意,它可能并不明显,但是onPostExecute()中的内容可能(将)实际上在所有其他标准回调之后运行(如onResume()等。)。