android settings命令

键字: android, settings, cmd

20180822 tjy

转载请注明出处

Android 中有一个可执行文件settings,可以使得调试变得方便。
查看settings的代码,会有其他额外发现。

文件位置

 

device-name:/system/bin # ls -l settings
ls -l settings
-rwxr-xr-x 1 root shell 35 2018-08-03 04:17 settings
device-name:/system/bin #

settings用法

 

device-name:/ # settings
settings
Settings provider (settings) commands:
  help
      Print this help text.
  get [--user <USER_ID> | current] NAMESPACE KEY
      Retrieve the current value of KEY.
  put [--user <USER_ID> | 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 <USER_ID> | 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 global
settings list global
adb_enabled=1
add_users_when_locked=0
airplane_mode_on=0
airplane_mode_radios=cell,bluetooth,wifi,nfc,wimax
airplane_mode_toggleable_radios=bluetooth,wifi,nfc
assisted_gps_enabled=1
att_wifi_call_enable=0
audio_safe_volume_state=3
auto_time=1
auto_time_zone=1
bluetooth_disabled_profiles=0
bluetooth_on=0
boot_count=5
call_auto_retry=0
captive_portal_mode=1
car_dock_sound=/system/media/audio/ui/Dock.ogg
car_undock_sound=/system/media/audio/ui/Undock.ogg
cdma_cell_broadcast_sms=1
data_roaming0=1
data_roaming=1
default_install_location=0
default_restrict_background_data=0
desk_dock_sound=/system/media/audio/ui/Dock.ogg
desk_undock_sound=/system/media/audio/ui/Undock.ogg
development_settings_enabled=1
device_provisioned=1
dock_audio_media_enabled=1
dock_sounds_enabled=0
dock_sounds_enabled_when_accessbility=0
emergency_affordance_needed=0
emergency_tone=0
heads_up_notifications_enabled=1
lock_sound=/system/media/audio/ui/Lock.ogg
low_battery_sound=/system/media/audio/ui/LowBattery.ogg
low_battery_sound_timeout=0
mobile_data0=1
mobile_data=1
mobile_data_always_on=0
mode_ringer=2
netstats_enabled=1
network_recommendations_enabled=0
package_verifier_enable=1
power_sounds_enabled=1
preferred_network_mode=10
set_install_location=0
stay_on_while_plugged_in=0
subscription_mode=0
theater_mode_on=0
trusted_sound=/system/media/audio/ui/Trusted.ogg
unlock_sound=/system/media/audio/ui/Unlock.ogg
usb_mass_storage_enabled=1
verifier_verify_adb_installs=0
volte_vt_enabled=1
wfc_ims_enabled=0
wfc_ims_mode=1
wifi_display_on=0
wifi_max_dhcp_retry_count=9
wifi_networks_available_notification_on=1
wifi_on=0
wifi_scan_always_enabled=0
wifi_sleep_policy=2
wifi_wakeup_available=0
wifi_wakeup_enabled=1
wireless_charging_started_sound=/system/media/audio/ui/WirelessChargingStarted.ogg

settings put和get

 

device-name:/ # settings get global bluetooth_on
settings get global bluetooth_on
0
device-name:/ # settings put global bluetooth_on 1
settings put global bluetooth_on 1
device-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/sh
cmd 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.cpp

int main(int argc, char* const argv[])
{
    signal(SIGPIPE, SIG_IGN);
    sp<ProcessState> 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<IServiceManager> 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<String16> services = sm->listServices();
        services.sort(sort_func);
        aout << "Currently running services:" << endl;

        for (size_t i=0; i<services.size(); i++) {
            sp<IBinder> service = sm->checkService(services[i]);
            if (service != NULL) {
                aout << "  " << services[i] << endl;
            }
        }
        return 0;
    }

    Vector<String16> args;
    for (int i=2; i<argc; i++) {
        args.add(String16(argv[i]));
    }
    String16 cmd = String16(argv[1]);
    sp<IBinder> 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<MyShellCallback> cb = new MyShellCallback();
    sp<MyResultReceiver> 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#65

status_t IBinder::shellCommand(const sp<IBinder>& target, int in, int out, int err,
    Vector<String16>& args, const sp<IShellCallback>& callback,
    const sp<IResultReceiver>& 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
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
android adb shell 命令大全 1. 显示系统中全部Android平台: android list targets 2. 显示系统中全部AVD(模拟器): android list avd 3. 创建AVD(模拟器): android create avd --name 名称 --target 平台编号 4. 启动模拟器: emulator -avd 名称 -sdcard ~/名称.img (-skin 1280x800) 5. 删除AVD(模拟器): android delete avd --name 名称 6. 创建SDCard: mksdcard 1024M ~/名称.img 7. AVD(模拟器)所在位置: Linux(~/.android/avd) Windows(C:\Documents and Settings\Administrator\.android\avd) 8. 启动DDMS: ddms 9. 显示当前运行的全部模拟器: adb devices 10. 对某一模拟器执行命令: abd -s 模拟器编号 命令 11. 安装应用程序: adb install -r 应用程序.apk 12. 获取模拟器中的文件: adb pull 13. 向模拟器中写文件: adb push 14. 进入模拟器的shell模式: adb shell 15. 启动SDK,文档,实例下载管理器: android 16. 缷载apk包: adb shell cd data/app rm apk包 exit adb uninstall apk包的主包名 adb install -r apk包 17. 查看adb命令帮助信息: adb help 18. 在命令行中查看LOG信息: adb logcat -s 标签名 19. adb shell后面跟的命令主要来自: 源码\system\core\toolbox目录和源码\frameworks\base\cmds目录。 20. 删除系统应用: adb remount (重新挂载系统分区,使系统分区重新可写)。 adb shell cd system/app rm *.apk 21. 获取管理员权限: adb root 22. 启动Activity: adb shell am start -n 包名/包名+类名(-n 类名,-a action,-d date,-m MIME-TYPE,-c category,-e 扩展数据,等)。 23、发布端口: 你可以设置任意的端口号,做为主机向模拟器或设备的请求端口。如: adb forward tcp:5555 tcp:8000 24、复制文件: 你可向一个设备或从一个设备中复制文件, 复制一个文件或目录到设备或模拟器上: adb push 如:adb push test.txt /tmp/test.txt 从设备或模拟器上复制一个文件或目录: adb pull 如:adb pull /addroid/lib/libwebcore.so . 25、搜索模拟器/设备的实例: 取得当前运行的模拟器/设备的实例的列表及每个实例的状态: adb devices 26、查看bug报告: adb bugreport 27、记录无线通讯日志: 一般来说,无线通讯的日志非常多,在运行时没必要去记录,但我们还是可以通过命令,设置记录: adb shell logcat -b radio 28、获取设备的ID和序列号: adb get-product adb get-serialno 29、访问数据库SQLite3 adb shell sqlite3 #cd system/sd/data //进入系统内指定文件夹 #ls //列表显示当前文件夹内容 #rm -r xxx //删除名字为xxx的文件夹及其里面的所有文件 #rm xxx //删除文件xxx #rmdir xxx //删除xxx的文件夹

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值