Android中如何通过反射调用setUsbTethering()方法 15

 我来答
好程序员
2016-04-19 · HTML5前端培训/大数据培训/Java
好程序员
好程序员是IT高端课程培训基地,从平凡到卓越,为梦想而拼搏。
向TA提问
展开全部
Usb
setting 中tethering 设置流程

一 资源位置及入口文件

USB
tethering, usb_tethering_button_text

Xml/tether_prefs.xml------usb_tether_settings,布局文件

packages/apps/Settings/AndroidManifest.xml

<!-- Keep compatibility with old
shortcuts. -->

<activity-alias
android:name=".TetherSettings"

android:label="@string/tether_settings_title_all"

android:clearTaskOnLaunch="true"

android:exported="true"

android:targetActivity="Settings$TetherSettingsActivity">

<meta-data
android:name="com.android.settings.FRAGMENT_CLASS"

android:value="com.android.settings.TetherSettings"
/>

<meta-data
android:name="com.android.settings.TOP_LEVEL_HEADER_ID"

android:resource="@id/wireless_settings"
/>

<meta-data
android:name="com.android.settings.PARENT_FRAGMENT_TITLE"

android:resource="@string/wireless_networks_settings_title"
/>

<meta-data
android:name="com.android.settings.PARENT_FRAGMENT_CLASS"

android:value="com.android.settings.Settings$WirelessSettingsActivity"
/>

</activity-alias>

TetherSettings.java

二 触发流程

2.1
TetherSettings.java

packages/apps/Settings/src/com/android/settings/TetherSettings.java

onPreferenceTreeClick

SXlog.d(TAG,
"onPreferenceTreeClick - setusbTethering(" + newState + ")
mUsbTethering: " + mUsbTethering);

setUsbTethering(true);

下面是设置时抓取的log日志

D/TetherSettings( 543): onPreferenceTreeClick -
setusbTethering(true) mUsbTethering: true

D/Tethering(
271): setUsbTethering(true)

D/UsbDeviceManager( 271):
setCurrentFunction(rndis) default: false

W/UsbDeviceManager( 271): handleMessage:
2

W/UsbDeviceManager( 271): setEnabledFunctions:
functions = rndis

W/UsbDeviceManager( 271): setEnabledFunctions,
mDefaultFunctions: mtp,adb

W/UsbDeviceManager( 271): setEnabledFunctions,
mCurrentFunctions: mtp,adb

D/UsbDeviceManager( 271): setEnabledFunctions,
mSettingFunction: mtp,adb

W/UsbDeviceManager( 271): containsFunction,
functions: rndis

W/UsbDeviceManager( 271): containsFunction,
function: adb

W/UsbDeviceManager( 271): containsFunction
index: -1

W/UsbDeviceManager( 271): addFunction,
functions: rndis

W/UsbDeviceManager( 271): addFunction, function:
adb

W/UsbDeviceManager( 271): containsFunction,
functions: rndis

W/UsbDeviceManager( 271): containsFunction,
function: acm

W/UsbDeviceManager( 271): containsFunction
index: -1

W/UsbDeviceManager( 271): containsFunction,
functions: rndis,adb

W/UsbDeviceManager( 271): containsFunction,
function: acm

W/UsbDeviceManager( 271): containsFunction
index: -1

D/UsbDeviceManager( 271):
setUsbConfig(none)

W/UsbDeviceManager( 271): setUsbConfig, config:
none

public
boolean onPreferenceTreeClick(PreferenceScreen
screen, Preference preference) {

ConnectivityManager cm =

(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);

if
(preference == mUsbTether) {

if
(!mUsbTethering) {

boolean
newState = mUsbTether.isChecked();

mUsbTethering = true;

mUsbTetherCheckEnable = false;

mUsbTether.setEnabled(false);

SXlog.d(TAG,
"onPreferenceTreeClick - setusbTethering(" + newState + ")
mUsbTethering: " + mUsbTethering);

if
(newState) {

startProvisioningIfNecessary(USB_TETHERING);

} else
{

setUsbTethering(newState);

}

} else
{

return
true;

}

} else if
(preference == mBluetoothTether) {

private void
setUsbTethering(boolean
enabled) {

ConnectivityManager cm =

(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);

if
(cm.setUsbTethering(enabled) !=
ConnectivityManager.TETHER_ERROR_NO_ERROR) {

mUsbTether.setChecked(false);

mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);

return;

}

mUsbTether.setSummary("");

}

2.2
ConnectivityManager.java

frameworks/base/core/java/android/net/ConnectivityManager.java

private
final IConnectivityManager
mService;

public int
setUsbTethering(boolean
enable) {

try
{

return
mService.setUsbTethering(enable);

} catch
(RemoteException e) {

return
TETHER_ERROR_SERVICE_UNAVAIL;

}

}

manager 和service有一个对应关系,固定的规则。 manager是为了sdk诞生的,方便app开发者调用。其实可以直接调用service,如mountservice是没有mountmanager的。

service是在系统起来是就被android系统启动的,而manager是后期有需要时实例化起来的。

Service的目录在:/frameworks/base/services/java/com/android/server/

manager的目录在:frameworks/base/core/java/android

2.3
ConnectivityService.java

frameworks/base/services/java/com/android/server

private
Tethering mTethering;

public int
setUsbTethering(boolean
enable) {

enforceTetherAccessPermission();

if
(isTetheringSupported()) {

return
mTethering.setUsbTethering(enable);

} else
{

return
ConnectivityManager.TETHER_ERROR_UNSUPPORTED;

}

}

2.4
Tethering.java

frameworks/base/services/java/com/android/server/connectivity/Tethering.java

public int
setUsbTethering(boolean
enable) {

UsbManager usbManager = (UsbManager)mContext.getSystemService(Context.USB_SERVICE);

………………

else
{

mUsbTetherRequested = true;

usbManager.setCurrentFunction(UsbManager.USB_FUNCTION_RNDIS,
false);

}

通过service,来获取manager,进行操作。这个tethering.java的构造函数

public
Tethering(Context context,
INetworkManagementService nmService,

INetworkStatsService statsService,
IConnectivityManager connService, Looper looper)
{

Context是获取全局信息的接口,

2.5 UsbManager.java

/frameworks/base/core/java/android/hardware/usb/UsbManager.java

public void setCurrentFunction(String
function, boolean makeDefault) {

try {

mService.setCurrentFunction(function,
makeDefault);

}
catch (RemoteException e) {

Log.e(TAG, "RemoteException in
setCurrentFunction", e);

}

}

2.6
frameworks/base/services/java/com/android/server/usb$

mengfd1@tablet-C:~/work/A2107/frameworks/base/services/java/com/android/server$
cd usb

mengfd1@tablet-C:~/work/A2107/frameworks/base/services/java/com/android/server/usb$
ls

UsbDeviceManager.java UsbHostManager.java
UsbService.java UsbSettingsManager.java

/frameworks/base/services/java/com/android/server/usb/UsbService.java

public void setCurrentFunction(String function, boolean
makeDefault) {

mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB,
null);

if
(mDeviceManager != null) {

mDeviceManager.setCurrentFunction(function,
makeDefault);

}
else {

throw new IllegalStateException("USB device
mode not supported");

}

}

/frameworks/base/services/java/com/android/server/usb/UsbDeviceManager.java

public void setCurrentFunction(String
function, boolean makeDefault) {

if
(DEBUG) Slog.d(TAG, "setCurrentFunction(" + function + ") default:
" + makeDefault);

mHandler.sendMessage(MSG_SET_CURRENT_FUNCTION,
function, makeDefault);

}

public void handleMessage(Message msg)
{

case
MSG_SET_CURRENT_FUNCTION:

String function =
(String)msg.obj;

boolean makeDefault = (msg.arg1 ==
1);

if (function != null
&&
function.equals(UsbManager.USB_FUNCTION_CHARGING_ONLY))
{

mSettingUsbCharging = true;

SXlog.d(TAG, "handleMessage -
MSG_SET_CURRENT_FUNCTION - USB_FUNCTION_CHARGING_ONLY -
makeDefault: " + makeDefault);

} else {

mSettingUsbCharging = false;

}

setEnabledFunctions(function,
makeDefault);

SXlog.d(TAG, "handleMessage -
MSG_SET_CURRENT_FUNCTION - function: " +
function);

break;

private void setEnabledFunctions(String
functions, boolean makeDefault) {

private boolean setUsbConfig(String config) {

SystemProperties.set("sys.usb.config",
config);
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式