3G的断开与连接可以通过修改网络接入点APN来实现--写入一个错误的APN 网络即断开 写入正确的APN 网络即可连接
开发板实验:正确的APN-- 3GNET 错误的APN --3wap
主要代码:
package com.cart.view;
import java.util.ArrayList;
import java.util.List;
import com.cart.vo.ApnInfo;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;
public class Apn {
String allPath="content://telephony/carriers";
String currentPath="content://telephony/carriers/preferapn";
Uri allUri,currentUri;
ContentResolver cResolver;
public Apn(Context context)
{
allUri=Uri.parse(allPath);
currentUri=Uri.parse(currentPath);
cResolver=context.getContentResolver();
}
/**
* 获取所有APN
* @return apnList
* @author fangcm 2011-08-19
*/
public List<ApnInfo> getAllApn()
{
List<ApnInfo> apnList=new ArrayList<ApnInfo>();
Cursor cr=cResolver.query(allUri, null, null, null, null);
while (cr!=null&&cr.moveToNext()) {
ApnInfo apnInfo=new ApnInfo();
apnInfo.setApnId(cr.getString(cr.getColumnIndex("_id")));
apnInfo.setApnName(cr.getString(cr.getColumnIndex("apn")));
cr.getString(cr.getColumnIndex("current"));
apnList.add(apnInfo);
}
return apnList;
}
/**
* 获取当前选中APN
* @return 当前APN
* @author fangcm 2011-08-19
*/
public ApnInfo getCurrentApn()
{
ApnInfo apnInfo=null;
Cursor cr=cResolver.query(currentUri, null, null, null, null);
while (cr!=null&&cr.moveToNext()) {
apnInfo=new ApnInfo();
apnInfo.setApnId(cr.getString(cr.getColumnIndex("_id")));
apnInfo.setApnName(cr.getString(cr.getColumnIndex("apn")));
cr.getString(cr.getColumnIndex("current"));
Log.d("carWifi", "---getCurrentApn-----====="+apnInfo.getApnId());
}
return apnInfo;
}
/**
* 添加APN
* @param apnInfo 新apn
* @return uri 新apn uri
* @author fangcm 2011-08-19
*/
public Uri addApn(ApnInfo apnInfo)
{
Uri uri = null;
if (apnInfo!=null) {
ContentValues values=new ContentValues();
values.put("name", apnInfo.getApnName());
values.put("apn", apnInfo.getApnName());
values.put("mcc", apnInfo.getMcc());
values.put("mnc", apnInfo.getMnc());
values.put("numeric", apnInfo.getNumeric());
uri=cResolver.insert(allUri, values);
Log.d("carWifi", "---addApn-uri----====="+uri);
}
return uri;
}
/**
* 更改APN
* @param apnInfo 默认apn
* @return 1 成功
* @author fangcm 2011-08-19
*/
public int updateApn(ApnInfo apnInfo)
{
int x=-1;
ContentValues values=new ContentValues();
values.put("apn_id", apnInfo.getApnId());
values.put("numeric", apnInfo.getNumeric());
x=cResolver.update(currentUri, values, null, null);
Log.d("carWifi", apnInfo.getApnId()+"---updateApn-----====="+x);
return x;
}
/**
* 获取指定apn的id
* @param uri
* @return
* @author fangcm 2011-08-19
*/
public String getApnId(Uri uri)
{
String id="-1";
if (uri!=null) {
Cursor cr=cResolver.query(uri, null, null, null, null);
while (cr!=null&&cr.moveToNext()) {
id=cr.getString(cr.getColumnIndex("_id"));
}
}
Log.d("carWifi", "---getApnId-----====="+id);
return id;
}
}