BroadCast CONNECTIVITY_CHANGE触发了很多次

时间:2014-07-24 14:44:26

标签: android wifi broadcast connectivity receiver

在BroadcastReceiever中,我使用此代码检测设备何时连接到互联网(当连接可用时 - WiFi /数据):

if(arg1.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {

        ConnectivityManager connMgr = (ConnectivityManager) arg0.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

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

 Toast.makeText(Context, "Connected to internet", Toast.LENGTH_LONG).show();

 }

问题:

  1. 电话已断开

  2. 启用WiFi或数据连接

  3. 我收到通知但次数太多了。手机仍然连接,但我一直收到通知。

  4. 为什么?

    由于

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

public class MainActivity extends Activity {

    private class ConnectivityBroadcastReceiver extends BroadcastReceiver {

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

            MainActivity.this.oldWifiConnectionState = MainActivity.this.currentWifiConnectionState;

            boolean wifiConnected = false;

            final ConnectivityManager connMgr = ( ConnectivityManager ) context.getSystemService( Context.CONNECTIVITY_SERVICE );

            final NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

            if ( ( networkInfo != null ) && networkInfo.isConnected() && networkInfo.isAvailable() ) {
                wifiConnected = true;
            }

           // or you can use 
           // wifiConnected  = ( activeNetwork != null ) && activeNetwork.isConnectedOrConnecting();


            boolean mobileDataEnabled = false; // Assume disabled

            try {
                final Class cmClass = Class.forName( connMgr.getClass().getName() );
                final Method method = cmClass.getDeclaredMethod( "getMobileDataEnabled" );
                method.setAccessible( true ); // Make the method callable

                // get the setting for "mobile data"
                mobileDataEnabled = ( Boolean ) method.invoke( connMgr );

            } catch ( final Exception e ) {
                mobileDataEnabled = false;
            }

            MainActivity.this.currentWifiConnectionState = ( wifiConnected || mobileDataEnabled ) ? WifiConnectionState.CONNECTED : WifiConnectionState.NOT_CONNECTED;

            if ( !MainActivity.this.currentWifiConnectionState.equals( MainActivity.this.oldWifiConnectionState ) ) {
                // Notifiy any handlers.

            }

        }

    }

    private enum WifiConnectionState {
        CONNECTED, NOT_CONNECTED, UNKNOWN
    }

    private WifiConnectionState           currentWifiConnectionState;

    private ConnectivityBroadcastReceiver mReceiver = null;

    private WifiConnectionState           oldWifiConnectionState;

    @Override
    protected void onCreate( final Bundle savedInstanceState ) {

        this.currentWifiConnectionState = WifiConnectionState.UNKNOWN;

        this.mReceiver = new ConnectivityBroadcastReceiver();

        super.onCreate( savedInstanceState );

    }

    @Override
    protected void onPause() {

        this.unregisterReceiver( this.mReceiver );
        super.onPause();
    }

    @Override
    protected void onResume() {
        this.registerReceiver( this.mReceiver, new IntentFilter( "android.net.conn.CONNECTIVITY_CHANGE" ) );
        super.onResume();
    }
}