1

当设备失去网络连接时,我需要显示警报屏幕。当连接恢复时,我会隐藏此警报。我正在使用 ConnectivityManager.NetworkCallback() 来确定连接状态。该应用程序正在 API 22 设备上运行。

public void registerNetworkAvailability(final Context context, BroadcastReceiver networkAvailabilityReceiver) {
        context.registerReceiver(networkAvailabilityReceiver, new IntentFilter(NETWORK_AVAILABILITY_ACTION));
        connectivityManager = (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE);
        NetworkRequest.Builder builder = new NetworkRequest.Builder();
        networkCallback = new ConnectivityManager.NetworkCallback() {
            @Override
            public void onAvailable(Network network) {
                context.sendBroadcast(getNetworkAvailabilityIntent(true));
            }

            @Override
            public void onLost(Network network) {
                if (!isAvailable(context))
                    context.sendBroadcast(getNetworkAvailabilityIntent(false));
            }
        };
        connectivityManager.registerNetworkCallback(builder.build(), networkCallback);
    }

它在我的 Activity 的 onCreate() 方法中注册

    networkAvailability = NetworkAvailability.getInstance();
    networkAvailability.registerNetworkAvailability(this, receiver);

这是广播接收器

private BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent == null) return;
        boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
        if (noConnectivity) {
            connectivityError = true;
            showNetworkAlert();
        } else {
            NetworkInfo networkInfo = networkAvailability.getActiveNetworkInfo(getApplicationContext());
            if (networkInfo != null) {
                networkType = networkInfo.getType();
                Logger.i(TAG, "connection type: " + networkInfo.getTypeName());
                Logger.i(TAG, "connection IP: " + networkAvailability.getLocalIpAddress());
            }
            if (connectivityError) {
                connectivityError = false;
                showDialog(null);
                initConfiguration();
            }
        }
        if (updateDialog != null) {
            updateDialog.dismiss(null);
            updateDialog = null;
        }
    }
};

通常,onAvailable()在连接 Wi-Fi 后被调用 1-2 次。但是对于应用程序正在运行的某些地方,onAvailable()每分钟都会有多个回调到达。

07-07 14:13:12.074  7343  7343 connection type: WIFI
07-07 14:13:12.077  7343  7343 connection IP: 192.168.1.216
07-07 14:13:24.160  7343  7343 connection type: WIFI
07-07 14:13:24.164  7343  7343 connection IP: 192.168.1.216
07-07 14:13:33.268  7343  7343 connection type: WIFI
07-07 14:13:33.278  7343  7343 connection IP: 192.168.1.216
07-07 14:13:45.409  7343  7343 connection type: WIFI
07-07 14:13:45.418  7343  7343 connection IP: 192.168.1.216
07-07 14:13:54.500  7343  7343 connection type: WIFI
07-07 14:13:54.512  7343  7343 connection IP: 192.168.1.216

这会影响应用程序性能,使 UI 缓慢且无响应。

这可能是什么原因?在这种情况下如何提高性能?

4

0 回答 0