20117 月31
Android开发:休眠唤醒或开机后cmwap/cmnet网络不能连接的解决办法
Android手机(移动GSM)在休眠或开机后不能成功启用网络链接(设置都正常),有时候甚至状态栏图标是连接的,但网络依旧不可用。
如下解决方法,不知可通用,但测试HTC野火手机移动版可使用:
(被这个问题折腾死了,从本站相关文章可以看到,之前尝试了APN切换也不行,估计是网络enable==false)
package com.wiz.tools;
import java.lang.reflect.Method;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.telephony.TelephonyManager;
import android.util.Log;
/**
* Wizzer.cn
* @author Wizzer
*
*/
public class NetCheck {
public static final Uri APN_URI = Uri.parse("content://telephony/carriers");
public static final Uri DEFAULTAPN_URI = Uri
.parse("content://telephony/carriers/restore");
public static final Uri CURRENT_APN_URI = Uri
.parse("content://telephony/carriers/preferapn");
public static Context c1;
public static String getCurrentAPNFromSetting(ContentResolver resolver) {
Cursor cursor = null;
try {
cursor = resolver.query(CURRENT_APN_URI, null, null, null, null);
String curApnId = null;
String apnName1 = null;
if (cursor != null && cursor.moveToFirst()) {
curApnId = cursor.getString(cursor.getColumnIndex("_id"));
apnName1 = cursor.getString(cursor.getColumnIndex("apn"));
}
Log.e("NetCheck getCurrentAPNFromSetting", "curApnId:" + curApnId
+ " apnName1:" + apnName1);
// find apn name from apn list
if (curApnId != null) {
cursor = resolver.query(APN_URI, null, " _id = ?",
new String[] { curApnId }, null);
if (cursor != null && cursor.moveToFirst()) {
String apnName = cursor.getString(cursor
.getColumnIndex("apn"));
return apnName;
}
}
} catch (SQLException e) {
Log.e("NetCheck getCurrentAPNFromSetting", e.getMessage());
} finally {
if (cursor != null) {
cursor.close();
}
}
return null;
}
public static int updateCurrentAPN(ContentResolver resolver, String newAPN) {
Cursor cursor = null;
try {
// get new apn id from list
cursor = resolver.query(APN_URI, null, " apn = ? and current = 1",
new String[] { newAPN.toLowerCase() }, null);
String apnId = null;
if (cursor != null && cursor.moveToFirst()) {
apnId = cursor.getString(cursor.getColumnIndex("_id"));
}
Log.e("NetCheck updateCurrentAPN", "apnId:" + apnId);
// set new apn id as chosen one
if (apnId != null) {
ContentValues values = new ContentValues();
values.put("apn_id", apnId);
resolver.update(CURRENT_APN_URI, values, null, null);
} else {
// apn id not found, return 0.
return 0;
}
} catch (SQLException e) {
Log.e("NetCheck updateCurrentAPN", e.getMessage());
} finally {
if (cursor != null) {
cursor.close();
}
}
// update success
return 1;
}
public String getApn(Context c) {
boolean netSataus = false;
ConnectivityManager conManager = (ConnectivityManager) c
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (conManager.getActiveNetworkInfo() != null) {
netSataus = conManager.getActiveNetworkInfo().isAvailable();
}
NetworkInfo info = conManager
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
String oldAPN = StringUtils.null2String(info.getExtraInfo());
Log
.e("NetCheck getApn", "oldAPN:" + oldAPN + " netSataus:"
+ netSataus);
if (netSataus == false) {
Log.e("NetCheck getApn", "setMobileDataEnabled(true)");
setMobileDataEnabled(c, true);
try {
Thread.sleep(4012);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if("".equals(oldAPN)){
updateCurrentAPN(c.getContentResolver(), "cmnet");
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
info = conManager
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
oldAPN = StringUtils.null2String(info.getExtraInfo());
Log
.e("NetCheck getApn", "newApn:" + oldAPN);
return oldAPN.toLowerCase();
}
public boolean setMobileDataEnabled(Context c, boolean enabled) {
final TelephonyManager mTelManager;
mTelManager = (TelephonyManager) c
.getSystemService(Context.TELEPHONY_SERVICE);
try {
Method m = mTelManager.getClass()
.getDeclaredMethod("getITelephony");
m.setAccessible(true);
Object telephony = m.invoke(mTelManager);
m = telephony.getClass().getMethod(
(enabled ? "enable" : "disable") + "DataConnectivity");
m.invoke(telephony);
return true;
} catch (Exception e) {
Log.e("NetCheck ", "cannot fake telephony", e);
return false;
}
}
}