我开发了一个Android应用程序,只要有WiFi或3G网络更改,它就会更新服务器数据库。我已经实现了一个BroadcastReceiver
来监听WiFi change or a 3G connectivity change
。
问题是此代码在Android版本4.x.x中无效。即使启用了网络或启用了wifi,“互联网已连接”也不会出现“吐司”。
它在android 2.3.x和3.x.x中完美地工作,出现了“Internet is connected”toast,并且数据库也被更新。
这是MainActivity.java
文件:
public class MainActivity extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Integer flag;
flag = 0;
ConnectivityManager check = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] info = check.getAllNetworkInfo();
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
Toast.makeText(context, "Internet is connected",
Toast.LENGTH_SHORT).show();
flag = 1;
}
}
if (flag != 0) {
StringBuffer sb = new StringBuffer();
TelephonyManager TelephonyMgr = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
m_deviceId = TelephonyMgr.getDeviceId();
m_androidId = Secure.getString(context.getContentResolver(),
Secure.ANDROID_ID);
String bm = android.os.Build.MANUFACTURER + ","
+ android.os.Build.MODEL;
System.out.println("Device Name: " + bm + "\n");
try {
URL url = new URL(
"http://saslabtech.com/atsdemo/import_vr.php?d="
+ URLEncoder.encode(m_deviceId, "UTF-8") + "&a="
+ URLEncoder.encode(m_androidId, "UTF-8")
+ "&model=" + URLEncoder.encode(bm, "UTF-8")
+ "&ip="
+ URLEncoder.encode(getLocalIpv4Address(), "UTF-8"));
System.out.println(url);
HttpURLConnection con = (HttpURLConnection) url
.openConnection();
readStream(con.getInputStream());
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(context, "Updated", Toast.LENGTH_SHORT).show();
}
}
}
这是AndroidManifest.xml
文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.read"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name=".MainActivity" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</receiver>
</application>
</manifest>
我无法弄清楚我哪里出错了。
谢谢!
答案 0 :(得分:0)
您可以简单地检查连接,如下所述:
@Override
public void onReceive(Context context, Intent intent) {
Log.d("Network Monitoring", "onReceive ");
if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
boolean noConnectivity = intent.getBooleanExtra(
ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
if (noConnectivity) {
return;//you don't have internet connectivity
}
//you have internet connectivity, so you may put your code here for further process
}
}