关于APN的一些东西1

效果:

当点击添加新APN会自动添加,如果已经添加会直接设置成默认选中使用当前的APN

查询会查询出所有的已知的APN

并且以list显示出来,在list里点击可以直接设置成默认选中的APN或者删除当前选中的APN

权限:

View Code
1 <uses-permission android:name="android.permission.WRITE_APN_SETTINGS"></uses-permission>
2 <uses-permission android:name="android.permission.INTERNET"></uses-permission>
3 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

XML:

1

main.xml

主界面

View Code
1 <?xml version="1.0" encoding="utf-8"?>
2  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:orientation="vertical" android:layout_width="fill_parent"
4 android:layout_height="fill_parent">
5 <TextView android:layout_width="fill_parent"
6 android:layout_height="wrap_content" android:text="ApnProject"/>
7 <Button android:layout_width="fill_parent"
8 android:layout_height="wrap_content" android:id="@+id/addButton"
9 android:text="添加新的APN"/>
10 <Button android:layout_width="fill_parent"
11 android:layout_height="wrap_content" android:id="@+id/allButton"
12 android:text="查询所有APN"/>
13  </LinearLayout>

2

showapn.xml

显示查询所有的APN

View Code
1 <?xml version="1.0" encoding="utf-8"?>
2  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent" android:layout_height="fill_parent">
4 <ListView android:id="@+id/listview" android:layout_width="fill_parent"
5 android:layout_height="fill_parent"/>
6  </LinearLayout>

3

apn.xml

listview中使用的元素

View Code
1 <?xml version="1.0" encoding="utf-8"?>
2  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent" android:layout_height="fill_parent"
4 android:orientation="vertical">
5 <TextView android:layout_width="fill_parent"
6 android:layout_height="wrap_content" android:id="@+id/_id"/>
7 <TextView android:layout_width="fill_parent"
8 android:layout_height="wrap_content" android:id="@+id/name"/>
9 <TextView android:layout_width="fill_parent"
10 android:layout_height="wrap_content" android:id="@+id/apn"/>
11  </LinearLayout>

下面的是java代码:

MainActivity.java

