您的位置:首页 > 移动开发 > Android开发

android 系统数据业务---打开/关闭概述

2017-07-17 19:03 501 查看

数据业务的打开/关闭

一般的app都可以实现数据业务的打开和关闭,例如手机里面的设置,数据业务的打开和关闭都是通过

TelephonyManager接口来实现,具体的代码如下,

设置默认卡的打开和关闭,

public void setDataEnabled(boolean enable) {
setDataEnabled(SubscriptionManager.getDefaultDataSubId(), enable);
}


设置卡一或者卡二的打开/关闭,

public void setDataEnabled(int subId, boolean enable) {
try {
Log.d(TAG, "setDataEnabled: enabled=" + enable);
getITelephony().setDataEnabled(subId, enable);
} catch (RemoteException e) {
Log.e(TAG, "Error calling ITelephony#setDataEnabled", e);
}
}

下面是获取卡的状态,打开/关闭,

public boolean getDataEnabled() {
return getDataEnabled(SubscriptionManager.getDefaultDataSubId());
}


public boolean getDataEnabled(int subId) {
boolean retVal = false;
try {
retVal = getITelephony().getDataEnabled(subId);
} catch (RemoteException e) {
Log.e(TAG, "Error calling ITelephony#getDataEnabled", e);
} catch (NullPointerException e) {
}
Log.d(TAG, "getDataEnabled: retVal=" + retVal);
return retVal;
}

首先调用getITelephony方法获取服务端对象,然后调用对应的方法, getITelephony方法如下,

private ITelephony getITelephony() {
return ITelephony.Stub.asInterface(
ServiceManager.getService(Context.TELEPHONY_SERVICE));
}

获取的是PhoneInterfaceManager对象,

TelephonyManager和PhoneInterfaceManager分别是binder机制的两端, TelephonyManager是客户端,

PhoneInterfaceManager是服务端,运行于phone进程中,

PhoneInterfaceManager定义如下,

public class PhoneInterfaceManager extends ITelephony.Stub {

PhoneInterfaceManager的setDataEnabled方法调用流程图如下,



setDataEnabled方法的逻辑如下,

public void setDataEnabled(int subId, boolean enable) {
enforceModifyPermission();//检查权限
int phoneId = mSubscriptionController.getPhoneId(subId);//获取phone id
Phone phone = PhoneFactory.getPhone(phoneId);//获取phone对象
if (phone != null) {
phone.setDataEnabled(enable);//调用setDataEnabled方法
} else {
}
}

获取的phone一般为GSMPhone或者CDMAPhone对象,以GSMPhone为例来论述,

GSMPhone的setDataEnabled方法如下,

public void setDataEnabled(boolean enable) {
mDcTracker.setDataEnabled(enable);
}

调用DcTracker的setDataEnabled方法,在其父类DcTrackerBase中实现,

public void setDataEnabled(boolean enable) {
Message msg = obtainMessage(DctConstants.CMD_SET_USER_DATA_ENABLE);
msg.arg1 = enable ? 1 : 0;
if (DBG) log("setDataEnabled: sendMessage: enable=" + enable);
sendMessage(msg);
}

这里发送消息,主要是切换到主线程中执行,

DcTrackerBase对CMD_SET_USER_DATA_ENABLE消息处理如下,

case DctConstants.CMD_SET_USER_DATA_ENABLE: {
final boolean enabled = (msg.arg1 == DctConstants.ENABLED) ? true : false;
if (DBG) log("CMD_SET_USER_DATA_ENABLE enabled=" + enabled);
onSetUserDataEnabled(enabled);
break;
}

onSetUserDataEnabled方法主要逻辑如下,

1,和上次的phone状态对比,

if (mUserDataEnabled != enabled) {
mUserDataEnabled = enabled;

如果和上次的状态一样,就没有必要进行更进一步的操作,然后保存这次的状态.

2,将状态值保存到数据库,

if (TelephonyManager.getDefault().getSimCount() == 1) {
Settings.Global.putInt(mResolver, Settings.Global.MOBILE_DATA, enabled ? 1 : 0);
} else {
int phoneSubId = mPhone.getSubId();
Settings.Global.putInt(mResolver, Settings.Global.MOBILE_DATA + phoneSubId,
enabled ? 1 : 0);
}

3,通知apn断开,

if (getDataOnRoamingEnabled() == false &&
mPhone.getServiceState().getDataRoaming() == true) {
if (enabled) {
notifyOffApnsOfAvailability(Phone.REASON_ROAMING_ON);
} else {
notifyOffApnsOfAvailability(Phone.REASON_DATA_DISABLED);
}
}

4,打开/关闭 数据业务

if (enabled) {
onTrySetupData(Phone.REASON_DATA_ENABLED);
} else {
onCleanUpAllConnections(Phone.REASON_DATA_SPECIFIC_DISABLED);
}

分2部分进行论述,首先论述数据业务打开的详细流程,然后论述关闭的详细流程。

onTrySetupData和onTrySetupData方法都是在子类DcTracker中实现的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息