这是我在做什么:
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
ConnectivityManager conn = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = conn.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null
&& activeNetwork.isConnectedOrConnecting();
if (isConnected == true) {
// initChatHub();
Intent serviceIntent = new Intent(context, ChatService.class);
Toast.makeText(context, "Connected", 1000).show();
serviceIntent.putExtras(intent);
context.startService(serviceIntent);
context.bindService(serviceIntent, mServiceConnection,
Context.BIND_AUTO_CREATE);
} else {
Toast.makeText(context, "Disconnected", 1000).show();
}
}
当网络状态更改时,当我断开数据连接时,应用程序将崩溃;然后,当数据连接打开时,将不会出现错误;然后,应用程序将崩溃,并且出现此异常,请向我建议如何解决。
最佳答案
看到这里Context.bindService:
因此,使用BroadcastReceiver.peekService从运行Service返回IBinder
:
Intent serviceIntent = new Intent(context, ChatService.class);
context.startService(serviceIntent);
if (isConnected == true) {
IBinder binder = peekService(context, new Intent(context, ChatService.class));
Toast.makeText(context, "Connected", 1000).show();
if (binder != null){
mChatService = ((LocalBinder) binder).getService();
//.... other code here
}
}
关于android - 不允许Broadcastreceiver组件绑定(bind)到android中的服务,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29114072/