gpt4 book ai didi

android - CONNECTIVITY_ACTION 的 BroadcastReceiver 在 intent.getExtras() 中总是返回 null

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:57:22 25 4
gpt4 key购买 nike


我正在尝试接收来自 CONNECTIVITY_ACTION 的 BroadcastMessages:

    // 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));

}
}

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

最佳答案

你不能得到额外的,但你可以通过这种方式获得数据

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

关于android - CONNECTIVITY_ACTION 的 BroadcastReceiver 在 intent.getExtras() 中总是返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6924006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com