[英]How to detect Internet Connection State via CONNECTIVITY_CHANGE broadcast
I need to handle the connectivity change broadcast in my app. 我需要处理我的应用中广播的连接更改。 Every thing is great except that when it comes for the broadcast the application crashes. 一切都很棒,除了广播时应用程序崩溃。 I am using the following code in my Broadcast : 我在广播中使用以下代码:
@Override
public void onReceive(Context context, Intent intent) {
Log.i("NET", "Broadcast started");
Intent startServiceIntent = new Intent(context, NewsService.class);
boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
if(noConnectivity) {
context.stopService(startServiceIntent);
Toast.makeText(context, "Connection is terminated!", Toast.LENGTH_LONG).show();
Log.i("NET", "Stopped");
}
else {
context.startService(startServiceIntent);
Toast.makeText(context, "Connection is ok!", Toast.LENGTH_LONG).show();
}
}
This code is supposed to stop a service when no internet connection is found and to start it whenever it finds a connection. 如果找不到互联网连接,则此代码应停止服务,并在找到连接时启动该服务。
最后,经过2天的搜索,我发现了问题所在,这与广播所放置的包裹有关, 我在这里找到了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.