01 | public class APNActivity extends Activity { |
02 |
03 |
public static final Uri APN_URI = Uri.parse( "content://telephony/carriers" ); |
04 |
public static final Uri CURRENT_APN_URI = Uri.parse( "content://telephony/carriers/preferapn" ); |
05 |
06 |
@Override |
07 |
public void onCreate(Bundle savedInstanceState) { |
08 |
super .onCreate(savedInstanceState); |
09 |
setContentView(R.layout.main); |
10 |
int _cmnetId = addAPN(); |
11 |
SetAPN(_cmnetId); |
12 |
} |
01 |
public void checkAPN(){ |
02 |
// 检查当前连接的APN |
03 |
Cursor cr = getContentResolver().query(CURRENT_APN_URI, null , null , |
04 |
null , null ); |
05 |
while (cr != null && cr.moveToNext()) { |
06 |
// APN id |
07 |
String id = cr.getString(cr.getColumnIndex( "_id" )); |
08 |
// APN name |
09 |
String apn = StringUtils.null2String(cr |
10 |
.getString(cr.getColumnIndex( "apn" ))); |
11 |
//Toast.makeText(getApplicationContext(), |
12 |
//"当前 id:" + id + " apn:" + apn, Toast.LENGTH_LONG).show(); |
13 |
14 |
} |
15 |
16 |
//新增一个cmnet接入点 |
17 |
public int addAPN() { |
18 |
int id = - 1 ; |
19 |
ContentResolver resolver = this .getContentResolver(); |
20 |
ContentValues values = new ContentValues(); |
21 |
values.put( "name" , "cmnet" ); |
22 |
values.put( "apn" , "cmnet" ); |
23 |
Cursor c = null ; |
24 |
Uri newRow = resolver.insert(APN_URI, values); |
25 |
if (newRow != null ) { |
26 |
c = resolver.query(newRow, null , null , null , null ); |
27 |
int idIndex = c.getColumnIndex( "_id" ); |
28 |
c.moveToFirst(); |
29 |
id = c.getShort(idIndex); |
30 |
} |
31 |
if (c != null ) |
32 |
c.close(); |
33 |
return id; |
34 |
} |
35 |
//设置接入点 |
36 |
public void SetAPN( int id) { |
37 |
ContentResolver resolver = this .getContentResolver(); |
38 |
ContentValues values = new ContentValues(); |
39 |
values.put( "apn_id" , id); |
40 |
resolver.update(CURRENT_APN_URI, values, null , null ); |
41 |
} |
42 | } |