仅在数据或 wifi 断开连接时收到通知
Get notified only when data or wifi is disconnected
大家好,我正在构建一个应用程序,我只想在设备失去连接(无论是移动数据还是 wifi)时通知用户。
到目前为止我用过这个方法:
private void setupReceiver() {
CheckNetworkStatusReceiver receiver = new CheckNetworkStatusReceiver();
registerReceiver(receiver, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));
}
和我的接收器:
public class CheckNetworkStatusReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
if (intent != null){
Log.e("Action ",intent.getAction());
if(intent.getAction().equalsIgnoreCase("android.net.conn.CONNECTIVITY_CHANGE")) {
Toast.makeText(context, "Connection changed", Toast.LENGTH_SHORT).show();
}
}
}
}
它工作正常,但问题是 toast 在连接到网络时也会显示,这是我不想要的。我只希望吐司在断开连接时显示。
有什么想法吗?
在显示 toast 之前检查连接是否可用,类似这样
if(!isNetworkAvailable()){
Toast.makeText(context, "Connection changed", Toast.LENGTH_SHORT).show();
}
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null;
}
编辑
为了安全起见,使用这个
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
您可以这样检查网络连接:
public static boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager) MBankApplication.appContext.getSystemService
(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiNetwork = cm.getNetworkInfo(1);
if (wifiNetwork != null && wifiNetwork.isConnected()) {
return true;
}
NetworkInfo mobileNetwork = cm.getNetworkInfo(0);
if (mobileNetwork != null && mobileNetwork.isConnected()) {
return true;
}
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork == null || !activeNetwork.isConnected()) {
return false;
}
return true;
}
然后展示你的吐司
if(!isNetworkConnected()){
Toast.makeText(context, "Connection changed", Toast.LENGTH_SHORT).show();
}
大家好,我正在构建一个应用程序,我只想在设备失去连接(无论是移动数据还是 wifi)时通知用户。
到目前为止我用过这个方法:
private void setupReceiver() {
CheckNetworkStatusReceiver receiver = new CheckNetworkStatusReceiver();
registerReceiver(receiver, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));
}
和我的接收器:
public class CheckNetworkStatusReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
if (intent != null){
Log.e("Action ",intent.getAction());
if(intent.getAction().equalsIgnoreCase("android.net.conn.CONNECTIVITY_CHANGE")) {
Toast.makeText(context, "Connection changed", Toast.LENGTH_SHORT).show();
}
}
}
}
它工作正常,但问题是 toast 在连接到网络时也会显示,这是我不想要的。我只希望吐司在断开连接时显示。
有什么想法吗?
在显示 toast 之前检查连接是否可用,类似这样
if(!isNetworkAvailable()){
Toast.makeText(context, "Connection changed", Toast.LENGTH_SHORT).show();
}
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null;
}
编辑
为了安全起见,使用这个
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
您可以这样检查网络连接:
public static boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager) MBankApplication.appContext.getSystemService
(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiNetwork = cm.getNetworkInfo(1);
if (wifiNetwork != null && wifiNetwork.isConnected()) {
return true;
}
NetworkInfo mobileNetwork = cm.getNetworkInfo(0);
if (mobileNetwork != null && mobileNetwork.isConnected()) {
return true;
}
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork == null || !activeNetwork.isConnected()) {
return false;
}
return true;
}
然后展示你的吐司
if(!isNetworkConnected()){
Toast.makeText(context, "Connection changed", Toast.LENGTH_SHORT).show();
}