View Code
1 package com.hsm.activity;
2
3  import android.app.Activity;
4 import android.content.ContentResolver;
5 import android.content.ContentUris;
6 import android.content.ContentValues;
7 import android.content.Context;
8 import android.content.Intent;
9 import android.database.Cursor;
10 import android.net.Uri;
11 import android.os.Bundle;
12 import android.telephony.TelephonyManager;
13 import android.util.Log;
14 import android.view.View;
15 import android.view.View.OnClickListener;
16 import android.widget.Button;
17 import android.widget.Toast;
18
19 publicclass MainActivity extends Activity
20 {
21 private Button addButton;
22 private Button allButton;
23 // 获取所有的APN所用的URI
24 privatestaticfinal Uri APN_TABLE_URI = Uri.parse("content://telephony/carriers");
25 // 获取当前使用的APN所用的uri
26 privatestaticfinal Uri PREFERRED_APN_URI = Uri.parse("content://telephony/carriers/preferapn");
27 privateint addID;
28
29 /** Called when the activity is first created. */
30 @Override
31 publicvoid onCreate(Bundle savedInstanceState)
32 {
33 super.onCreate(savedInstanceState);
34 setContentView(R.layout.main);
35 addButton = (Button) findViewById(R.id.addButton);
36 addButton.setOnClickListener(addOnClickListener);
37 allButton = (Button) findViewById(R.id.allButton);
38 allButton.setOnClickListener(allOnClickListener);
39 }
40
41 private OnClickListener allOnClickListener =new OnClickListener() {
42 publicvoid onClick(View v)
43 {
44 // TODO Auto-generated method stub
45 Intent mIntent =new Intent();
46 mIntent.setClass(MainActivity.this, ShowAllApnActivity.class);
47 startActivity(mIntent);
48 }
49 };
50 // 设置添加APN的按钮监听
51 private OnClickListener addOnClickListener =new OnClickListener() {
52 publicvoid onClick(View v)
53 {
54 // TODO Auto-generated method stub
55 addAPN();
56 // addNewApn();
57 }
58 };
59
60 // 添加APN
61 void addAPN()
62 {
63 Cursor cursor_all = getContentResolver().query(APN_TABLE_URI, null,
64 null, null, null);// 所有的移动网络设置中的APN列表
65 if (cursor_all.getCount() >0)
66 {
67 while (cursor_all !=null&& cursor_all.moveToNext())
68 {
69 if (cursor_all.getString(cursor_all.getColumnIndex("proxy")).equals(
70 "10.0.0.200"))
71 {
72 // ||cursor_all.getString(cursor_all.getColumnIndex("apn")).equals("ctnet")
73 Toast.makeText(MainActivity.this, "您所添加的APN已有直接设置为默认选择",
74 Toast.LENGTH_SHORT).show();
75 ContentResolver resolver = getContentResolver();
76 ContentValues values =new ContentValues();
77 values.put(
78 "apn_id",
79 cursor_all.getString(cursor_all.getColumnIndex("_id")));
80 resolver.update(PREFERRED_APN_URI, values, null, null);
81 break;
82 }
83 else
84 {
85 addNewApn();
86 break;
87 }
88 }
89 }
90 else
91 {
92 addNewApn();
93 }
94 }
95
96 // 获得mnc
97 private String getMNC()
98 {
99 TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
100 String numeric = tm.getSimOperator();
101 String mnc = numeric.substring(3, numeric.length());
102 return mnc;
103 }
104
105 // 获得numeric
106 private String getSimOperator()
107 {
108 TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
109 String SimOperator = tm.getSimOperator();
110 return SimOperator;
111 }
112
113 // 获得mcc
114 private String getMCC()
115 {
116 TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
117 String numeric = tm.getSimOperator();
118 String mcc = numeric.substring(0, 3);
119 return mcc;
120 }
121
122 void addNewApn()
123 {
124 ContentResolver resolver = getContentResolver();
125 ContentValues values =new ContentValues();
126 values.put("name", "cheer");
127 values.put("apn", "cmnet");
128 //values.put("proxy", "10.0.0.200");
129 values.put("port", "80");
130 values.put("user", "card");
131 values.put("password", "card");
132 values.put("mcc", getMCC());
133 values.put("mnc", getMNC());
134 values.put("numeric", getSimOperator());
135 // values.put("name", "中国电信CTNET");
136 // values.put("apn", "ctnet");
137 // values.put("proxy", "10.0.0.200");
138 // values.put("port", "80");
139 // values.put("user", "card");
140 // values.put("password", "card");
141 // values.put("mcc", getMCC());
142 // values.put("mnc", getMNC());
143 // values.put("numeric", getSimOperator());
144 Cursor c =null;
145 Uri newRow = resolver.insert(APN_TABLE_URI, values);
146 if (newRow !=null)
147 {
148 c = resolver.query(newRow, null, null, null, null);
149 int idindex = c.getColumnIndex("_id");
150 c.moveToFirst();
151 addID = c.getShort(idindex);
152 }
153 }
154
155 @Override
156 protectedvoid onDestroy()
157 {
158 // TODO Auto-generated method stub
159 super.onDestroy();
160 ContentResolver resolver = getContentResolver();
161 Uri deleteIdUri = ContentUris.withAppendedId(
162 APN_TABLE_URI,
163 addID);
164 resolver.delete(deleteIdUri, null, null);
165 }
166
167 }

ShowAllApnActivity.java

