自动检查互联网的广播接收器不适用于果冻豆或奇巧
Posted
技术标签:
【中文标题】自动检查互联网的广播接收器不适用于果冻豆或奇巧【英文标题】:BroadCast Receiver to check internet automatically wont work on jelly beans or kitkat 【发布时间】:2014-07-13 09:06:00 【问题描述】:我拥有的是用于检查互联网连接何时发生变化的 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】:这在 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>
希望对你有所帮助
【讨论】:
以上是关于自动检查互联网的广播接收器不适用于果冻豆或奇巧的主要内容,如果未能解决你的问题,请参考以下文章
无法添加窗口 - 令牌 null 不适用于广播接收器的 OnReceive 内的应用程序