BroadcastReceiver CONNECTIVITY_CHANGE 总是在第一次启动应用程序时 运行?
BroadcastReceiver CONNECTIVITY_CHANGE always run in the first time launch app?
我在 Google 上阅读它开发者:
("android.net.conn.CONNECTIVITY_CHANGE") action whenever the connectivity details have changed
我有这个代码:
public class MainActivity 扩展 AppCompatActivity {
private NetworkChangeReceiver receiver;
private boolean connIntentFilterIsRegistered;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
receiver = new NetworkChangeReceiver();
}
@Override
protected void onPause() {
super.onPause();
if (connIntentFilterIsRegistered) {
unregisterReceiver(receiver);
connIntentFilterIsRegistered = false;
}
}
@Override
protected void onResume() {
super.onResume();
if (!connIntentFilterIsRegistered) {
registerReceiver(receiver, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));
connIntentFilterIsRegistered = true;
}
}
和
//
public class NetworkUtil {
public static int TYPE_WIFI = 1;
public static int TYPE_MOBILE = 0;
public static int TYPE_NOT_CONNECTED = 2;
public static int getConnectivityStatus(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.isConnectedOrConnecting()) {
if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
return TYPE_WIFI;
}
if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
return TYPE_MOBILE;
}
}
return TYPE_NOT_CONNECTED;
}
public static String getConnectivityStatusString(Context context) {
int conn = NetworkUtil.getConnectivityStatus(context);
String status = null;
if (conn == TYPE_MOBILE) {
status = "Mobile cellular enabled";
} else if (conn == TYPE_WIFI) {
status = "Wifi enabled";
} else if (conn == TYPE_NOT_CONNECTED) {
status = "Not connected to internet";
}
return status;
}
}
当我第一次启动应用程序时,此 Intent 总是会触发并显示一个包含当前网络状态的对话框。但是根据这个文件,只有当连接改变时才会发生?如果我只想在网络发生变化时显示此内容,我该怎么办?非常感谢
广播android.net.conn.CONNECTIVITY_CHANGE
是粘性广播。这意味着每当您为此 ACTION 注册 BroadcastReceiver
时,它总是会立即被触发,并且 onReceive()
将随着 最近的广播连接更改 被调用。这使您无需等待更改即可获得连接的当前状态。
如果您想忽略当前状态,只想处理状态更改,您可以将此添加到您的 onReceive()
:
if (isInitialStickyBroadcast()) {
// Ignore this call to onReceive, as this is the sticky broadcast
} else {
// Connectivity state has changed
... (your code here)
}
我在 Google 上阅读它开发者:
("android.net.conn.CONNECTIVITY_CHANGE") action whenever the connectivity details have changed
我有这个代码:
public class MainActivity 扩展 AppCompatActivity {
private NetworkChangeReceiver receiver;
private boolean connIntentFilterIsRegistered;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
receiver = new NetworkChangeReceiver();
}
@Override
protected void onPause() {
super.onPause();
if (connIntentFilterIsRegistered) {
unregisterReceiver(receiver);
connIntentFilterIsRegistered = false;
}
}
@Override
protected void onResume() {
super.onResume();
if (!connIntentFilterIsRegistered) {
registerReceiver(receiver, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));
connIntentFilterIsRegistered = true;
}
}
和 //
public class NetworkUtil {
public static int TYPE_WIFI = 1;
public static int TYPE_MOBILE = 0;
public static int TYPE_NOT_CONNECTED = 2;
public static int getConnectivityStatus(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.isConnectedOrConnecting()) {
if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
return TYPE_WIFI;
}
if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
return TYPE_MOBILE;
}
}
return TYPE_NOT_CONNECTED;
}
public static String getConnectivityStatusString(Context context) {
int conn = NetworkUtil.getConnectivityStatus(context);
String status = null;
if (conn == TYPE_MOBILE) {
status = "Mobile cellular enabled";
} else if (conn == TYPE_WIFI) {
status = "Wifi enabled";
} else if (conn == TYPE_NOT_CONNECTED) {
status = "Not connected to internet";
}
return status;
}
}
当我第一次启动应用程序时,此 Intent 总是会触发并显示一个包含当前网络状态的对话框。但是根据这个文件,只有当连接改变时才会发生?如果我只想在网络发生变化时显示此内容,我该怎么办?非常感谢
广播android.net.conn.CONNECTIVITY_CHANGE
是粘性广播。这意味着每当您为此 ACTION 注册 BroadcastReceiver
时,它总是会立即被触发,并且 onReceive()
将随着 最近的广播连接更改 被调用。这使您无需等待更改即可获得连接的当前状态。
如果您想忽略当前状态,只想处理状态更改,您可以将此添加到您的 onReceive()
:
if (isInitialStickyBroadcast()) {
// Ignore this call to onReceive, as this is the sticky broadcast
} else {
// Connectivity state has changed
... (your code here)
}