View Code
1 package com.hsm.activity;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7
8 import android.app.Activity;
9 import android.app.AlertDialog;
10 import android.content.ContentResolver;
11 import android.content.ContentUris;
12 import android.content.ContentValues;
13 import android.content.DialogInterface;
14 import android.database.Cursor;
15 import android.net.Uri;
16 import android.os.Bundle;
17 import android.util.Log;
18 import android.view.View;
19 import android.widget.AdapterView;
20 import android.widget.ListView;
21
22 import com.hsm.uitl.ApnArrayAdapter;
23
24 publicclass ShowAllApnActivity extends Activity
25 {
26 private List<Map<String, Object>> mData;
27 private ListView mListView;
28 private ApnArrayAdapter mApnArrayAdapter;
29 // 获取所有的APN所用的URI
30 privatestaticfinal Uri APN_TABLE_URI = Uri.parse("content://telephony/carriers");
31 // 获取当前使用的APN所用的uri
32 privatestaticfinal Uri PREFERRED_APN_URI = Uri.parse("content://telephony/carriers/preferapn");
33
34 @Override
35 protectedvoid onCreate(Bundle savedInstanceState)
36 {
37 super.onCreate(savedInstanceState);
38 this.setContentView(R.layout.showapn);
39 mData = allAPN();
40 mListView = (ListView) findViewById(R.id.listview);
41 mApnArrayAdapter =new ApnArrayAdapter(this, mData);
42 mListView.setAdapter(mApnArrayAdapter);
43 mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
44 publicvoid onItemClick(AdapterView<?> arg0, View arg1,
45 int position, long arg3)
46 {
47 final Map<String, Object> map = (Map<String, Object>) mApnArrayAdapter.getItem(position);
48
49 Log.d("------------------------------", map.get("_id") +"");
50 new AlertDialog.Builder(ShowAllApnActivity.this).setTitle("标题").setMessage(
51 "简单消息框").setPositiveButton("删除该条APN",
52 new DialogInterface.OnClickListener() {
53
54 publicvoid onClick(DialogInterface dialog,
55 int which)
56 {
57 ContentResolver resolver = getContentResolver();
58 Uri deleteIdUri = ContentUris.withAppendedId(
59 APN_TABLE_URI,
60 Long.parseLong(map.get("_id") +""));
61 resolver.delete(deleteIdUri, null, null);
62 }
63 }).setNeutralButton("设置为默认APN",
64 new DialogInterface.OnClickListener() {
65 publicvoid onClick(DialogInterface dialog,
66 int which)
67 {
68 ContentResolver resolver = getContentResolver();
69 ContentValues values =new ContentValues();
70 values.put("apn_id", map.get("_id") +"");
71 resolver.update(PREFERRED_APN_URI, values,
72 null, null);
73 }
74 }).setNegativeButton("取消", null).show();
75 }
76 });
77 }
78
79 List<Map<String, Object>> allAPN()
80 {
81 List<Map<String, Object>> list =new ArrayList<Map<String, Object>>();
82 Map<String, Object> map;
83 Cursor cursor_all = getContentResolver().query(APN_TABLE_URI, null,
84 null, null, null);// 所有的移动网络设置中的APN列表
85 while (cursor_all !=null&& cursor_all.moveToNext())
86 {
87 map =new HashMap<String, Object>();
88 map.put("_id",
89 cursor_all.getString(cursor_all.getColumnIndex("_id")));
90 map.put("name",
91 cursor_all.getString(cursor_all.getColumnIndex("name")));
92 map.put("apn",
93 cursor_all.getString(cursor_all.getColumnIndex("apn")));
94 list.add(map);
95 }
96 return list;
97 }
98 }

ApnArrayAdapter.java

View Code
1 package com.hsm.uitl;
2
3 import java.util.List;
4 import java.util.Map;
5 import java.util.logging.Logger;
6
7 import com.hsm.activity.R;
8
9 import android.R.integer;
10 import android.content.ContentResolver;
11 import android.content.ContentUris;
12 import android.content.Context;
13 import android.net.Uri;
14 import android.util.Log;
15 import android.view.LayoutInflater;
16 import android.view.View;
17 import android.view.ViewGroup;
18 import android.widget.ArrayAdapter;
19 import android.widget.BaseAdapter;
20 import android.widget.Button;
21 import android.widget.ImageView;
22 import android.widget.TextView;
23
24 publicclass ApnArrayAdapter extends BaseAdapter
25 {
26 private LayoutInflater mInflater;
27 private List<Map<String, Object>> mData;
28
29 public ApnArrayAdapter(Context context, List<Map<String, Object>> mData)
30 {
31 this.mInflater = LayoutInflater.from(context);
32 this.mData = mData;
33 }
34
35 publicint getCount()
36 {
37 return mData.size();
38 }
39
40 public Object getItem(int arg0)
41 {
42 return mData.get(arg0);
43 }
44
45 publiclong getItemId(int arg0)
46 {
47 return arg0;
48 }
49
50 public View getView(int position, View convertView, ViewGroup parent)
51 {
52 ViewHolder holder =null;
53 if (convertView ==null)
54 {
55 holder =new ViewHolder();
56 convertView = mInflater.inflate(R.layout.apn, null);
57 holder._id = (TextView) convertView.findViewById(R.id._id);
58 holder.name = (TextView) convertView.findViewById(R.id.name);
59 holder.apn = (TextView) convertView.findViewById(R.id.apn);
60 convertView.setTag(holder);
61 }
62 else
63 {
64 holder = (ViewHolder) convertView.getTag();
65 }
66 holder._id.setText("id is : "
67 + (String) mData.get(position).get("_id"));
68 holder.name.setText("name is : "
69 + (String) mData.get(position).get("name"));
70 holder.apn.setText("apn is : "
71 + (String) mData.get(position).get("apn"));
72 return convertView;
73 }
74
75 publicfinalclass ViewHolder
76 {
77 public TextView _id;
78 public TextView name;
79 public TextView apn;
80 }
81 }
posted @ 2011-06-15 09:29  飞翔的熊猫  阅读(654)  评论(0编辑  收藏  举报