I am working on a battery monitoring app in which I have to show battery usage for all apps. After a lot of googling I found there a only way to do so is using command line
$ adb shell dumpsys batterystats --charged --checkin
The above command will provide all battery stats but I don't know how can I get this data in my code?
Please help!
Following code to show Summary of all apps battery usage, So you can try this.
Intent sample = new Intent("android.intent.action.POWER_USAGE_SUMMARY");
startActivity(sample);
You can grab dumpsys information on the device, but it does require permissions which are not granted to regular applications.
The shell-user has permission to run the command however - if you're running an on-device test using Instrumentation you can use Instrumentation.getUiAutomation().executeShellCommand(...) to execute the command as the shell user.
Related
In my app, I am using Timber as a logger. I am accessing the logs from the terminal via adb using this isntruction:
./adb logcat com.company.my_app:D
I do get the logs but the issue is that I am getting a crazy amount of noise from the OS (ie SurfaceFlinger , GraphicBuffer, vndksupport) which make the logs harder to read.
Is there a way to filter the logs like in Android Studio and just get logs from my app. Thank you !
P.S. I have trie a few answers from here but none of them remove the noise.
What works best for me is to grep for the process ID which is displayed in every log line. In the example below 7098 is printed with every log line.
08-10 18:48:39.825 7098 7144 D NetworkModule: --> END POST
So this is the adb instruction used to get filtered logs:
adb logcat | grep -F "7098"
Note: the process ID is not static and it is going to change if app is hard-closed or device is restarted.
I don't know if it is the best solution, but it works in my case.
I am trying to make a form where people can toggle notifications on and off. I figured the easiest way to do that would be set the permissions to allow or deny but I can't figure out how to remove the permission once it has been granted.
I am triggering the initial permission request by doing the following in android and not too sure what I will do with iOS as it requests the permissions somewhat automatically.
string rec = "android.c2dm.intent.RECEIVE";
string reg = "android.c2dm.intent.REGISTRATION";
ActivityCompat.RequestPermissions(Plugin.CurrentActivity.CrossCurrentActivity.Current.Activity, new string[] { rec, reg },0);
I essentially would like a page that has a few Switch controls to enable/disable permissions.
any help would be greatly appreciated.
Thanks,
There's no built in method for you to toggle a permission (ie, allow, then disallow), but you could do it if your app can run an adb command.
To undo a permission:
adb shell pm revoke <package_name> <permission_name>
https://stackoverflow.com/a/32683390/6668797
To run an adb command within your app, a couple of variations, but all involve:
Process process = Runtime.getRuntime().exec("your command")
how to run adb shell commands from android code?
Is it possible to execute adb commands through my android app?
I am trying to record my device screen using the shell command as outlined here:
http://developer.android.com/tools/help/adb.html#screenrecord
Using the verbose flag, I am getting a Permission Denial that I think is connected to the fact there is an Owner profile on the device, as it references User 0 and User 1 in the error message.
Does anyone know how I can remove the Owner profile (can't see any options to do so) or how I can set the Owner to user 0?
EDIT - clarification, the error states "broadcast asks to run as user -1 but is calling from user 0"
I got the recording to work.
It seems that for whatever reason, you need to do this via adb shell.
Whilst adb shell is running shell commands on the connected device, running the screenrecord commands directly on the device just doesn't want to work.
What I'm doing:
I've built GNU emacs for native use on an phone.
I run emacs in daemon mode on the phone, so I connect to it anytime with emacsclient, to continue working with regular files, run processes, etc.
When logging in from the terminal on the phone, I'm currently user 10157, everything works:
$ id
uid=10157(10157) gid=10157(10157)
groups=10157(10157),1015(1015),1023(1023),1028(1028),3003(3003)
When I connect via ssh to the phone from a PC (I use DigiSSHd on the phone), it logs me in as a regular user 10282, everything works:
$ id
uid=10282 gid=10282 groups=1015(1015),1023(1023),1028(1028),3003(3003)
Emacs runs fine etc. However, this way I can't connect via emacsclient to the emacs process running under user 10157. This is desirable, since I don't want to start two emacs processes, since I want to continue working with files that I have open in emacs under user 10157.
Therefore:
$ su - 10157
Fine, I can run emacs etc. However, I cannot access the web.
$ ping -c1 google.com
You must have internet permissions to use ping. Aborting.
$ id
uid=10157(10157) gid=10157(10157) groups=10157(10157)
Thus I'm no longer in group 3003, necessary for internet access, besides other groups also.
Why does this group info get stripped, and how can I remedy this, so I can continue accessing the web when su as this user under ssh?
When i run the command:
busybox --list
I don't see su in the list.
su --help
shows Superuser.apk in the help text. It means su is provided by Superuser app.
I followed the steps described by you and i could su as another user and still have internet permission as shown below.
I have the following apps installed.
BusyBox v1.18.5-Stericson
Superuser v3.0.7
Terminal Emulator v1.0.45
SSHDroid v1.9.6
Suggestion:
I think the issue is with su on your device. You may try this one.
https://play.google.com/store/apps/details?id=com.noshufou.android.su
If i just use adb shell without running SSHDroid still i can su as another user with internet permission.
Note: The BusyBox id command doesn't show groups information always.
According to the standard man page for su (from a linux box)
When - is used, it must be specified as the last su option. The other forms (-l and --login) do not have this restriction.
Based on that, try
$ su 10157 -
I'm probably missing something here because this seems way too obvious but why not just 'sudo -u 10157' your emacs program?
you'd still have access to the net and your emacs would be working. or did I miss something important?
Permissions are not environment variables that can be inherited via su -.
Moreover, gid are are hard coded and their associations with each APP uid cannot be changed after installation.
10157 should be the uid of the DigiSSHd application, thus you could try to rebuild it after changing the AndroidManifest.xml to require the proper permission.
You can find something useful here and here.
The same should work for BusyBox (see here).
However, you could open some security hole by enabling NETWORK access through such applications.
In adb shell, do we have any command to get device info, like lspci?
Thank you!
This should probably be on android.stackexchange.com, but anyhow. even if you are not rooted, you can access use getprop and it will return information about the device.
I'm afraid access to the linux layer is only possible by "rooting" the device
You can get some (messy) messages from dmesg:
$ adb shell dmesg
Note that the kernel ring buffer is limited. The first few lines may be dropped as time goes by.
dmesg requires root, too.