Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> 安卓開發的APN網絡的開關實現

安卓開發的APN網絡的開關實現

編輯:關於android開發

  安卓開發中對於APN的網絡API沒有公開,不過我們可以閱讀源代碼,然後進行數據庫操作,系統會自動監聽數據庫的變化,從而實現開啟或者關閉APN。

  大家可以研究一下frameworks/base/core/Java/android/provider/Telephony.java這個類,比較重要的就是 URI 和數據庫字段: content://telephony/carriers。字段可以在Telephony.java中找到。

  1、當開啟APN的時候,設置一個正確的移動或者聯通的APN

  2、關閉的時候設置一個錯誤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.util.Log; import android.view.View; import android.widget.Button;

  /** * Activity

  */

  public class Main extends Activity {

  /** Called when the activity is first created. */

  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 tag = "Main.getAPNList()";

  //current不為空表示可以使用的APN

  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()){

  Log.d(tag, cr.getString(cr.getColumnIndex("_id")) + " " + cr.getString(cr.getColumnIndex("apn")) + " " + cr.getString(cr.getColumnIndex("type"))+ " " + cr.getString(cr.getColumnIndex("current")));

  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;

  }

  }

  /**

  *----分頁

  */

  public final class APNMatchTools {

  public static class APNNet{

  /* * 中國移動cmwap

  */

  public static String CMWAP = "cmwap";

  /** * 中國移動cmnet

  */

  public static String CMNET = "cmnet";

  //中國聯通3GWAP設置 中國聯通3G因特網設置 中國聯通WAP設置 中國聯通因特網設置

  //3gwap 3gnet uniwap uninet

  /** * 3G wap 中國聯通3gwap APN

  */

  public static String GWAP_3 = "3gwap";

  /** * 3G net 中國聯通3gnet APN

  */

  public static String GNET_3="3gnet";

  /** * uni wap 中國聯通uni wap APN

  */

  public static String UNIWAP="uniwap";

  /** * uni net 中國聯通uni net APN

  */

  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 "";

  // return currentName.substring(0, currentName.length() - SUFFIX.length());

  }

  }

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved