1. 程式人生 > >開啟和關閉android行動網路

開啟和關閉android行動網路

開啟和關閉移動資料網路有兩種方法:一種是通過作業系統的資料庫改變APN(網路接入點),從而實現開啟和關閉移動資料網路,另一種是通過反射呼叫系統(ConnectivityManager)的setMoblieDataEnabled方法,通過操作該方法開啟和關閉系統移動資料,同時也可以通過反射呼叫getMoblieDataEnabled方法獲取當前的開啟和關閉狀態。

第一種方式:

   通過APN的方式開啟和關閉很威猛啊,為什麼這麼說呢,廢話不多說,先看程式碼:

   1. 匹配類:

  1. //建立一個匹配類,用於匹配移動、電信、聯通的APN
  2. publicfinalclass APNMatchTools  
  3. {  
  4.     // 中國移動cmwap
  5.     publicstatic String CMWAP = "cmwap";  
  6.     // 中國移動cmnet
  7.     publicstatic String CMNET = "cmnet";  
  8.     // 中國聯通3gwap APN
  9.     publicstatic String GWAP_3 = "3gwap";  
  10.     // 中國聯通3gnet APN
  11.     publicstatic String GNET_3 = "3gnet";  
  12.     // 中國聯通uni wap APN
  13.     publicstatic String UNIWAP = 
    "uniwap";  
  14.     // 中國聯通uni net APN
  15.     publicstatic String UNINET = "uninet";  
  16.     // 中國電信 ct wap APN
  17.     publicstatic String CTWAP = "ctwap";  
  18.     // 中國電信ct net APN
  19.     publicstatic String CTNET = "ctnet";  
  20.     publicstatic String matchAPN(String currentName)  
  21.     {  
  22.         if ("".equals(currentName) || 
    null == currentName)  
  23.         {  
  24.             return"";  
  25.         }  
  26.         // 引數轉為小寫
  27.         currentName = currentName.toLowerCase();  
  28.         // 檢查引數是否與各APN匹配,返回匹配值
  29.         if (currentName.startsWith(CMNET))  
  30.             return CMNET;  
  31.         elseif (currentName.startsWith(CMWAP))  
  32.             return CMWAP;  
  33.         elseif (currentName.startsWith(GNET_3))  
  34.             return GNET_3;  
  35.         elseif (currentName.startsWith(GWAP_3))  
  36.             return GWAP_3;  
  37.         elseif (currentName.startsWith(UNINET))  
  38.             return UNINET;  
  39.         elseif (currentName.startsWith(UNIWAP))  
  40.             return UNIWAP;  
  41.         elseif (currentName.startsWith(CTWAP))  
  42.             return CTWAP;  
  43.         elseif (currentName.startsWith(CTNET))  
  44.             return CTNET;  
  45.         elseif (currentName.startsWith("default"))  
  46.             return"default";  
  47.         else
  48.             return"";  
  49.     }  
  50. }  

