有许多相同标题的问题,但它们没有帮助。所以我在问你。
我有一项服务在我的第一次活动中成功地开始了。当切换到第二个活动时,我需要使用服务进行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;
}
};谢谢你的帮助。
发布于 2016-02-01 22:28:12
尝试直接调用bindService,而不是从应用程序上下文调用。
bindService(intent, cnn, Context.BIND_AUTO_CREATE)https://stackoverflow.com/questions/22852444
复制相似问题