打开/关闭移动蜂窝数据(移动互联网)时如何触发BroadcastReceiver

时间:2014-12-19 04:42:16

标签: android

如果我打开/关闭移动蜂窝数据,我想知道如何触发 BroadcastReceiver 。我已经注册了BroadcastReceiver并且如果我打开/关闭wifi,它工作正常但是如果我打开/关闭蜂窝数据没有广播触发器。有人可以帮我解决这个问题吗?

这是我的代码。

我在 Manifest.file

中注册 BroadcastReceiver
       <receiver android:name="com.servicesandroid.NetworkCheckReceiver" >
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            </intent-filter>
        </receiver>

清单权限

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET"/>

这是我的 BroadcastReceiver类。

public class NetworkCheckReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Toast.makeText(context, "Started", Toast.LENGTH_SHORT).show();
         Log.d("app","Network connectivity change");
    }

}

2 个答案:

答案 0 :(得分:7)

您需要指定适当的权限并按照以下说明执行操作:

清单中的权限:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

清单中的接收者声明:

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

NetworkCheckReceiver类文件:

public class NetworkCheckReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
            Log.d("NetworkCheckReceiver", "NetworkCheckReceiver invoked...");


            boolean noConnectivity = intent.getBooleanExtra(
                    ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);

            if (!noConnectivity) {
                Log.d("NetworkCheckReceiver", "connected");
            }
            else
            {
                Log.d("NetworkCheckReceiver", "disconnected");
            }
        }
    }

}

注意:

确保您正在使用移动蜂窝数据连接。如果您没有互联网包并且只是打开/关闭,则此接收器不会触发。你肯定需要有工作连接来测试。

答案 1 :(得分:2)

尝试以下操作,这将在移动数据和wifi更改时收到

 public class NetworkCheckReceiver extends BroadcastReceiver {

        /*
         * (non-Javadoc)
         * 
         * @see android.content.BroadcastReceiver#onReceive(android.content.Context,
         * android.content.Intent)
         */
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.e("NetworkCheckReceiver","ConnectionChangeReceiver.onReceive()");
            String statusString = getConnectivityStatusString(context);
            Toast.makeText(context, statusString, Toast.LENGTH_SHORT).show();
        }

        public int TYPE_WIFI = 1;
        public int TYPE_MOBILE = 2;
        public int TYPE_NOT_CONNECTED = 0;

        public int getConnectivityStatus(Context context) {
            ConnectivityManager cm = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
            if (null != activeNetwork) {
                if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
                    return TYPE_WIFI;

                if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
                    return TYPE_MOBILE;
            }
            return TYPE_NOT_CONNECTED;
        }

        public String getConnectivityStatusString(Context context) {
            int conn = getConnectivityStatus(context);
            String status = null;
            if (conn == TYPE_WIFI) {
                status = "Wifi enabled";
            } else if (conn == TYPE_MOBILE) {
                status = "Mobile data enabled";
            } else if (conn == TYPE_NOT_CONNECTED) {
                status = "Not connected to Internet";
            }
            return status;
        }

    }