Skip to content

Instantly share code, notes, and snippets.

@slashthinking
Created January 17, 2013 17:24
Show Gist options
  • Save slashthinking/4557706 to your computer and use it in GitHub Desktop.
Save slashthinking/4557706 to your computer and use it in GitHub Desktop.
ConnectivityManager connectivity = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
// 获取网络连接管理的对象
NetworkInfo info = connectivity.getActiveNetworkInfo();
if (info != null && info.isConnected()) {
// 判断当前网络是否已经连接
if (info.getState() == NetworkInfo.State.CONNECTED) {
if(info.getTypeName().equals("WIFI")){
else{
Uri uri = Uri.parse("content://telephony/carriers/preferapn");
Cursor cr = context.getContentResolver().query(uri, null,null, null, null);
while (cr != null && cr.moveToNext()) {
// APN id
@SuppressWarnings("unused")
String id = cr.getString(cr.getColumnIndex("_id"));
// APN name
@SuppressWarnings("unused")
String apn = cr.getString(cr.getColumnIndex("apn"));
// do other things...
String strProxy = cr.getString(cr.getColumnIndex("proxy"));
String strPort = cr.getString(cr.getColumnIndex("port"));
if(strProxy != null && !"".equals(strProxy)){
Config.host = strProxy;
Config.port = Integer.valueOf(strPort);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment