天天看点

开关Android的APN网络

由于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. import  java.util.ArrayList;  
  3. import  java.util.List;  
  4. import  android.app.Activity;  
  5. import  android.content.ContentValues;  
  6. import  android.database.Cursor;  
  7. import  android.net.Uri;  
  8. import  android.os.Bundle;  
  9. import  android.util.Log;  
  10. import  android.view.View;  
  11. import  android.widget.Button;  
  12. public   class  Main  extends  Activity {  
  13.     Uri uri = Uri.parse("content://telephony/carriers" );  
  14.     @Override   
  15.     public   void  onCreate(Bundle savedInstanceState) {  
  16.         super .onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main);  
  18.         Button open= (Button) findViewById(R.id.open);  
  19.         Button close= (Button) findViewById(R.id.close);  
  20.         open.setOnClickListener(new  View.OnClickListener() {  
  21.             @Override   
  22.             public   void  onClick(View v) {  
  23.                 openAPN();  
  24.             }  
  25.         });  
  26.         close.setOnClickListener(new  View.OnClickListener() {  
  27.             @Override   
  28.             public   void  onClick(View v) {  
  29.                 closeAPN();  
  30.             }  
  31.         });  
  32.     }  
  33.     public    void  openAPN(){  
  34.         List<APN> list = getAPNList();  
  35.         for  (APN apn : list) {  
  36.             ContentValues cv = new  ContentValues();  
  37.             cv.put("apn" , APNMatchTools.matchAPN(apn.apn));  
  38.             cv.put("type" , APNMatchTools.matchAPN(apn.type));  
  39.             getContentResolver().update(uri, cv, "_id=?" ,  new  String[]{apn.id});  
  40.         }  
  41.     }  
  42.     public   void  closeAPN(){  
  43.         List<APN> list = getAPNList();  
  44.         for  (APN apn : list) {  
  45.             ContentValues cv = new  ContentValues();  
  46.             cv.put("apn" , APNMatchTools.matchAPN(apn.apn)+ "mdev" );  
  47.             cv.put("type" , APNMatchTools.matchAPN(apn.type)+ "mdev" );  
  48.             getContentResolver().update(uri, cv, "_id=?" ,  new  String[]{apn.id});  
  49.         }  
  50.     }  
  51.     private  List<APN> getAPNList(){  
  52.         String tag = "Main.getAPNList()" ;  
  53.         //current不为空表示可以使用的APN   
  54.         String  projection[] = {"_id,apn,type,current" };  
  55.         Cursor cr = this .getContentResolver().query(uri, projection,  null ,  null ,  null );  
  56.         List<APN> list = new  ArrayList<APN>();  
  57.         while (cr!= null  && cr.moveToNext()){  
  58.             Log.d(tag, cr.getString(cr.getColumnIndex("_id" )) +  "  "  + cr.getString(cr.getColumnIndex( "apn" )) +  "  "  + cr.getString(cr.getColumnIndex( "type" ))+  "  "  + cr.getString(cr.getColumnIndex( "current" )));  
  59.             APN a = new  APN();  
  60.             a.id = cr.getString(cr.getColumnIndex("_id" ));  
  61.             a.apn = cr.getString(cr.getColumnIndex("apn" ));  
  62.             a.type = cr.getString(cr.getColumnIndex("type" ));  
  63.             list.add(a);  
  64.         }  
  65.         if (cr!= null )  
  66.         cr.close();  
  67.         return  list;  
  68.     }  
  69.     public   static   class  APN{  
  70.         String id;  
  71.         String apn;  
  72.         String type;  
  73.     }  
  74. }  
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. public   final   class  APNMatchTools {  
  3.     public   static   class  APNNet{  
  4.         public   static  String CMWAP =  "cmwap" ;  
  5.         public   static  String CMNET =  "cmnet" ;  
  6.         //中国联通3GWAP设置        中国联通3G因特网设置        中国联通WAP设置        中国联通因特网设置   
  7.         //3gwap                 3gnet                uniwap            uninet   
  8.         public   static  String GWAP_3 =  "3gwap" ;  
  9.         public   static  String GNET_3= "3gnet" ;  
  10.         public   static  String UNIWAP= "uniwap" ;  
  11.         public   static  String UNINET= "uninet" ;  
  12.     }  
  13.     public   static  String matchAPN(String currentName) {          
  14.         if ( "" .equals(currentName) ||  null ==currentName){  
  15.             return   "" ;  
  16.         }  
  17.         currentName = currentName.toLowerCase();  
  18.         if (currentName.startsWith(APNNet.CMNET))  
  19.             return  APNNet.CMNET;  
  20.         else   if (currentName.startsWith(APNNet.CMWAP))  
  21.             return  APNNet.CMWAP;  
  22.         else   if (currentName.startsWith(APNNet.GNET_3))  
  23.             return  APNNet.GNET_3;  
  24.         else   if (currentName.startsWith(APNNet.GWAP_3))  
  25.             return  APNNet.GWAP_3;  
  26.         else   if (currentName.startsWith(APNNet.UNINET))  
  27.             return  APNNet.UNINET;  
  28.         else   if (currentName.startsWith(APNNet.UNIWAP))  
  29.             return  APNNet.UNIWAP;  
  30.         else   if (currentName.startsWith( "default" ))  
  31.             return   "default" ;  
  32.         else   return   "" ;  
  33.        // return currentName.substring(0, currentName.length() - SUFFIX.length());   
  34.     }  
  35. }  
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的权限: