我怎样才能收到通知时,该设备在Android L移动失去网络连接(API 21)通知、设备、网络、Android

2023-09-06 07:40:50 作者:屎香味的猪

我使用这个code当连接在API 20将丢失,到通知。

I'm using this code to be notified when the connection is lost in API 20 and down.

registerReceiver(getConnectivityStateBroadcastReceiver(), new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

private class ConnectivityStateBroadcastReceiver extends BaseBroadcastReceiver {

    /**
     * @param userLoggedIn
     * @param context
     * @param intent
     */
    @Override
    protected void onReceive(Boolean userLoggedIn, Context context, Intent intent) {

        Bundle extras = intent.getExtras();
        boolean notConnected = extras.getBoolean(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);

        // DO something
    }
}

但它不是在API 21个。

but it's not working in API 21.

我该如何解决呢?也许它有与 ConnectivityManager.NetworkCallbak 做的,但我没有'找不到任何例子,如何使用它。谢谢你。

How can I fix that? maybe it's got to do with ConnectivityManager.NetworkCallbak but I didn't find any example to how to use it. Thanks.

推荐答案

OK,所以我想出如何做到这一点,但会AP preciate确认这个解决方案是正确的。

OK, so I figure out how to do it but would appreciate confirmation that this solution is the right one.

我所做的是增加在我的应用程序类的OnCreate添加到此code通话

All I did is to add add a call to this code in the onCreate of my application class

/**
 *
 */
@SuppressLint("NewApi")
private void registerConnectivityNetworkMonitorForAPI21AndUp() {

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        return;
    }

    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkRequest.Builder builder = new NetworkRequest.Builder();

    connectivityManager.registerNetworkCallback(
            builder.build(),
            new ConnectivityManager.NetworkCallback() {
                /**
                 * @param network
                 */
                @Override
                public void onAvailable(Network network) {

                    sendBroadcast(
                            getConnectivityIntent(false)
                    );

                }

                /**
                 * @param network
                 */
                @Override
                public void onLost(Network network) {

                    sendBroadcast(
                            getConnectivityIntent(true)
                    );

                }
            }

    );

}

 /**
 * @param noConnection
 * @return
 */
private Intent getConnectivityIntent(boolean noConnection) {

    Intent intent = new Intent();

    intent.setAction("mypackage.CONNECTIVITY_CHANGE");
    intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, noConnection);

    return intent;

}

和在IntentFilter的已监视我的连通性API 20,但我将此

and in the IntentFilter that already monitoring my connectivity for API 20 and less I added this

IntentFilter filter = new IntentFilter();

filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
filter.addAction("mypackage.CONNECTIVITY_CHANGE");

现在我已经工作的广播接收器获取有关API 21太网络更改的通知。

and now my already working broadcast receiver get notification about network changes in API 21 too.