Android手機(移動GSM)在休眠或開機後不能成功啟用網路連結(設定都正常),有時候甚至狀態列表徵圖是串連的,但網路依舊不可用。
如下解決方案,不知可通用,但測試HTC野火手機移動版可使用:
(被這個問題折騰死了,從本站相關文章可以看到,之前嘗試了APN切換也不行,估計是網路enable==false)
import java.lang.reflect.Method;004 005import android.content.ContentResolver;006import android.content.ContentValues;007import android.content.Context;008import android.database.Cursor;009import android.database.SQLException;010import android.net.ConnectivityManager;011import android.net.NetworkInfo;012import android.net.Uri;013import android.telephony.TelephonyManager;014import android.util.Log;015/**016 * Wizzer.cn017 * @author Wizzer018 *019 */020public class NetCheck {021 public static final Uri APN_URI = Uri.parse("content://telephony/carriers");022 public static final Uri DEFAULTAPN_URI = Uri023 .parse("content://telephony/carriers/restore");024 public static final Uri CURRENT_APN_URI = Uri025 .parse("content://telephony/carriers/preferapn");026 public static Context c1;027 028 public static String getCurrentAPNFromSetting(ContentResolver resolver) {029 Cursor cursor = null;030 try {031 cursor = resolver.query(CURRENT_APN_URI, null, null, null, null);032 String curApnId = null;033 String apnName1 = null;034 if (cursor != null && cursor.moveToFirst()) {035 curApnId = cursor.getString(cursor.getColumnIndex("_id"));036 apnName1 = cursor.getString(cursor.getColumnIndex("apn"));037 }038 Log.e("NetCheck getCurrentAPNFromSetting", "curApnId:" + curApnId039 + " apnName1:" + apnName1);040 // find apn name from apn list041 if (curApnId != null) {042 cursor = resolver.query(APN_URI, null, " _id = ?",043 new String[] { curApnId }, null);044 if (cursor != null && cursor.moveToFirst()) {045 String apnName = cursor.getString(cursor046 .getColumnIndex("apn"));047 return apnName;048 }049 }050 051 } catch (SQLException e) {052 Log.e("NetCheck getCurrentAPNFromSetting", e.getMessage());053 } finally {054 if (cursor != null) {055 cursor.close();056 }057 }058 059 return null;060 }061 062 public static int updateCurrentAPN(ContentResolver resolver, String newAPN) {063 Cursor cursor = null;064 try {065 // get new apn id from list066 cursor = resolver.query(APN_URI, null, " apn = ? and current = 1",067 new String[] { newAPN.toLowerCase() }, null);068 String apnId = null;069 if (cursor != null && cursor.moveToFirst()) {070 apnId = cursor.getString(cursor.getColumnIndex("_id"));071 }072 Log.e("NetCheck updateCurrentAPN", "apnId:" + apnId);073 // set new apn id as chosen one074 if (apnId != null) {075 ContentValues values = new ContentValues();076 values.put("apn_id", apnId);077 resolver.update(CURRENT_APN_URI, values, null, null);078 } else {079 // apn id not found, return 0.080 return 0;081 }082 } catch (SQLException e) {083 Log.e("NetCheck updateCurrentAPN", e.getMessage());084 } finally {085 if (cursor != null) {086 cursor.close();087 }088 }089 090 // update success091 return 1;092 }093 094 public String getApn(Context c) {095 boolean netSataus = false;096 097 ConnectivityManager conManager = (ConnectivityManager) c098 .getSystemService(Context.CONNECTIVITY_SERVICE);099 if (conManager.getActiveNetworkInfo() != null) {100 netSataus = conManager.getActiveNetworkInfo().isAvailable();101 102 }103 NetworkInfo info = conManager104 .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);105 String oldAPN = StringUtils.null2String(info.getExtraInfo());106 Log107 .e("NetCheck getApn", "oldAPN:" + oldAPN + " netSataus:"108 + netSataus);109 if (netSataus == false) {110 Log.e("NetCheck getApn", "setMobileDataEnabled(true)");111 setMobileDataEnabled(c, true); 112 113 try {114 Thread.sleep(4012);115 } catch (InterruptedException e) {116 e.printStackTrace();117 }118 }119 if("".equals(oldAPN)){120 updateCurrentAPN(c.getContentResolver(), "cmnet");121 try {122 Thread.sleep(1500);123 } catch (InterruptedException e) {124 e.printStackTrace();125 }126 }127 info = conManager128 .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);129 oldAPN = StringUtils.null2String(info.getExtraInfo());130 Log131 .e("NetCheck getApn", "newApn:" + oldAPN);132 return oldAPN.toLowerCase();133 }134 135 public boolean setMobileDataEnabled(Context c, boolean enabled) {136 final TelephonyManager mTelManager;137 mTelManager = (TelephonyManager) c138 .getSystemService(Context.TELEPHONY_SERVICE);139 try {140 141 Method m = mTelManager.getClass()142 .getDeclaredMethod("getITelephony");143 m.setAccessible(true);144 Object telephony = m.invoke(mTelManager);145 m = telephony.getClass().getMethod(146 (enabled ? "enable" : "disable") + "DataConnectivity");147 m.invoke(telephony);148 return true;149 } catch (Exception e) {150 Log.e("NetCheck ", "cannot fake telephony", e);151 return false;152 }153 }154 155}