Android Wifi & APN 的控制手記
- 說明
此筆記是我公司實際上針對 WIFI 及APN使用的的程式片段,用來顯示及控制WIFI 及 APN 的開啟,關閉及新增
/*
(1) getNetStatus(this); //取得目前網路狀態(自行定義的 method)
(2) ControlWifi // ControlWifi(wifiManager, "Connect") 開啟 WIFI
// ControlWifi(wifiManager, "D"); 關閉 WIFI
// tbtnSwitch.setChecked(false); 設定 WIFI按鈕為關閉狀態
(3) ControlAPN //ControlAPN(index.this, "0") 關閉 APN
// ControlAPN(index.this, "1") 開啟 APN
//APNSwitch.setChecked(false); //設定 APN按鈕為關閉狀態
(4) InsertAPN(index.this,String name,String user,String password)
//用來新增一個 APN
(5)
*/
- 建立二個TEXT及 ToggleButton ,用於 Activity 的顯示
//.XML 的部分內容
//WIFI
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="22sp" android:id="@+id/WiFitext" android:text="@string/WiFi" android:layout_gravity="center"/>
<ToggleButton android:text="ToggleButton" android:id="@+id/WiFiCtl" android:layout_width="wrap_content" android:layout_height="wrap_content"></ToggleButton>
//APN
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="22sp" android:id="@+id/ApnText" android:text="@string/APN" android:layout_gravity="center"/>
<ToggleButton android:text="ToggleButton" android:id="@+id/ApnCtl" android:layout_width="wrap_content" android:layout_height="wrap_content"></ToggleButton>
- .java 中的部分內容
//變數定義
ToggleButton tbtnSwitch; //WIFI
ToggleButton APNSwitch; //APN
WifiManager wifiManager ;
String[] NetStats = {"","","","",""};
OnClickListener listener4 = null; //WIFI
OnClickListener listener5 = null; //APN
//onCreate 片段
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
NetStats = getNetStatus(this); //取得目前網路狀態(自行定義的 method)
tbtnSwitch = (ToggleButton) this.findViewById(R.id.WiFiCtl);
APNSwitch = (ToggleButton) this.findViewById(R.id.ApnCtl);
//初使化WIFI&APN 的 ToggleButton 在畫面中顯示開啟或關閉
if (wifiManager.getWifiState() == 3 ) { tbtnSwitch.setChecked(true);}
else { tbtnSwitch.setChecked(false);
if ( NetStats[2].equals("1") || NetStats[3].equals("0") ) { APNSwitch.setChecked(true);}
else
{ APNSwitch.setChecked(false);}
}
//WIFI Control
//當開啟 WIFI 會將 APN 關閉
listener4 = new OnClickListener() {
public void onClick(View v) {
tbtnSwitch.setEnabled(false); //暫時關閉WIFI按鈕的控制
//
if (v == tbtnSwitch)
{
if ( tbtnSwitch.isChecked()) {
try {
ControlWifi(wifiManager, "Connect"); //開啟WIFI
if ( ControlAPN(index.this, "0") ) {
APNSwitch.setChecked(false); //設定 APN按鈕為關閉狀態
}
if (wifiManager.getWifiState() == 2 || wifiManager.getWifiState() == 0) {
AlertMessage("Wifi", "開啟成功!請等待無線的圖示出來後再使用!!" );
}
else {
AlertMessage("Wifi","開啟失敗!" + String.valueOf(wifiManager.getWifiState()));
}
}
catch(Exception e) {
AlertMessage("Wifi",e.getMessage());
}
}
else {
try {
ControlWifi(wifiManager, "D"); //關閉 WIFI }
catch(Exception e) {
AlertMessage("Wifi",e.getMessage());
}
}
}
tbtnSwitch.setEnabled(true); //恢復 WIFI按鈕的控制
}
};
//APN Control
listener5 = new OnClickListener() {
public void onClick(View v) {
APNSwitch.setEnabled(false);
//
if (v == APNSwitch)
{
if ( APNSwitch.isChecked()) {
try {
if ( ControlAPN(index.this, "3") ) {
AlertMessage("APN", "APN 已開啟!\n請耐心等待3G的圖示出來後再使用!!" );
ControlWifi(wifiManager, "D");
tbtnSwitch.setChecked(false);
}
else {
AlertMessage("APN", "APN 尚未設定或設定錯誤!");
APNSwitch.setChecked(false);
}
}
catch(Exception e) {
AlertMessage("APN",e.getMessage());
}
}
else {
try {
ControlAPN(index.this, "0");
}
catch(Exception e) {
AlertMessage("APN",e.getMessage());
}
}
}
APNSwitch.setEnabled(true);
}
};
//取得目前網路狀態 getNetStatus
protected String[] getNetStatus(Context context) {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
ContentResolver cv = context.getContentResolver();
String WifiStats = android.provider.Settings.System.getString(cv,android.provider.Settings.System.WIFI_ON );
String Status =""; //0: no connect 1:Connected
String NetType =""; //0:VPN 1:WIFI
String DID[] = {"","","","",""};
//DID 說明
//DID[0] = tm.getNetworkType();
//DID[1] = WifiStats;
//DID[2] = Status;
//DID[3] = NetType;
ConnectivityManager conManager=(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = conManager.getActiveNetworkInfo();
if (info == null || !info.isConnected()) {
Status = "0";
}
else {
if (!info.isAvailable()) { Status = "0"; }
else {
Status = "1";
NetType = String.valueOf(info.getType());
}
}
//如果狀態是尚未開啟時再檢查 APN 的設定值
List<APN> list = getAPNList(context);
if ( !list.isEmpty() ) {
for (APN apn : list) {
if ( apn.apn.equals("vpn2") ) {
Status = "1";
}
else {
Status = "0";
}
}
}
DID[0] = String.valueOf(tm.getNetworkType());
DID[1] = WifiStats;
DID[2] = Status;
DID[3] = NetType;
return DID;
}
//protected boolean ControlWifi(Context context,String cmd) {
protected String ControlWifi(WifiManager wifiManager,String cmd) {
//WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
boolean stats = true;
if ( cmd.equals("Connect")) {
wifiManager.setWifiEnabled(true);
//stats = wifiManager.reconnect();
}
else {
stats = wifiManager.disconnect();
wifiManager.setWifiEnabled(false); //關閉 wifi
}
if (stats) { return "1"; } else { return "0"; }
}
private List<APN> getAPNList(Context context){
//current不為空表示可以使用的APN
String projection[] = {"_id,apn,authtype,name,user,Password"};
Cursor cr = context.getContentResolver().query(uri, projection, null, null, null);
//getContentResolver 0:_id 1:name 2:numeric 3:mcc 4:mnc 5:apn 6:user 7:server 8:password 9:proxy 10:port 11:mmsproxy 12:mmsport 13:mmsc 14:authtype 15:type
List<APN> list = new ArrayList<APN>();
while(cr!=null && cr.moveToNext()){
APN a = new APN();
if ( cr.getString(cr.getColumnIndex("name")).equals(index.user_info[0])) {
a.id = cr.getString(cr.getColumnIndex("_id"));
a.apn = cr.getString(cr.getColumnIndex("apn"));
a.authtype = cr.getString(cr.getColumnIndex("authtype"));
a.Name = cr.getString(cr.getColumnIndex("name"));
a.User = cr.getString(cr.getColumnIndex("USER"));
a.Password = cr.getString(cr.getColumnIndex("Password"));
list.add(a);
}
}
if(cr!=null)
cr.close();
return list;
}
//action 0:close 3:open
public boolean ControlAPN(Context context,String action){
//String str = "";
List<APN> list = getAPNList(context);
for (APN apn : list) {
ContentValues cv = new ContentValues();
if ( action.equals("0")) {
cv.put("apn", "close");
}
else {
cv.put("apn", "vpn2");
}
cv.put("authtype", action);
context.getContentResolver().update(uri, cv, "_id=?", new String[]{apn.id});
//}
}
if ( list.isEmpty()) { return false; } else { return true; }
// AlertMessage("APN","開啟 VPN!");
}
public static class APN{
String id;
String apn;
String authtype;
String Name;
String User;
String Password;
//String[] info;
}
//這是台灣大哥大的設定方式,其它業者也許不同,很重要的一點是 numeric 的值一定要設且為 mmc+mnc 的值,否則雖然可以新增成功但卻看不它
public int InsertAPN(Context context,String name,String user,String password) {
int id = -1;
ContentResolver resolver = context.getContentResolver();
ContentValues values = new ContentValues();
values.put("name",name);
values.put("apn","vpn2");
values.put("user",user);
values.put("password",password);
values.put("authtype","3");
values.put("mcc","466");
values.put("mnc","97");
values.put("numeric", "46697");
Cursor c = null;
try
{
Uri newRow = resolver.insert(uri, values);
if(newRow != null) {
c = resolver.query(newRow, null, null, null, null);
int idindex = c.getColumnIndex("_id");
c.moveToFirst();
id = c.getShort(idindex);
}
}
catch(Exception e)
{
return -999;
}
if(c !=null ) c.close();
return id;
}