要求:在Settings应用添加直接勾选“打开“或”关闭“移动网络
。
1.PreferenceActivity
( 1)实现系统设置界面
WirelessSettings.java
把extends Activity改为extends PreferenceActivity
。然后把java文件中的setContentView(R.layout.main);改为addPreferencesFromResource(R.xml.preferences);
G3Enabler.java
当PreferenceActivity中的内容改变时,Android系统会自动进行保存和持久化维护,我们只需要在要用的设置界面中数据的地方进行读取就可以了。同时Android还提供了OnPreferenceClickListener和OnPreferenceChangeListener
两个与Preference相关的监听接口,当PreferenceActivity中的某一个Preference进行了点击或者改变
的操作时,都会回调接口中的函数,这样可以第一个时间向其他Activity等通知系统设置进行了改变。
WirelessSettings.xml
ListPreference是一个常用的用来进行设置操作的标签。
array.xml
2.G3Enabler
G3Enabler.java
private ITelephony mPhoneService;
//使用aidl,与电话服务进程通信,这里用作打开数据连接 和 断开数据连接
mPhoneService = ITelephony.Stub.asInterface(ServiceManager.getService(Context.TELEPHONY_SERVICE));
//打开网络...
boolean state = m
PhoneService.enableDataConnectivity();
//断开网络...
mPhoneService.disableDataConnectivity();
private ITelephony mPhoneService;
//定义广播接收器,及广播处理函数onReceive
private final BroadcastReceiver mG3StateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
log("receive intent: "+intent.getAction());
if (intent.getAction().equals(TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED)) {
Phone.DataState state = getDataState(intent);
String reason = intent.getStringExtra(Phone.STATE_CHANGE_REASON_KEY);
String apnName = intent.getStringExtra(Phone.DATA_APN_KEY);
boolean unavailable = intent.getBooleanExtra(Phone.NETWORK_UNAVAILABLE_KEY, false);
switch (state) {
case DISCONNECTED:
//更新UI
break;
case CONNECTING:
//更新UI
break;
case SUSPENDED:
//更新UI
break;
case CONNECTED:
//更新UI
break;
}
} else if (intent.getAction().equals(TelephonyIntents.ACTION_DATA_CONNECTION_FAILED)) {
String reason = intent.getStringExtra(Phone.FAILURE_REASON_KEY);
Log.d(TAG, "[DataStateReceiver](ACTION_DATA_CONNECTION_FAILED)Received " + intent.getAction() + " broadcast" +reason == null ? "" : "(" + reason + ")");
gprsconnected = false;
}else if (intent.getAction().equals(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED)){
Log.d(TAG, "[DataStateReceiver](ACTION_SERVICE_STATE_CHANGED)");
mServiceState = ServiceState.newFromBundle(intent.getExtras());
}
}
};
private static Phone.DataState getDataState(Intent intent) {
String str = intent.getStringExtra(Phone.STATE_KEY);
if (str != null)
return Enum.valueOf(Phone.DataState.class, str);
else
return Phone.DataState.DISCONNECTED;
}
public G3Enabler(Context context, WifiManager wifiManager, Phone phone, CheckBoxPreference g3CheckBoxPreference) {
log("[G3Enabler](Constructor)");
mContext = context;
mG3EnableCheckBoxPref = g3CheckBoxPreference;
mOriginalSummary = g3CheckBoxPreference.getSummary();
g3CheckBoxPreference.setPersistent(false);
mWifiManager = wifiManager;
mPhone = phone;
//使用aidl,与电话服务进程通信,这里用作打开数据连接 和 断开数据连接
mPhoneService = ITelephony.Stub.asInterface(ServiceManager.getService(Context.TELEPHONY_SERVICE));
//该应用组件那能响应的广播类型
mG3StateFilter = new IntentFilter(TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED);
mG3StateFilter.addAction(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
mG3StateFilter.addAction(TelephonyIntents.ACTION_DATA_CONNECTION_FAILED);
}
public void resume() {
log("[resume]");
//注册广播接收器
mContext.registerReceiver(mG3StateReceiver, mG3StateFilter);
//为Preference注册监听接口
mG3EnableCheckBoxPref.setOnPreferenceChangeListener(this);
}
public void pause() {
log("[pause]");
mContext.unregisterReceiver(mG3StateReceiver);
mG3EnableCheckBoxPref.setOnPreferenceChangeListener(null);
}
public void destroy() {
log("[destroy]");
dismissDialog();
}
public boolean onPreferenceChange(Preference preference, Object value) {
if( (Boolean) value ){
...
//打开网络...
boolean state = mPhoneService.enableDataConnectivity();
...
}else{
...
//断开网络...
mPhoneService.disableDataConnectivity();
...
}
return true;
}
}
3.拨号脚本
(1) eference-ril.c
sprintf(wstring, "TIMEOUT 60/n");
strcat(wstring, "ABORT 'BUSY'/n");
strcat(wstring, "ABORT 'NO CARRIER'/n");
strcat(wstring,"ABORT 'NO ANSWER'/n");
strcat(wstring, "ABORT 'ERROR'/n");
strcat(wstring, "ABORT '+CME ERROR: 100'/n");
strcat(wstring, "/"/" AT/n");
asprintf(&tmp, "OK AT+CGDCONT=1,/"IP/",/"%s/",/"/",0,0/n",apn);
strcat(wstring,tmp);
strcat(wstring, "OK AT+CGEQREQ=1,2,128,2048,0,0,0,0,/"0E0/",/"0E0/",,0,0/n");
asprintf(&tmp, "OK %s/n",dialup); // ATD*98*1#
strcat(wstring,tmp);
strcat(wstring, "CONNECT/n");
fwrite(wstring, 1, strlen(wstring), fd);
fclose(fd);
/*
LOGD("create file: /etc/ppp/chat/disconnect");
fd = fopen("/etc/ppp/chat/disconnect", "w+");
if(fd == 0)
goto error;
sprintf(wstring,"ECHO ON/n");
strcat(wstring,"ABORT /"BUSY/"/n");
strcat(wstring, "ABORT /"ERROR/"/n");
strcat(wstring, "ABORT /"NO DIALTONE/"/n");
strcat(wstring,"/"/" ATH /n");
strcat(wstring,"OK AT/n");
strcat(wstring,"OK ATE0V1/n");
strcat(wstring,"OK AT/n");
asprintf(&tmp,"OK AT+CGDCONT=1,/"IP/",/"%s/",,0,0/n",apn);
strcat(wstring,tmp);
strcat(wstring,"OK ATS0");
fwrite(wstring, 1, strlen(wstring), fd);
fclose(fd);
*/
return 0;
error:
fclose(fd);
return -1;
}
错误:以上代码中 strcat(wstring, "/"/" AT/n"); 应该改为 strcat(wstring, "/"/" /rAT/n");
create_misc_file函数的作用是生成拨号脚本(/etc/ppp/peers/peer、/etc/ppp/chat/connect、/etc/ppp/chat/disconnect)
(2) 拨号脚本 - /etc/ppp/chat/connect
(3) 拨号脚本 - /etc/ppp/chat/disconnect
给Modern发送/K和ATH分别是什么作用?
[参考]
http://janseling.blog.163.com/blog/static/126653852201011152910167/
http://jianxiaza.blog.163.com/blog/static/8768562820101120111049565/
http://hi.baidu.com/uncolll/blog/item/919e8165289338fcf6365438.html