Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

安卓開發進階:以程式碼實現 APN 網路的開關切換

編輯:關於 Android 開發

Android 並未公開 APN(接入點名稱)相關的 API,但我們可以研究原始碼,透過操作系統的內容提供者(ContentProvider)資料庫來達成目的——系統會自動監聽資料庫變化,進而開啟或關閉網路。

關鍵在於研究 frameworks/base/core/java/android/provider/Telephony.java 這個類別,重要的 URI 是 content://telephony/carriers,各個資料庫欄位則可在該類別中找到。

基本思路是:開啟時設定一個正確的移動或聯通 APN;關閉時則故意設定一個錯誤的 APN,網路便會自動中斷。

 import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.ContentValues; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.Button; public class Main extends Activity { Uri uri = Uri.parse("content://telephony/carriers"); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button open= (Button) findViewById(R.id.open); Button close= (Button) findViewById(R.id.close); open.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { openAPN(); } }); close.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { closeAPN(); } }); } public void openAPN(){ List list = getAPNList(); for (APN apn : list) { ContentValues cv = new ContentValues(); cv.put("apn", APNMatchTools.matchAPN(apn.apn)); cv.put("type", APNMatchTools.matchAPN(apn.type)); getContentResolver().update(uri, cv, "_id=?", new String[]{apn.id}); } } public void closeAPN(){ List list = getAPNList(); for (APN apn : list) { ContentValues cv = new ContentValues(); cv.put("apn", APNMatchTools.matchAPN(apn.apn)+"mdev"); cv.put("type", APNMatchTools.matchAPN(apn.type)+"mdev"); getContentResolver().update(uri, cv, "_id=?", new String[]{apn.id}); } } private List getAPNList(){ String projection[] = {"_id,apn,type,current"}; Cursor cr = this.getContentResolver().query(uri, projection, null, null, null); List list = new ArrayList(); while(cr!=null && cr.moveToNext()){ APN a = new APN(); a.id = cr.getString(cr.getColumnIndex("_id")); a.apn = cr.getString(cr.getColumnIndex("apn")); a.type = cr.getString(cr.getColumnIndex("type")); list.add(a); } if(cr!=null) cr.close(); return list; } public static class APN{ String id; String apn; String type; } }

輔助工具類 APNMatchTools 則負責比對並正規化電信商名稱,例如 cmnet、cmwap、3gwap、3gnet、uniwap、uninet 等:

 public final class APNMatchTools { public static class APNNet{ public static String CMWAP = "cmwap"; public static String CMNET = "cmnet"; public static String GWAP_3 = "3gwap"; public static String GNET_3="3gnet"; public static String UNIWAP="uniwap"; public static String UNINET="uninet"; } public static String matchAPN(String currentName) { if("".equals(currentName) || null==currentName){ return ""; } currentName = currentName.toLowerCase(); if(currentName.startsWith(APNNet.CMNET)) return APNNet.CMNET; else if(currentName.startsWith(APNNet.CMWAP)) return APNNet.CMWAP; else if(currentName.startsWith(APNNet.GNET_3)) return APNNet.GNET_3; else if(currentName.startsWith(APNNet.GWAP_3)) return APNNet.GWAP_3; else if(currentName.startsWith(APNNet.UNINET)) return APNNet.UNINET; else if(currentName.startsWith(APNNet.UNIWAP)) return APNNet.UNIWAP; else if(currentName.startsWith("default")) return "default"; else return ""; } }

需要提醒的是,此手法依賴非公開的系統接口,通常需要系統權限或 root 才能正常運作,且不同 Android 版本間可能有差異,僅供研究參考。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved