APN

版权所有,转载请注明来自Mobile Developer (http://mdev.cc )  作者  : SinFrancis

 

由于Android对于APN的网络API没有公开,不过我们可以阅读源代码,然后进行数据库操作,系统会自动监听数据库的变化,从而实现开启或者关闭APN。

 

大家可以研究一下frameworks/base/core/java/android/provider/Telephony.java这个类,

比较重要的就是 URI 和数据库字段: content://telephony/carriers

字段可以在Telephony.java中找到。

 

 

其实原理很简单 : 

1 、 当开启APN的时候,设置一个正确的移动或者联通的APN

2、 关闭的时候设置一个错误APN就会自动关闭网络

 

请看代码:Activity:

 

 

Java代码 复制代码
  1. package cc.mdev.apn;   
  2.   
  3. import java.util.ArrayList;   
  4. import java.util.List;   
  5.   
  6. import android.app.Activity;   
  7. import android.content.ContentValues;   
  8. import android.database.Cursor;   
  9. import android.net.Uri;   
  10. import android.os.Bundle;   
  11. import android.util.Log;   
  12. import android.view.View;   
  13. import android.widget.Button;   
  14.   
  15.   
  16. /**  
  17.  * 這裡是Activity  
  18.  * @author SinFrancis wong  
  19.  * @site http://mdev.cc  
  20.  * @wiki http://mdev.cc/wiki  
  21.  * @since 2010-01-08  
  22.  */  
  23. public class Main extends Activity {   
  24.     /** Called when the activity is first created. */  
  25.     Uri uri = Uri.parse("content://telephony/carriers");   
  26.     @Override  
  27.     public void onCreate(Bundle savedInstanceState) {   
  28.         super.onCreate(savedInstanceState);   
  29.         setContentView(R.layout.main);   
  30.            
  31.         Button open= (Button) findViewById(R.id.open);   
  32.         Button close= (Button) findViewById(R.id.close);   
  33.            
  34.         open.setOnClickListener(new View.OnClickListener() {   
  35.                
  36.             @Override  
  37.             public void onClick(View v) {   
  38.                 openAPN();   
  39.             }   
  40.         });   
  41.            
  42.            
  43.         close.setOnClickListener(new View.OnClickListener() {   
  44.                
  45.             @Override  
  46.             public void onClick(View v) {   
  47.                 closeAPN();   
  48.             }   
  49.         });   
  50.            
  51.     }   
  52.        
  53.     public  void openAPN(){   
  54.            
  55.         List<APN> list = getAPNList();   
  56.         for (APN apn : list) {   
  57.             ContentValues cv = new ContentValues();   
  58.             cv.put("apn", APNMatchTools.matchAPN(apn.apn));   
  59.             cv.put("type", APNMatchTools.matchAPN(apn.type));   
  60.             getContentResolver().update(uri, cv, "_id=?"new String[]{apn.id});   
  61.                
  62.         }   
  63.     }   
  64.        
  65.     public void closeAPN(){   
  66.         List<APN> list = getAPNList();   
  67.         for (APN apn : list) {   
  68.             ContentValues cv = new ContentValues();   
  69.             cv.put("apn", APNMatchTools.matchAPN(apn.apn)+"mdev");   
  70.             cv.put("type", APNMatchTools.matchAPN(apn.type)+"mdev");   
  71.             getContentResolver().update(uri, cv, "_id=?"new String[]{apn.id});   
  72.                
  73.         }   
  74.     }   
  75.        
  76.     private List<APN> getAPNList(){   
  77.         String tag = "Main.getAPNList()";   
  78.            
  79.         //current不为空表示可以使用的APN   
  80.         String  projection[] = {"_id,apn,type,current"};   
  81.         Cursor cr = this.getContentResolver().query(uri, projection, nullnullnull);   
  82.            
  83.         List<APN> list = new ArrayList<APN>();   
  84.            
  85.         while(cr!=null && cr.moveToNext()){   
  86.             Log.d(tag, cr.getString(cr.getColumnIndex("_id")) + "  " + cr.getString(cr.getColumnIndex("apn")) + "  " + cr.getString(cr.getColumnIndex("type"))+ "  " + cr.getString(cr.getColumnIndex("current")));   
  87.             APN a = new APN();   
  88.             a.id = cr.getString(cr.getColumnIndex("_id"));   
  89.             a.apn = cr.getString(cr.getColumnIndex("apn"));   
  90.             a.type = cr.getString(cr.getColumnIndex("type"));   
  91.             list.add(a);   
  92.         }   
  93.         if(cr!=null)   
  94.         cr.close();   
  95.         return list;   
  96.     }   
  97.        
  98.        
  99.     public static class APN{   
  100.         String id;   
  101.         String apn;   
  102.         String type;   
  103.     }   
  104.        
  105. }  
package cc.mdev.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
 * @author SinFrancis wong
 * @site http://mdev.cc
 * @wiki http://mdev.cc/wiki
 * @since 2010-01-08
 */
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<APN> 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<APN> 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<APN> getAPNList(){
    	String tag = "Main.getAPNList()";
    	
    	//current不为空表示可以使用的APN
    	String  projection[] = {"_id,apn,type,current"};
    	Cursor cr = this.getContentResolver().query(uri, projection, null, null, null);
    	
    	List<APN> list = new ArrayList<APN>();
    	
    	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;
    }
    
}

 

APNMatchTools.java

 

 

Java代码 复制代码
  1. package cc.mdev.apn;   
  2.   
  3.   
  4.   
  5. /**  
  6.  * 這裡是APN匹配,用於匹配移動或者聯通的APN  
  7.  * @author SinFrancis wong  
  8.  * @site http://mdev.cc  
  9.  * @wiki http://mdev.cc/wiki  
  10.  * @since 2010-01-08  
  11.  *  
  12.  */  
  13. public final class APNMatchTools {   
  14.        
  15.     public static class APNNet{   
  16.         /**  
  17.          * 中国移动cmwap  
  18.          */  
  19.         public static String CMWAP = "cmwap";   
  20.            
  21.         /**  
  22.          * 中国移动cmnet  
  23.          */  
  24.         public static String CMNET = "cmnet";   
  25.            
  26.         //中国联通3GWAP设置        中国联通3G因特网设置        中国联通WAP设置        中国联通因特网设置   
  27.         //3gwap                 3gnet                uniwap            uninet   
  28.            
  29.            
  30.         /**  
  31.          * 3G wap 中国联通3gwap APN   
  32.          */  
  33.         public static String GWAP_3 = "3gwap";   
  34.            
  35.         /**  
  36.          * 3G net 中国联通3gnet APN   
  37.          */  
  38.         public static String GNET_3="3gnet";   
  39.            
  40.         /**  
  41.          * uni wap 中国联通uni wap APN   
  42.          */  
  43.         public static String UNIWAP="uniwap";   
  44.         /**  
  45.          * uni net 中国联通uni net APN   
  46.          */  
  47.         public static String UNINET="uninet";   
  48.     }   
  49.   
  50.   
  51.   
  52.     public static String matchAPN(String currentName) {           
  53.         if("".equals(currentName) || null==currentName){   
  54.             return "";   
  55.         }   
  56.         currentName = currentName.toLowerCase();   
  57.         if(currentName.startsWith(APNNet.CMNET))   
  58.             return APNNet.CMNET;   
  59.         else if(currentName.startsWith(APNNet.CMWAP))   
  60.             return APNNet.CMWAP;   
  61.         else if(currentName.startsWith(APNNet.GNET_3))   
  62.             return APNNet.GNET_3;   
  63.         else if(currentName.startsWith(APNNet.GWAP_3))   
  64.             return APNNet.GWAP_3;   
  65.         else if(currentName.startsWith(APNNet.UNINET))   
  66.             return APNNet.UNINET;   
  67.         else if(currentName.startsWith(APNNet.UNIWAP))   
  68.             return APNNet.UNIWAP;   
  69.         else if(currentName.startsWith("default"))   
  70.             return "default";   
  71.         else return "";   
  72.        // return currentName.substring(0, currentName.length() - SUFFIX.length());   
  73.     }   
  74.        
  75.        
  76. }  
package cc.mdev.apn;



/**
 * 這裡是APN匹配,用於匹配移動或者聯通的APN
 * @author SinFrancis wong
 * @site http://mdev.cc
 * @wiki http://mdev.cc/wiki
 * @since 2010-01-08
 *
 */
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());
    }
    
    
}

 最后不要忘记加上修改APN的权限:

 

 

Xml代码 复制代码
  1. <uses-permission android:name="android.permission.WRITE_APN_SETTINGS"></uses-permission>  

 经过测试在G1 上联通和移动卡均是成功的。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值