连接wifi时如何接收移动数据禁用事件

时间:2019-01-30 04:59:33

标签: java android networking wifi

ConnectivityManager.CONNECTIVITY_ACTION在当前网络类型为wifi时无法接收广播,当用户单击禁用或启用移动数据时如何接收相关事件。 我的代码如下:

context.register(MyBroadCast,newIntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

我使用wifi连接到网络时无法接收BroadCast

但是我发现我可以得到这样的状态

 Settings.Secure.getInt(mContext.getContentResolver(), "mobile_data", -1)

看起来像contentprovider(URI,...),我想为此registerObserver,但我找不到相关的Uri,有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

您可以利用ConnectivityManager的API:特别是在您的用例中,您对registerDefaultNetworkCallback()感兴趣:

public class TestActivity extends AppCompatActivity {

    private ConnectivityManager manager;
    private final ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
        @Override
        public void onAvailable(Network network) {
            super.onAvailable(network);
            // this ternary operation is not quite true, because non-metered doesn't yet mean, that it's wifi
            // nevertheless, for simplicity let's assume that's true
            Log.i("vvv", "connected to " + (manager.isActiveNetworkMetered() ? "LTE" : "WIFI"));
        }

        @Override
        public void onLost(Network network) {
            super.onLost(network);
            Log.i("vvv", "losing active connection");
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        manager.registerDefaultNetworkCallback(networkCallback);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        manager.unregisterNetworkCallback(networkCallback);
    }
}

您无法事先知道,当WIFI断开时,设备最终是否会最终连接到LTE。因此,您可以采用以下方法:在处理程序上发布操作以在一秒钟内发生,然后在该操作内取消调用。如果连接很快出现-取消安排先前发布的操作。如果您最终遇到的是Runnable代码,则说明连接建立得并不很快,这意味着您应该结束调用。

 public class TestActivity extends AppCompatActivity {

        private ConnectivityManager manager;

        private final Handler handler = new Handler();
        private final ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
            @Override
            public void onAvailable(Network network) {
                super.onAvailable(network);
                Log.i("vvv", "connected to " + (manager.isActiveNetworkMetered() ? "LTE" : "WIFI"));

                // we've got a connection, remove callbacks (if we have posted any)
                handler.removeCallbacks(endCall);
            }

            @Override
            public void onLost(Network network) {
                super.onLost(network);
                Log.i("vvv", "losing active connection");

                // Schedule an event to take place in a second
                handler.postDelayed(endCall, 1000);
            }
        };

        private final Runnable endCall = new Runnable() {
            @Override
            public void run() {
                // if execution has reached here - feel free to cancel the call
                // because no connection was established in a second
            }
        };

        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            manager.registerDefaultNetworkCallback(networkCallback);
        }

        @Override
        protected void onDestroy() {
            super.onDestroy();
            manager.unregisterNetworkCallback(networkCallback);
            handler.removeCallbacks(endCall);
        }
    }