2. 開啟和關閉APN的方法在ApnSwitchTest類中實現,如下:

  1. import java.util.ArrayList;  
  2. import java.util.List;  
  3. import android.app.Activity;  
  4. import android.content.ContentValues;  
  5. import android.database.Cursor;  
  6. import android.net.Uri;  
  7. import android.util.Log;  
  8. publicclass ApnSwitchTest extends Activity  
  9. {  
  10.     Uri uri = Uri.parse("content://telephony/carriers/preferapn");  
  11.     // 開啟APN
  12.     publicvoid openAPN()  
  13.     {  
  14.         List<APN> list = getAPNList();  
  15.         for (APN apn : list)  
  16.         {  
  17.             ContentValues cv = new ContentValues();  
  18.             // 獲取及儲存移動或聯通手機卡的APN網路匹配
  19.             cv.put("apn", APNMatchTools.matchAPN(apn.apn));  
  20.             cv.put("type", APNMatchTools.matchAPN(apn.type));  
  21.             // 更新系統資料庫,改變行動網路狀態
  22.             getContentResolver().update(uri, cv, "_id=?"new String[]  
  23.             {  
  24.                 apn.id  
  25.             });  
  26.         }  
  27.     }  
  28.     // 關閉APN
  29.     publicvoid closeAPN()  
  30.     {  
  31.         List<APN> list = getAPNList();  
  32.         for (APN apn : list)  
  33.         {  
  34.             // 建立ContentValues儲存資料
  35.             ContentValues cv = new ContentValues();  
  36.             // 新增"close"匹配一個錯誤的APN,關閉網路
  37.             cv.put("apn", APNMatchTools.matchAPN(apn.apn) + "close");  
  38.             cv.put("type", APNMatchTools.matchAPN(apn.type) + "close");  
  39.             // 更新系統資料庫,改變行動網路狀態
  40.             getContentResolver().update(uri, cv, "_id=?"new String[]  
  41.             {  
  42.                 apn.id  
  43.             });  
  44.         }  
  45.     }  
  46.     publicstaticclass APN  
  47.     {  
  48.         String id;  
  49.         String apn;  
  50.         String type;  
  51.     }  
  52.     private List<APN> getAPNList()  
  53.     {  
  54.         // current不為空表示可以使用的APN
  55.         String projection[] =  
  56.         {  
  57.             "_id, apn, type, current"
  58.         };  
  59.         // 查詢獲取系統資料庫的內容
  60.         Cursor cr = getContentResolver().query(uri, projection, nullnullnull);  
  61.         // 建立一個List集合
  62.         List<APN> list = new ArrayList<APN>();  
  63.         while (cr != null && cr.moveToNext())  
  64.         {  
  65.             Log.d("ApnSwitch""id" + cr.getString(cr.getColumnIndex("_id")) + " \n" + "apn"
  66.                     + cr.getString(cr.getColumnIndex("apn")) + "\n" + "type"
  67.                     + cr.getString(cr.getColumnIndex("type")) + "\n" + "current"
  68.                     + cr.getString(cr.getColumnIndex("current")));  
  69.             APN a = new APN();  
  70.             a.id = cr.getString(cr.getColumnIndex("_id"));  
  71.             a.apn = cr.getString(cr.getColumnIndex("apn"));  
  72.             a.type = cr.getString(cr.getColumnIndex("type"));  
  73.             list.add(a);  
  74.         }  
  75.         if (cr != null)  
  76.             cr.close();  
  77.         return list;  
  78.     }  
  79. }<span style="font-family: 'Comic Sans MS'; "> </span>  

  最後,別忘了在AndroidManifext.xml檔案中新增訪問許可權<uses-permission android:name="android.permission.WRITE_APN_SETTINGS" />

  親們,從上面的程式碼中看出什麼來了麼,沒錯,通過APN的方式就是修改資料庫,關閉APN其實就是給它隨便匹配一個錯誤的APN。為什麼說這種方法很生猛呢,當你通過這個方式關閉APN後,你在通過手機上的快捷開關開啟移動資料網路時,是沒效果的,也就是說開啟不了,除非你再用同樣的方法開啟APN。

第二種方式:

  這就奇怪了,關閉APN後,為什麼再通過手機上的快捷開關(AppWidget)開啟不了呢,這個問題就值得思考了,說明快捷開關其實並不是通過這個方式來開啟和關閉行動網路的。道理很簡單,想想那些快捷開關是怎麼樣根據開啟和關閉行動網路,然後更換亮和暗的圖示的呢(更新UI)。這裡肯定會涉及到一個獲取系統當前開啟和關閉移動資料狀態的問題。那到底是怎樣獲取的,是通過什麼樣的形式的?其實道理很簡單,就是通過呼叫系統的getMobileDataState和setMobileData(我是這麼知道它是呼叫到這個方法的呢?親們,如果你有android手機,把它插到電腦上,然後開啟已經搭建好的android開發環境的eclpise,開啟logcat面板,相應地在你手機的快捷開關上開啟和關閉行動網路,然後看看在logcat面板上出現什麼了)。

  既然知道是呼叫上面這兩個方法了,我們是不是就可以直接呼叫這個兩個方法實現了?NO,沒這麼簡單,這個兩個方法不能直接呼叫,必須通過反射機制呼叫(呵呵,沒接觸過java有關反射的知識的,或者是忘了的,可以去學習和溫習一下)。

  1. /** 
  2.  * 設定手機的移動資料 
  3.  */
  4. publicstaticvoid