wifi连接改变了广播接收器重复多次

时间:2014-11-25 05:32:25

标签: android broadcastreceiver

我有以下广播接收器。

public class TestNetworkReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        String TAG = "TEST:";

        if (intent.getAction().equals("android.net.wifi.WIFI_STATE_CHANGED")) {

            Log.i(TAG, "Wifi toggled");

        } else if (intent.getAction().equals("android.net.conn.CONNECTIVITY_CHANGE")) {

            Log.i(TAG, "Network connection changed");

            ConnectivityManager cm = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);

            NetworkInfo networkInfo = cm.getActiveNetworkInfo();

            if (networkInfo != null && networkInfo.isConnected()) {

                Log.i(TAG, "Network connecion is:" + networkInfo.getTypeName());

                Log.i(TAG, "Detail:"+networkInfo.isFailover());
            }
        }
    }
}

当我打开和关闭wifi时

I/TEST:﹕ Wifi toggled
I/TEST:﹕ Network connection changed
I/TEST:﹕ Network connecion is:WIFI
I/TEST:﹕ Detail:false
I/TEST:﹕ Network connection changed
I/TEST:﹕ Network connecion is:WIFI
I/TEST:﹕ Detail:false
I/TEST:﹕ Network connection changed
I/TEST:﹕ Network connecion is:WIFI
I/TEST:﹕ Detail:false

I/TEST:﹕ Network connection changed
I/TEST:﹕ Network connecion is:mobile
I/TEST:﹕ Detail:true
I/TEST:﹕ Network connection changed
I/TEST:﹕ Network connecion is:mobile
I/TEST:﹕ Detail:true

我想要达到的目标是知道什么时候连接“#34;移动"或" wifi"并且活跃但只有一次。否则它会运行我的例程3或4次。我尝试过更改networkInfo.whatever(),但所有内容都会重复3次或4次或更多次。有没有办法让我知道连接已经改变了,接收器中的某个地方只有一次?

4 个答案:

答案 0 :(得分:4)

广播[CONNECTIVITY_ACTION]似乎在少数设备上很粘,因此当您注册接收器时,它会立即使用最近发送的广播呼叫onReceive()。

因此,建议您需要在清单上进行接收器的静态声明。

这就是我用过的:

@Override
protected void onResume() {
    super.onResume();
    setMyReceiverStayus(PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
}

@Override
protected void onPause() {
    super.onPause();
    setMyReceiverStayus(PackageManager.COMPONENT_ENABLED_STATE_DISABLED);
}


private void setMyReceiverStayus(int receiverState) {
    ComponentName receiver = new ComponentName(this, MyReceiver.class);
    PackageManager pm = this.getPackageManager();
    pm.setComponentEnabledSetting(receiver, receiverState,       
    PackageManager.DONT_KILL_APP);
}

的AndroidManifest.xml

<receiver android:name="com.example.receiver.MyReceiver">
  <intent-filter>
    <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
  </intent-filter>
</receiver>

答案 1 :(得分:1)

你可以试试这个

public class TestNetworkReceiver extends BroadcastReceiver {
    public static boolean firstTime = true;

    @Override
    public void onReceive(Context context, Intent intent) {

        String TAG = "TEST:";

        if (firstTime) {
            firstTime = false;
            //do stuff
        }
    }
}

可能不是最强的方式,但它可以解决问题

答案 2 :(得分:0)

public void onReceive(Context context, Intent intent) {
     ConnectivityManager cm = (ConnectivityManager)  
     context.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo info = cm.getActiveNetworkInfo();
        if (info != null) {
            if (info.isConnected()) {
                // you got a connection! tell your user!
                Log.i("Post", "Connected");

            }
        } 
    }

将此添加到清单

 <receiver android:name="packageName.ConnectivityDetector_Receiver" >
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
    </receiver>

试试这个

答案 3 :(得分:0)

对于希望通过连接上/下处理程序进行一些后台活动并维护线程安全的人,后期发布可能很有用。

我使用了AtomicBoolean变量,

private AtomicBoolean networkUp = new AtomicBoolean(false);

private void onConnectivityUp() {
    Log.d("TAG", "onConnectivityUp");

    if (networkUp.compareAndSet(false, true)) {
        // Do something
    } else {
        // Duplicate event ignoring
        Log.d("TAG", "onConnectivityUp - ignoring");
    }
}

private void onConnectivityDown() {
    Log.d("TAG", "onConnectivityDown");
    if (networkUp.compareAndSet(true, false)) {
        // Do something
    } else {
        // Duplicate event ignoring
        Log.d("TAG", "onConnectivityDown - ignoring");
    }
}

PS:我的广播接收器正在调用onConnectivityUp和onConnectivityDown

相关问题