有许多具有相同标题的问题,但它们没有帮助。所以我在问。
我有一项服务在我的第一个活动中成功启动。切换到第二个活动时,我需要bind
使用服务。但onServiceConnected
从未调用过。
(注意:我在一个emulator
,并使用这个例子来实现绑定)
活动:
public class GameActivity extends NativeActivity implements GroupInfoListener
{
public SimmobBroker broker;// <- this is my service
ServiceConnection cnn= new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name,
IBinder service) {
Log.i(TAG,"onServiceConnected");
SimmobBrokerBinder binder = (SimmobBrokerBinder) service;
broker = binder.getService();
Log.i(TAG,"onServiceConnected...now initializing the activity");
InitializeActivity();
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, SimmobBroker.class);
getApplicationContext().bindService(intent, cnn, getApplicationContext().BIND_AUTO_CREATE);
// InitializeActivity();
}
};
服务:
public class SimmobBroker extends Service {
@Override
public void onCreate() {
Log.i(TAG, "SimmobBroker Created");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "SimmobBroker onStartCommand");
myHandler.post(simmobConnect);
return START_STICKY;
}
@SuppressLint("HandlerLeak")
public final Handler myHandler = new Handler() {/*...*/}
private final IBinder myBinder = new SimmobBrokerBinder();
public class SimmobBrokerBinder extends Binder {
public SimmobBroker getService() {
return SimmobBroker.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return myBinder;
}
};
谢谢你的热心帮助。