CONNECTIVITY_ACTION 的 BroadcastReceiver 始终在intent.getExtras() 中返回 null

发布于 2024-11-28 01:25:49 字数 1635 浏览 0 评论 0原文


我试图从 CONNECTIVITY_ACTION 接收广播消息:

    // register BroadcastReceiver on network state changes
    final IntentFilter mIFNetwork = new IntentFilter();
    mIFNetwork.addAction(android.net.ConnectivityManager.CONNECTIVITY_ACTION); //"android.net.conn.CONNECTIVITY_CHANGE"
    registerReceiver(mIRNetwork, mIFNetwork);

接收者是:

private BroadcastReceiver mIRNetwork = new BroadcastReceiver() {
    @Override
    public void onReceive(final Context context, final Intent intent) {

        android.util.Log.i(TAG,"mIRNetwork: Network State Received: "+intent.getAction());
        Bundle extras = intent.getExtras();
        if (extras!=null){
                android.util.Log.i(TAG,"mIRNetwork: ACTION_BACKGROUND_DATA_SETTING_CHANGED: "+extras.getString(ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED));
                android.util.Log.i(TAG,"mIRNetwork: CONNECTIVITY_ACTION: "+extras.getString(ConnectivityManager.CONNECTIVITY_ACTION));
                android.util.Log.i(TAG,"mIRNetwork: EXTRA_EXTRA_INFO: "+extras.getString(ConnectivityManager.EXTRA_EXTRA_INFO));
                android.util.Log.i(TAG,"mIRNetwork: EXTRA_NO_CONNECTIVITY: "+extras.getString(ConnectivityManager.EXTRA_NO_CONNECTIVITY));
                android.util.Log.i(TAG,"mIRNetwork: EXTRA_REASON: "+extras.getString(ConnectivityManager.EXTRA_REASON));

            }
        }

简而言之,额外内容始终为空。我认为如果我失去了 WiFi 连接,我应该得到 EXTRA_NO_CONNECTIVITY (因为它是连接互联网的唯一途径)或者至少是列表中的某些内容。但没有运气。如果我断开我的 WiFi AP 接收器会收到他的消息,但没有额外信息。当我再次打开 WiFi 时,接收器会触发,但没有额外的东西......这是为什么?如何知道该应用程序丢失了任何网络连接?我以为就是这样。

Im trying to receive BroadcastMessages from CONNECTIVITY_ACTION:

    // register BroadcastReceiver on network state changes
    final IntentFilter mIFNetwork = new IntentFilter();
    mIFNetwork.addAction(android.net.ConnectivityManager.CONNECTIVITY_ACTION); //"android.net.conn.CONNECTIVITY_CHANGE"
    registerReceiver(mIRNetwork, mIFNetwork);

and receiver is:

private BroadcastReceiver mIRNetwork = new BroadcastReceiver() {
    @Override
    public void onReceive(final Context context, final Intent intent) {

        android.util.Log.i(TAG,"mIRNetwork: Network State Received: "+intent.getAction());
        Bundle extras = intent.getExtras();
        if (extras!=null){
                android.util.Log.i(TAG,"mIRNetwork: ACTION_BACKGROUND_DATA_SETTING_CHANGED: "+extras.getString(ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED));
                android.util.Log.i(TAG,"mIRNetwork: CONNECTIVITY_ACTION: "+extras.getString(ConnectivityManager.CONNECTIVITY_ACTION));
                android.util.Log.i(TAG,"mIRNetwork: EXTRA_EXTRA_INFO: "+extras.getString(ConnectivityManager.EXTRA_EXTRA_INFO));
                android.util.Log.i(TAG,"mIRNetwork: EXTRA_NO_CONNECTIVITY: "+extras.getString(ConnectivityManager.EXTRA_NO_CONNECTIVITY));
                android.util.Log.i(TAG,"mIRNetwork: EXTRA_REASON: "+extras.getString(ConnectivityManager.EXTRA_REASON));

            }
        }

in short extras is always null. I thought that if i loose WiFi connection i should get EXTRA_NO_CONNECTIVITY (cuz its the only way to Internet) or at least something from the list. But no luck. If i disconnect my WiFi AP receiver gets his message but with null extras. When i turn my WiFi back on once again receiver fires but no extras... Why is that? How to know that app lost any network connection? I thought its the way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何止钟意 2024-12-05 01:25:49

您无法获得额外的信息,但可以通过这种方式获取数据

private class ConnectivityBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager
                .EXTRA_NO_CONNECTIVITY, false);
        NetworkInfo info1 = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager
                .EXTRA_NETWORK_INFO);
        NetworkInfo info2 = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager
                .EXTRA_OTHER_NETWORK_INFO);
        String reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON);
        boolean failOver = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);
        Log.d("MY_TAG", "onReceive(): mNetworkInfo=" + info1 + " mOtherNetworkInfo = " +
                (info2 == null ? "[none]" : info2 + " noConn=" + noConnectivity));
    }
}

有关详细信息,请参阅此

http://code.google.com/p/androidwisprclient/source/browse/trunk/src/com/joan/pruebas/NetworkConnectivityListener.java?r=2

You can not get extra but you can get data by this way

private class ConnectivityBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager
                .EXTRA_NO_CONNECTIVITY, false);
        NetworkInfo info1 = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager
                .EXTRA_NETWORK_INFO);
        NetworkInfo info2 = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager
                .EXTRA_OTHER_NETWORK_INFO);
        String reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON);
        boolean failOver = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);
        Log.d("MY_TAG", "onReceive(): mNetworkInfo=" + info1 + " mOtherNetworkInfo = " +
                (info2 == null ? "[none]" : info2 + " noConn=" + noConnectivity));
    }
}

For more info see this

http://code.google.com/p/androidwisprclient/source/browse/trunk/src/com/joan/pruebas/NetworkConnectivityListener.java?r=2

不再见 2024-12-05 01:25:49

Dharmendras 答案很好。但请注意,EXTRA_NETWORK_INFO 现已弃用(自 Api 级别 14 起),并且 Android 文档说明如下:

由于 NetworkInfo 可能会根据 UID 的不同而有所不同,因此应用程序应始终通过 getActiveNetworkInfo() 获取网络信息。

这实际上让我们的事情变得非常容易。您可以重复使用之前可能执行的连接检查,并执行类似以下操作:

private class ConnectivityBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        unregisterReceiver(this)
        checkConnection();
    }
}

private void checkConnection() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    if (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo()
            .isConnectedOrConnecting()) {
        // do something
    }
}

假设您位于注册广播的活动中。

这还有一个额外的好处,即仅遵循最佳实践尽可能简短地监听连接广播,概述在这里:)

Dharmendras answer is good. However, note that EXTRA_NETWORK_INFO is now deprecated (since Api Level 14) and the Android docs say the following:

Since NetworkInfo can vary based on UID, applications should always obtain network information through getActiveNetworkInfo().

That actually makes things very easy for us. You could reuse the connectivity check you probably did before and do something like this:

private class ConnectivityBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        unregisterReceiver(this)
        checkConnection();
    }
}

private void checkConnection() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    if (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo()
            .isConnectedOrConnecting()) {
        // do something
    }
}

Assuming you're inside the activity that registered the broadcast of course.

This also has the added benefit of following the best practices for only listening for the connectivity broadcast as briefly as possible, outlined here :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文