获取当前APN,更改APN

1.获取APN

ConnectivityManager conManager = 
   (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
  NetworkInfo ni = conManager.getActiveNetworkInfo(); 
//获取网络接入点,中国移动:cmwap和cmnet; 中国电信ctwap,ctnet
String apn = ni.getExtraInfo();

获取接入点方式,需要权限:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>

2.如何更改APN接入点呢?

//---->android4.0 后无法修改APN
public int updateCurrentAPN(ContentResolver resolver, String newAPN) { 
        Cursor cursor = null; 
        try { 
            //get new apn id from list 4.2后只限系统应用有权限使用(query当前APN)
            cursor = resolver.query(APN_LIST_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")); 
            } 
            cursor.close(); 
            
            //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); 
                
                //通知apn已经更改
                IntentFilter upIntentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); 
                registerReceiver(new NetworkChangeReceiver (), upIntentFilter);  
                
            } else { 
                //apn id not found, return 0. 
                return 0; 
            } 
        } catch (SQLException e) { 
        } finally { 
            if (cursor != null) { 
                cursor.close(); 
            } 
        } 
        
        //update success 
        return 1; 
} 

更改APN需要的权限为:

<uses-permission android:name="android.permission.WRITE_APN_SETTINGS"></uses-permission>


你可能感兴趣的:(android,android,android,apn)