BroadCast接收器自动检查互联网不会对果冻豆或kitkat工作

时间:2014-07-13 09:06:01

标签: android broadcastreceiver android-4.3-jelly-bean

我有什么Android代码来检查互联网连接何时发生变化,所以我可以知道并做其他事情......我确信这段代码是真的,但它不适用于比API 15更新的版本,这里是我的接收者:

public class NetworkChangeReceiver extends BroadcastReceiver {

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

        String status = NetworkUtil.getConnectivityStatusString(context);

        Toast.makeText(context, status, Toast.LENGTH_LONG).show();
    }
}

这是Network Util类:

public class NetworkUtil {

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


    public static 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 static String getConnectivityStatusString(Context context) {
        int conn = NetworkUtil.getConnectivityStatus(context);
        String status = null;
        if (conn == NetworkUtil.TYPE_WIFI) {
            status = "Wifi enabled";
        } else if (conn == NetworkUtil.TYPE_MOBILE) {
            status = "Mobile data enabled";
        } else if (conn == NetworkUtil.TYPE_NOT_CONNECTED) {
            status = "Not connected to Internet";
        }
        return status;
    }
}

这是我的清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.viralpatel.network"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <receiver
            android:name="net.viralpatel.network.NetworkChangeReceiver"
            android:label="NetworkChangeReceiver" >
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

那么我可以改变什么,我可以在Api 18或19上使用它?

1 个答案:

答案 0 :(得分:0)

这对KitKat来说很有用

public class WifiControlReceiver extends BroadcastReceiver {

// Strings - Log
final String TAG = "WifiControlReceiver";

@Override
public void onReceive(Context wiFiContext, Intent wiFiIntent) {
    Log.d(TAG, "WifiControlReceiver +++STARTED +++ ");

    Log.d(TAG, "@WifiControlReceiver actionCaught :" + wiFiIntent.getAction());
        if ("android.net.wifi.WIFI_STATE_CHANGED".equals(wiFiIntent
                .getAction())) {
            // TODO : what ever the action you want
        }
    Log.d(TAG, "DeviceControlHardware ---END/FIN--- / WifiControlReceiver ");
}}

对于清单

        <receiver
        android:name="com.example.WifiControlReceiver"
        android:enabled="true"
        android:label="WifiControlReceiver"
        android:permission="android.permission.ACCESS_NETWORK_STATE" >
        <intent-filter>
            <action android:name="android.net.wifi.WIFI_STATE_CHANGED" >
            </action>
        </intent-filter>
    </receiver>

希望有所帮助