Android手机(移动GSM)在休眠或开机后不能成功启用网络链接(设置都正常),有时候甚至状态栏图标是连接的,但网络依旧不可用。
如下解决方法,不知可通用,但测试HTC野火手机移动版可使用:
(被这个问题折腾死了,从本站相关文章可以看到,之前尝试了APN切换也不行,估计是网络enable==false)
import java.lang.reflect.Method;
004
005
import android.content.ContentResolver;
006
import android.content.ContentValues;
007
import android.content.Context;
008
import android.database.Cursor;
009
import android.database.SQLException;
010
import android.net.ConnectivityManager;
011
import android.net.NetworkInfo;
012
import android.net.Uri;
013
import android.telephony.TelephonyManager;
014
import android.util.Log;
015
/**
016
* Wizzer.cn
017
* @author Wizzer
018
*
019
*/
020
public class NetCheck {
021
public static final Uri APN_URI = Uri.parse("content://telephony/carriers");
022
public static final Uri DEFAULTAPN_URI = Uri
023
.parse("content://telephony/carriers/restore");
024
public static final Uri CURRENT_APN_URI = Uri
025
.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:" + curApnId
039
+ " apnName1:" + apnName1);
040
// find apn name from apn list
041
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(cursor
046
.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 list
066
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 one
074
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 success
091
return 1;
092
}
093
094
public String getApn(Context c) {
095
boolean netSataus = false;
096
097
ConnectivityManager conManager = (ConnectivityManager) c
098
.getSystemService(Context.CONNECTIVITY_SERVICE);
099
if (conManager.getActiveNetworkInfo() != null) {
100
netSataus = conManager.getActiveNetworkInfo().isAvailable();
101
102
}
103
NetworkInfo info = conManager
104
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
105
String oldAPN = StringUtils.null2String(info.getExtraInfo());
106
Log
107
.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 = conManager
128
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
129
oldAPN = StringUtils.null2String(info.getExtraInfo());
130
Log
131
.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) c
138
.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
}