我正在使用以下代码检查整个应用程序中的互联网连接。
public class UpdateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
boolean isConnected = activeNetInfo != null
&& activeNetInfo.isConnectedOrConnecting();
if (isConnected){
Log.i("NET", "connecte" + isConnected);
Toast.makeText(context, "net Connected", Toast.LENGTH_LONG)
.show();
}
else{
Log.i("NET", "not connecte" + isConnected);
Toast.makeText(context, "No Internet Connection", Toast.LENGTH_LONG)
.show();
}
}
}
问题是当他们没有互联网连接/wi-fi 信号时,toast 消息也会显示在其他应用程序中。我该如何避免这种情况?
谢谢:)