简体   繁体   English

BroadcastReceiver CONNECTIVITY_CHANGE总是在第一次启动应用程序中运行?

[英]BroadcastReceiver CONNECTIVITY_CHANGE always run in the first time launch app?

I read it on Google Developer: 我在Google Developer上阅读了它:

("android.net.conn.CONNECTIVITY_CHANGE") action whenever the connectivity details have changed (“android.net.conn.CONNECTIVITY_CHANGE”)操作只要连接详细信息发生更改

I have this code: 我有这个代码:

public class MainActivity extends AppCompatActivity { 公共类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;
    }
}

and // 和//

public class NetworkUtil { 公共类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;
}

} }

when first time i launch app, this intent always fired and displayed a dialog with the current state of network. 当我第一次启动应用程序时,此意图始终会触发并显示一个包含当前网络状态的对话框。 But based on this document, it only happens when connect change? 但基于这个文档,它只发生在连接变化时? If I want this display only when network changes, how could i do? 如果我只想在网络改变时才想显示这个显示器,我该怎么办? Many thanks 非常感谢

The broadcast android.net.conn.CONNECTIVITY_CHANGE is a sticky broadcast . 广播android.net.conn.CONNECTIVITY_CHANGE是一个粘性广播 This means that whenever you register a BroadcastReceiver for this ACTION, it will always be triggered immediately and onReceive() will be called with the most recent broadcast connectivity change . 这意味着无论何时为此ACTION注册BroadcastReceiver ,它都将立即被触发,并且将使用最近的广播连接更改来调用onReceive() This allows you to get the current state of the connectivity without waiting for something to change. 这使您可以获得连接的当前状态,而无需等待更改。

If you want to ignore the current state, and only want to process state changes, you can add this to your onReceive() : 如果要忽略当前状态,并且只想处理状态更改,可以将其添加到onReceive()

if (isInitialStickyBroadcast()) {
    // Ignore this call to onReceive, as this is the sticky broadcast
} else {
    // Connectivity state has changed
    ... (your code here)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM