天道酬勤,学无止境

Understanding command through adb shell and through code - Android

I am trying to give BATTERSTATS permission to an app. When I run the command from a PC - it works just fine:

adb shell pm grant com.example.sample.myapplication android.permission.BATTERY_STATS

But the same pm grant command does not work when run from Android app:

java.lang.Process process = Runtime.getRuntime().exec("pm grant com.example.sample.myapplication android.permission.BATTERY_STATS");

Does it require root permission to give this permission? If it is so, why it does not require the device to be rooted to run it via adb shell?

I am new to Android, please explain a bit more clearly what is happening inside and how to proceed.

评论

The command you run with adb shell gets executed with shell(UID=2000) user privileges. The command you run from your java code gets executed with your app's UID privileges. Thus the difference.

受限制的 HTML

  • 允许的HTML标签:<a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • 自动断行和分段。
  • 网页和电子邮件地址自动转换为链接。

相关推荐
  • adb shell su works but adb root does not
    I rooted my unlocked Galaxy S3 (SGH-T999) Now, I'm trying to run adb root from Windows Command Prompt, however, I'm getting adbd cannot run as root in production builds error. So, the very first thing I checked was whether my phone was really rooted? So I tried the following: Open Command prompt $adb devices // lists my device $adb shell //goes to shell $su // opens a 'SuperSu' prompt on my phone and I 'Grant' permission # // Before following the rooting instructions, I was getting 'no su command found' in the previous step. So, I believe my phone is ROOTED. **Correct me if I'm wrong.**
  • Debugging Android NDK native apps
    I'm trying to debug and step through an Android application that segfaults. I've tried ndk-gdb, but with little luck. I've also referred to Android NDK Debugging without being able to debug my app. When I try ndk-gdb --start, and I get: $ ndk-gdb --start --verbose Android NDK installation path: /opt/android-ndk-r7 Using default adb command: /opt/android-sdk-linux/platform-tools/adb ADB version found: Android Debug Bridge version 1.0.29 Using final ADB command: '/opt/android-sdk-linux/platform-tools/adb' Using auto-detected project path: . Found package name: com.example.native_plasma ABIs
  • macbook adb cannot open interface
    I am encountering a problem with Android ADB debugging through USB. Using Macbook Air 2013 and trying to connect Nexus 6 dev phone. On the MAC Android Studio, android latest sdk is installed. When I do adb kill-server adb devices I get: adb I 661 9881 usb_osx.cpp:259] Found vid=18d1 pid=**** serial=***** adb I 661 9881 usb_osx.cpp:259] adb E 661 9881 usb_osx.cpp:331] Could not open interface: e00002c5 adb E 661 9881 usb_osx.cpp:265] Could not find device interface So it found the device, vendor id, product id and serial match what I find in the system information for the Nexus 6. Following
  • Is it possible to execute adb commands through my android app?
    Can anyone say, whether adb commands can be executed through my android application. If it is possible to execute, how it can be implemented?
  • 从Android L及更高版本开始,无法再调用setMobileDataEnabled方法(The setMobileDataEnabled method is no longer callable as of Android L and later)
    问题 我已将与setMobileDataEnabled()方法不再可通过反射进行调用的问题记录在Google上的问题78084中。 从Android 2.1(API 7)到Android 4.4(API 19)可以通过反射调用,但是从Android L及更高版本开始,即使使用root用户, setMobileDataEnabled()方法也不能调用。 官方的答复是,此问题为“已关闭”,并且状态设置为“ WorkingAsIntended”。 Google的简单解释是: 私有API是私有的,因为它们不稳定,并且可能会在不通知的情况下消失。 是的,Google,我们知道使用反射来调用隐藏方法的风险-即使在Android出现之前也是如此-但您需要提供更可靠的答案,以获取与setMobileDataEnabled() 。 (如果您对我的决定不满意,请登录78084期,并对其进行尽可能多的注视,以使Google知道他们的做法有误。) 因此,我想问的是:在Android设备上以编程方式启用或禁用移动网络功能时,我们是否走到了尽头? Google采取的这种笨拙的方法在我看来并不理想。 如果您有适用于Android 5.0(Lollipop)及更高版本的解决方法,我很想听听您在此主题中的回答/讨论。 我使用下面的代码查看setMobileDataEnabled()方法是否可用: final
  • Multiple commands through JSch shell
    I was trying to execute multiple commands through SSH protocol using the JSch library. But I seem to have stuck and cannot find any solution. The setCommand() method can only execute single commands per session. But I want to execute the commands sequentially just like the connectbot app on the Android platform. So far my code is: package com.example.ssh; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.Properties; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget
  • How to read “adb shell dumpsys alarm” output
    I'm struggling with setting an alarm properly, and understanding the mechanism of cancelling and rescheduling alarms. I have found, that there is an adb command to retrieve all alarms scheduled on device, but I haven't found a documentation, explaining the format of the output. I do understand, that I'm asking a lot of explanations here, so if anybody will throw a link with detailed explanation about "adb shell dumpsys alarm", I will really appreciate it. So, here are the questions: Pending alarm batches: 23 a. Is '23' a number of currently active, scheduled alarms? Batch{4293d3a8 num=1 start
  • Getting the battery current values for the Android Phone
    I am trying to collect power usage statistics for the Android G1 Phone. I am interested in knowing the values of Voltage and Current, and then able to collect statistics as reported in this PDF. I am able to get the value of Battery voltage through registering for an intent receiver to receive the Broadcast for ACTION_BATTERY_CHANGED. But the problem is that Android does not expose the value of current through this SDK interface. One way I tried is via sysfs interface, where I can view the battery current value from adb shell, using the following command $cat /sys/class/power_supply/battery
  • Android ADB device offline, can't issue commands
    I can't connect to my device anymore using ADB through the command line or in Eclipse. Running the command adb devices returns the device name, but it says it's offline. Things I've tried. Toggled Android debugging mode Reinstalled the Google USB driver Restored the OS to a previously working backup (CyanogenMod) Swapped the USB cord Rebooted the phone/computer multiple times Updated the Android SDK I really don't have any clue what's going on. Anything else you think I can try, I'm all ears. To be clear, if you're having this same issue the problem is probably an out-of-date SDK. As of 4.2.2
  • 那些做Android开发必须知道的ADB命令
    以下记录一些比较常用的命令adb start-server启动adb服务,如果它没启动的话adb kill-server关闭服务adb devices查看所连接的设备以及设备所对应的序列号adb install -r xxxx.apk安装app,需要注意的是如果连接了两台设备,则会报错,此时可以添加-s <serialNumber>来处理adb uninstall packagename卸载app,有时候在手机上卸载App会出现数据清理不干净,导致App再也装不上了,这个时候可以敲命令来卸载adb shell进入shell环境adb shell pm clear packagename清除应用的数据,很常用吧?adb shell am start -n packagename/packagename.activityname启动某个应用的某个Activity(以前调试老年机,那种Launcher上没有APP的机器,全靠它啊!!!!!!!)adb connect <device-ip-address>连接到指定的ip,这个通常配合wifidebugadb shell dumpsys activity top查看栈顶Activity,可以用来获取包名,可以用来查看其它app的包名adb shell ps查看进程信息adb shell pm list packages
  • How can one pull the (private) data of one's own Android app?
    Attempting to pull a single file using adb pull /data/data/com.corp.appName/files/myFile.txt myFile.txt fails with failed to copy '/data/data/com.corp.appName/files/myFile.txt myFile.txt' to 'myFile.txt': Permission denied despite that USB debugging is enabled on the device. We can go around the problem through the archaic route adb shell run-as com.corp.appName cat files/myFile.txt > myFile.txt but this is unwieldy for more than one file. How can I pull the directory /data/data/com.corp.appName/files to my MacBook? Doing this either directly or through a transit in `/storage/sdcard0/myDir
  • How to record screen and take screenshots, using Android API?
    Background Android got a new API on Kitkat and Lollipop, to video capture the screen. You can do it either via the ADB tool or via code (starting from Lollipop). Ever since the new API was out, many apps came to that use this feature, allowing to record the screen, and Microsoft even made its own Google-Now-On-tap competitor app. Using ADB, you can use: adb shell screenrecord /sdcard/video.mp4 You can even do it from within Android Studio itself. The problem I can't find any tutorial or explanation about how to do it using the API, meaning in code. What I've found The only place I've found is
  • 如何从Android设备获取apk文件?(How do I get an apk file from an Android device?)
    问题 如何从Android设备获取apk文件? 或者如何将apk文件从设备传输到系统? 回答1 使用adb。 当设备通过USB连接时,可以使用adb pull将文件从设备复制到系统中。 当然,您还需要正确的权限才能访问文件所在的目录。如果没有,则需要首先对设备进行root用户操作。 如果您发现许多APK都命名为“ base.apk”,则还可以使用此命令将所有APK从您可以访问的电话中拉出,同时将任何“ base.apk”名称重命名为程序包名称。 这也解决了名称后似乎带有随机字符的APK路径的目录找不到问题: for i in $(adb shell pm list packages | awk -F':' '{print $2}'); do adb pull "$(adb shell pm path $i | awk -F':' '{print $2}')"; mv base.apk $i.apk 2&> /dev/null ;done 如果收到“ adb:错误:无法统计远程对象”,则表明您没有所需的权限。 我在未扎根的Moto Z2上运行了该程序,并且能够下载除youtube之外我未卸载的所有APK(请参见下文)。 adb shell pm uninstall --user 0 com.android.cellbroadcastreceiver <--- kills
  • 通过Wi-Fi运行/安装/调试Android应用程序?(Run/install/debug Android applications over Wi-Fi?)
    问题 我认为有一种方法可以通过Wi-Fi在开发中测试您的应用程序。 这可能吗? 我很希望能够拆开手机并进行无线开发。 回答1 查看论坛帖子是否可以无root权限远程查看Android屏幕? -发布#9 。 通过USB连接设备并确保调试正常; adb tcpip 5555 。 这使设备开始监听端口5555上的连接。 使用6.0或更高版本的adb shell netcfg或adb shell ifconfig查找设备IP地址; 您可以立即断开USB连接。 adb connect <DEVICE_IP_ADDRESS>:5555 。 这将连接到我们在步骤2上在设备上设置的服务器。 现在,您有了一个可以通过网络进行调试的设备。 要将服务器切换回USB模式,请运行adb usb ,这会将手机上的服务器恢复为USB模式。 如果有多个设备,则可以使用-s选项指定设备: adb -s <DEVICE_IP_ADDRESS>:5555 usb 。 无需root! 要查找设备的IP地址:运行adb shell ,然后运行netcfg 。 您会在那看到它。 要在使用OSX时查找IP地址,请运行命令adb shell ip route 。 警告:启用该选项很危险,即使您处于数据网络中,网络中的任何人都可以通过调试连接到设备。 仅在连接到受信任的Wi-Fi时执行此操作,并记住在完成连接后将其断开连接!
  • 如何为Android Shell构建可执行文件(How to build an executable for Android shell)
    问题 有时我需要在设备上运行命令或脚本,但是它们不可用或不存在。 除了可以在Android设备的shell使用的命令以外,我们是否可以向其添加其他命令? 例如,向我的设备添加screenrecord命令(我的设备的Android API低于19),该命令在其上不可用。 我知道如何使用adb shell获取设备上可用命令的列表 adb shell ls /system/bin 但我想添加更多的自定义命令和脚本,以完成一些特殊的工作。 有什么办法吗? 还是不可能? 回答1 答案提供了一系列步骤,可通过Eclipse (已过时)和Android Studio (在撰写本文时为4.1+)为Android shell构建可执行文件。 最后一个包括ndk-build和CMake 。 I.准备源代码 作为示例,请考虑mycommand.c : #include <stdio.h> int main() { printf("My Command!\n"); return 0; } 二。 可执行 Eclipse(可能已过时) 假设在Eclipse设置了NDK位置,请创建一个新的Android Application Project并执行以下步骤。 添加本机支持。 在“ Project Explorer >“ Android Tools >“ Add Native Support >“
  • Monkey命令
    Monkey测试 猴子在电脑前乱点。模拟用户触摸屏幕、滑动Trackball、按键等操作来对设备上的程序进行压力测试,检查程序多久的时间会发生异常。 Monkey本质 Android压力测试工具。目的是测试APP是否会crash Monkey简介 - Android系统自带,Java语言编写。系统存放路径为 /system/framework/monkey.jar - monkey.jar是由一个名为“monkey”的Shell脚本来启动执行。脚本系统存放路径为 /system/bin/monkey Monkey命令启动方式 1. PC的CMD窗口执行:adb shell monkey + {命令参数} 2. PC的adb shell进入Android系统,执行:monkey + {命令参数} 3. Android机或模拟器直接运行Monkey命令,即手机上安装终端模拟器(Terminal Emulator for Android) ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210328151653375.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L29faGloZ3k=
  • 从其他应用程序启动Facebook应用程序(launch facebook app from other app)
    问题 如何在Android应用中启动Facebook应用? 回答1 要仅启动默认的启动器活动,请执行以下操作: Intent intent = new Intent("android.intent.category.LAUNCHER"); intent.setClassName("com.facebook.katana", "com.facebook.katana.LoginActivity"); startActivity(intent); 我做了一些研究,因为我想找到答案:)。 我找到了一些方法来轻松地开始不同的活动。 但是我不能保证这会在facebook升级后起作用。 我使用当前的Facebook应用程序对其进行了测试,并且可以正常工作。 至少我使用“ am start .....”通过“ adb shell”对其进行了测试。 基本是: String uri = "facebook://facebook.com/inbox"; Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri)); startActivity(intent); 没有检查facebook.com部分。 您甚至可以输入:具有相同效果的“ facebook://gugus.com/inbox”。 如何在adb中执行此操作。 1. Start
  • ADB命令合集自己记录
    安装配置skd 修改安卓模拟器nox_adb.exe文件 ADB命令 基本命令 查看帮助 adb help 查看版本 adb version 查看adb的连接设备: adb devices 参数 显示列表 无 所有应用 -f 显示应用关联的 apk 文件 -d 只显示 disabled 的应用 -e 只显示 enabled 的应用 -s 只显示系统应用 -3 只显示第三方应用 -i 显示应用的 installer -u 包含已卸载应用 包名包含 字符串 该命令经常出现以下问题: offline —— 表示设备未连接成功或无响应;device —— 设备已连接;no device —— 没有设备/模拟器连接;List of devices attached 设备/模拟器未连接到 adb 或无响应 链接到夜神模拟器 adb connect 127.0.0.1:62001 # 如果连接不到的时候:先找到端口号 adb nodaemon server # 在根据 端口号 找到进程的PID netstat -ano | findstr "5037" # 在进行清除此PID taskkill /pid 22712 -f # 然后调用 重启 或者手动重新联机即可 adb start-server # 或者 adb connect 127.0.0.1:62001 杀进程 adb kill
  • Fire a pinch in/out command to Android phone using adb
    So far I'm able to tap, swipe, unlock, install/uninstall and launch app using adb commands but not able to find how to perform zoom in/out using adb command. I have got coordinates of pinch in/out but not understanding how to fire them using adb command from terminal. For example , we have command --> adb shell input tap x y So in this we can replace x and y with values/coordinates similarly I want to know the way to simulate pinch in/out. Coordinates that I get for pinch-in are below. {"id":0,"dumb":[{"events":[{"id":0,"pressure":1,"x":392.94117647058823,"y":607.4264705882354}],"action":0},{
  • 通过Java运行cmd命令(Run cmd commands through Java)
    问题 我发现了几个用于通过Java类运行cmd命令的代码段,但我无法理解。 这是打开cmd的代码 public void excCommand(String new_dir){ Runtime rt = Runtime.getRuntime(); try { rt.exec(new String[]{"cmd.exe","/c","start"}); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } 而且我找到了用于添加其他命令的其他链接,例如cd http://www.coderanch.com/t/109753/Linux-UNIX/exec-command-cd-command-java 如何使用Java打开命令提示符并插入命令? 谁能帮助我了解如何CD目录,例如: cd C:\Program Files\Flowella 然后在该目录上运行其他命令? 回答1 从Java程序的不同目录运行到工作目录的一种方法是更改​​目录,然后在同一命令行中运行该进程。 您可以通过使cmd.exe运行诸如cd some_directory && some_program类的命令行来完成此cd some_directory && some_program 。