简体   繁体   English

连接变化接收器在android中得到错误的意图

[英]connectivity change receiver gets false intents in android

I'm trying to add some connection state awareness to my app. 我正在尝试向我的应用添加一些连接状态感知。 Using examples here and google's documentation iv'e come up with a receiver that shows an alert correctly when the connection status changed, but also unwantedly shows it whenever the activity is created. 使用这里的示例和google的文档,我们想出了一个接收器,它可以在连接状态发生变化时正确显示警报,但每当创建活动时也会不需要地显示警报。

the ConnectStatusReceiver: ConnectStatusReceiver:

package com.zivtaller.placefinder.receivers;

 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
 import android.net.ConnectivityManager;
 import android.net.NetworkInfo;
 import android.util.Log;
 import android.widget.Toast;

  public class ConnectStatusReceiver extends BroadcastReceiver {

private String TAG = "netReceiver";

public ConnectStatusReceiver() {
    super();
}

@Override
public void onReceive(Context context, Intent arg1) {
    ConnectivityManager cm = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    boolean isConnected = activeNetwork != null
            && activeNetwork.isConnectedOrConnecting();
    if (isConnected) {
        Log.d(TAG, "Data connected");
        Toast.makeText(context, "Data connected", Toast.LENGTH_SHORT)
                .show();
    } else {
        Log.d(TAG, "Data not connected");
        Toast.makeText(context, "Data not connected", Toast.LENGTH_SHORT)
                .show();
    }

}

} }

its initialization and registration in activity's onResume(): 它在activity的onResume()中初始化和注册:

        netReceiver = new ConnectStatusReceiver();
    IntentFilter netFilter = new IntentFilter();
    netFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
    registerReceiver(netReceiver, netFilter);

The system is likely generating sticky broadcast android.net.conn.CONNECTIVITY_CHANGE intents. 该系统可能会生成粘性广播android.net.conn.CONNECTIVITY_CHANGE意图。 Sticky broadcasts stay around after they are sent. 粘性广播在发送后留在身边。 Whenever you register a BroadcastReceiver to listen for broadcasts that are sticky, the most recently broadcasted intent will be passed to the receiver immediately when registerReceiver() is called. 每当您注册BroadcastReceiver以侦听粘性广播时,最近广播的意图将在调用registerReceiver()时立即传递给接收方。

An easy way to test whether or not the android.net.conn.CONNECTIVITY_CHANGE broadcast is sticky would be to check the return value of your registerReceiver() call in onResume() 测试android.net.conn.CONNECTIVITY_CHANGE广播是否粘滞的简单方法是检查onResume() registerReceiver()调用的返回值

netReceiver = new ConnectStatusReceiver();
IntentFilter netFilter = new IntentFilter();
netFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
Intent intent = registerReceiver(netReceiver, netFilter);
if (intent != null) {
    // broadcast is sticky
}

If you are able to determine that the broadcasts are indeed sticky, then what you are experiencing is intended behavior. 如果您能够确定广播确实是粘性的,那么您所遇到的是预期的行为。

See what is the difference between sendStickyBroadcast and sendBroadcast in Android . 看看Android中sendStickyBroadcast和sendBroadcast有什么区别

Please do , 请做,

unregisterReceiver(netReceiver);

On your onPause() method. onPause()方法上。

As answered Bryan Dunlap you are dealing with sticky intent. 正如Bryan Dunlap所说,你正在处理粘性意图。 To check if intent that you have received is sticky, you should call isInitialStickyBroadcast() in onReceive() 要检查您收到的意图是否是粘性的,您应该在onReceive()调用isInitialStickyBroadcast() onReceive()

https://stackoverflow.com/a/11825203/1735100 https://stackoverflow.com/a/11825203/1735100

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM