android settings命令
16lz
2021-12-04
键字: android, settings, cmd
20180822 tjy
转载请注明出处
Android 中有一个可执行文件settings,可以使得调试变得方便。
查看settings的代码,会有其他额外发现。
文件位置
device-name:/system/bin # ls -l settingsls -l settings-rwxr-xr-x 1 root shell 35 2018-08-03 04:17 settingsdevice-name:/system/bin #
settings用法
device-name:/ # settingssettingsSettings provider (settings) commands: help Print this help text. get [--user | current] NAMESPACE KEY Retrieve the current value of KEY. put [--user | current] NAMESPACE KEY VALUE [TAG] [default] Change the contents of KEY to VALUE. TAG to associate with the setting. {default} to set as the default, case-insensitive only for global/secure namespace delete NAMESPACE KEY Delete the entry for KEY. reset [--user | current] NAMESPACE {PACKAGE_NAME | RESET_MODE} Reset the global/secure table for a package with mode. RESET_MODE is one of {untrusted_defaults, untrusted_clear, trusted_defaults}, case-insensitive list NAMESPACE Print all defined keys. NAMESPACE is one of {system, secure, global}, case-insensitive
settings list命令
device-name:/ # settings list globalsettings list globaladb_enabled=1add_users_when_locked=0airplane_mode_on=0airplane_mode_radios=cell,bluetooth,wifi,nfc,wimaxairplane_mode_toggleable_radios=bluetooth,wifi,nfcassisted_gps_enabled=1att_wifi_call_enable=0audio_safe_volume_state=3auto_time=1auto_time_zone=1bluetooth_disabled_profiles=0bluetooth_on=0boot_count=5call_auto_retry=0captive_portal_mode=1car_dock_sound=/system/media/audio/ui/Dock.oggcar_undock_sound=/system/media/audio/ui/Undock.oggcdma_cell_broadcast_sms=1data_roaming0=1data_roaming=1default_install_location=0default_restrict_background_data=0desk_dock_sound=/system/media/audio/ui/Dock.oggdesk_undock_sound=/system/media/audio/ui/Undock.oggdevelopment_settings_enabled=1device_provisioned=1dock_audio_media_enabled=1dock_sounds_enabled=0dock_sounds_enabled_when_accessbility=0emergency_affordance_needed=0emergency_tone=0heads_up_notifications_enabled=1lock_sound=/system/media/audio/ui/Lock.ogglow_battery_sound=/system/media/audio/ui/LowBattery.ogglow_battery_sound_timeout=0mobile_data0=1mobile_data=1mobile_data_always_on=0mode_ringer=2netstats_enabled=1network_recommendations_enabled=0package_verifier_enable=1power_sounds_enabled=1preferred_network_mode=10set_install_location=0stay_on_while_plugged_in=0subscription_mode=0theater_mode_on=0trusted_sound=/system/media/audio/ui/Trusted.oggunlock_sound=/system/media/audio/ui/Unlock.oggusb_mass_storage_enabled=1verifier_verify_adb_installs=0volte_vt_enabled=1wfc_ims_enabled=0wfc_ims_mode=1wifi_display_on=0wifi_max_dhcp_retry_count=9wifi_networks_available_notification_on=1wifi_on=0wifi_scan_always_enabled=0wifi_sleep_policy=2wifi_wakeup_available=0wifi_wakeup_enabled=1wireless_charging_started_sound=/system/media/audio/ui/WirelessChargingStarted.ogg
settings put和get
device-name:/ # settings get global bluetooth_onsettings get global bluetooth_on0device-name:/ # settings put global bluetooth_on 1settings put global bluetooth_on 1device-name:/ #
代码位置在frameworks/base/cmds/settings/settings,打开这个文件,发现它是一个shell,调用了cmd命令,cmd命令又在哪里呢?
//from http://androidxref.com/8.1.0_r33/xref/frameworks/base/cmds/settings/settings#!/system/bin/shcmd settings "$@"
在frameworks/native/cmds/cmd/cmd.cpp中,代码如下。
cmd通过IServiceManager去实现操作。
通过看代码,cmd -l可以把service都列出来。
cmd settings是通过调用 service settings中的方法来完成的。
//from http://androidxref.com/8.1.0_r33/xref/frameworks/native/cmds/cmd/cmd.cppint main(int argc, char* const argv[]){ signal(SIGPIPE, SIG_IGN); sp proc = ProcessState::self(); // setThreadPoolMaxThreadCount(0) actually tells the kernel it's // not allowed to spawn any additional threads, but we still spawn // a binder thread from userspace when we call startThreadPool(). // This is safe because we only have 2 callbacks, neither of which // block. // See b/36066697 for rationale proc->setThreadPoolMaxThreadCount(0); proc->startThreadPool(); sp sm = defaultServiceManager(); fflush(stdout); if (sm == NULL) { ALOGW("Unable to get default service manager!"); aerr << "cmd: Unable to get default service manager!" << endl; return 20; } if (argc == 1) { aerr << "cmd: No service specified; use -l to list all services" << endl; return 20; } if ((argc == 2) && (strcmp(argv[1], "-l") == 0)) { Vector services = sm->listServices(); services.sort(sort_func); aout << "Currently running services:" << endl; for (size_t i=0; i service = sm->checkService(services[i]); if (service != NULL) { aout << " " << services[i] << endl; } } return 0; } Vector args; for (int i=2; i service = sm->checkService(cmd); if (service == NULL) { ALOGW("Can't find service %s", argv[1]); aerr << "cmd: Can't find service: " << argv[1] << endl; return 20; } sp cb = new MyShellCallback(); sp result = new MyResultReceiver();#if DEBUG ALOGD("cmd: Invoking %s in=%d, out=%d, err=%d", argv[1], STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO);#endif // TODO: block until a result is returned to MyResultReceiver. status_t err = IBinder::shellCommand(service, STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO, args, cb, result); if (err < 0) { const char* errstr; switch (err) { case BAD_TYPE: errstr = "Bad type"; break; case FAILED_TRANSACTION: errstr = "Failed transaction"; break; case FDS_NOT_ALLOWED: errstr = "File descriptors not allowed"; break; case UNEXPECTED_NULL: errstr = "Unexpected null"; break; default: errstr = strerror(-err); break; } ALOGW("Failure calling service %s: %s (%d)", argv[1], errstr, -err); aout << "cmd: Failure calling service " << argv[1] << ": " << errstr << " (" << (-err) << ")" << endl; return err; } cb->mActive = false; status_t res = result->waitForResult();#if DEBUG ALOGD("result=%d", (int)res);#endif return res;}
继续追一下IBinder::shellCommand代码,定义在frameworks/native/libs/binder/Binder.cpp。
后续补充Binder的逻辑。
//from http://androidxref.com/8.1.0_r33/xref/frameworks/native/libs/binder/Binder.cpp#65status_t IBinder::shellCommand(const sp& target, int in, int out, int err, Vector& args, const sp& callback, const sp& resultReceiver){ Parcel send; Parcel reply; send.writeFileDescriptor(in); send.writeFileDescriptor(out); send.writeFileDescriptor(err); const size_t numArgs = args.size(); send.writeInt32(numArgs); for (size_t i = 0; i < numArgs; i++) { send.writeString16(args[i]); } send.writeStrongBinder(callback != NULL ? IInterface::asBinder(callback) : NULL); send.writeStrongBinder(resultReceiver != NULL ? IInterface::asBinder(resultReceiver) : NULL); return target->transact(SHELL_COMMAND_TRANSACTION, send, &reply);}
作者:tjy_2011
链接:https://www.jianshu.com/p/409bd28ce8b4
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
更多相关文章
- SeekBar自定义
- android NDK环境搭建
- android获取手机信息
- Android隐藏标题栏,全屏显示
- android中判断网络是否连接
- android中webview开启了硬件加速后会出现闪烁问题
- Android(安卓)和 PHP 之间进行数据加密传输
- android上传图片到服务器,求服务器那边和android的Activity的完整
- android实用代码