Running shell commands on the click of a button - android

I wish to reset an android device programmatically using :
adb shell
recovery --wipe_data
1. What is the easiest way to do this ?
2. Will it require root/superuser access ?

For running shell commands you can use Runtime class. For more information check the documentation
For example:
Runtime.getRuntime().exec("commands_here");

Related

how to enable Multi display feature on Android O

Did anyone tried Android multiple display feature which is new in Android O
Following steps which is not working for me
1.connect hdmi cable to mobile(not sure can use USB as well)
2.make device in root and give following command (expect app is installed)
and not seen that app is launching on secondary(Multiple display feature
) it's just reflecting mobile display as it is because connected hdmi cable
adb shell am start com.Chrome.Canary --display 1
Please suggest any other way or any command to make it work?
Android in general do mirroring when two displays are connected. Right now google has not enabled support for touch and display mapping on the two displays. But if you want to launch any activity on any of the two displays then the command adb shell am start "your_activity" --display display_id will launch your activity on that particular display id. If the above command doesn't launch your activity then you can use adb shell am stack start display_id "your activity". This will also work. But regarding touch, it will be mapped to the primary display (display id 0). As per google you can enable the mapping of touch in EventHub.cpp of Android source code but till now I haven't found it useful. Hope my answer helps you.
Ran using below procedure passed with dell monitor:
ADB shell can be used to call Activity Manager(am) for starting any system actions (or an activity).
The syntax:
adb shell am start [-n (component)] [-a (action)] [-t (mime type)] [-d (data_URL)]
examples:
1. For starting ‘Settings app’:
adb shell am start -n com.android.settings/.Settings
In the above-mentioned command, we set component(-n) to be settings in a specific format.
For starting ‘Gallery app’:
adb shell am start -t image/* -a android.intent.action.VIEW
In the above-mentioned command, we are setting action(-a) for opening the default app for gallery with mime type(-t) to be image/*
In general, for external display the ‘display_id’ starts from ‘1’ in an increasing order, final command to do multi display operation is
adb shell am start -n com.android.settings/.Settings --display 1
The above command will display ‘Settings’ app only in external display with display_id = 1. The primary display will be in the same mode as before executing the command.

Android: Change Device language automatically without device restart

I can change device language using following adb command:
adb shell setprop persist.sys.language fr;setprop persist.sys.country CA;stop;sleep 5;start
This command will restart device after changing device language. But is it possible to change devices language without restart device?
If you're working in a testing context, you could use the fastlane screengrab tool for changing the locale of your device directly.
I myself was looking for a possibility to change the locale of the device from ADB, but failed finding one. In the end I implemented a similar approach tp screengrab. There seems to be no easy way of changing the locale on the phone. Only triggering a configuration change directly through a BroadcastReceiver makes it work. It's called DevSet and after setting everything up a more or less simple call to ADB will change the locale (on all devices I've tested so far):
adb shell am broadcast -n dev.set/.locale --es l {locale}

How to enable Stay awake in the developer options using ADB?

I'm trying to find a way to enable the Developer options -> Stay awake option via ADB.
The solution that I found was to find a way to open developers option menu and then enable stay awake option using setevent command in ADB.
Now I'm trying to find the command that will open the developers options.
I found the command that opens display options:
adb shell am start -n com.android.settings/.DisplaySettings
So I think that a similar command that opens developers options exists.
I will also be glad to hear about another way to enable stay awake option via ADB if there is any.
adb shell am start -n com.android.settings/.DevelopmentSettings
You can enable Stay awake option without having to use UI with the following command:
adb shell settings put global stay_on_while_plugged_in 3
OK, I found a way:
adb shell am start -S com.android.settings/.Settings\$DevelopmentSettingsActivity
I used this : https://github.com/android/platform_packages_apps_settings/blob/master/AndroidManifest.xml
to find the name of the Activity I needed.
Now I have to figure out how to choose the stay awake option by createing a touch event or something...
Set the 'keep awake while plugged in' setting.
adb shell svc power stayon true
Okay I know it is not what MiSo was asking but I think it may be relevant, I have created an app that will enable/dissable this 'Stay awake' setting as you connect/disconnect ADB.
The code is available open-source here: ABD-Stay_Awake
try this:
setprop persist.sys.usb.config mtp,adb

Can I use adb to change the default launcher program?

I set one of the program as default launcher program and default setting program so I cannot change the default programs now, can I change the default programs from android-sdk\android-sdk\platform-tools\adb.exe or remote shell.
How can I do that?
And can I remove the defaults of program in Java code?
For system apps you can't uninstall, use pm disable as in
adb shell pm disable com.android.launcher
you can remove (Uninstall) the default program you set using ADB by doing this :
adb uninstall app.package ..... //for example (com.example.homeapp)
If you don't want to remove the app .. here is a quick hack to do it:
adb shell
am start -a android.intent.action.MAIN
That way you will have a picker with all apps on your devices that listens to Main Action
Choose any home screen app you want . then go to settings and set it as default.
adb shell cmd package set-home-activity "package/activity"
adb reboot
The key is adb. Once you know the package name of the app you wish to clear data for, try:
adb shell pm clear package.name.of.app
It'll clear all data for the app, but I don't know of a way to only clear the defaults.
You can check and change the default sms app by using adb shell settings get secure sms_default_application and then use adb shell settings put secure sms_default_application <package name>. I'm not sure about the other default apps, but it's just a matter of finding it with adb shell settings list <secure/system/global>

Android LogCat and Codegist CRest

I'm using http://crest.codegist.org/ to communicate with server and I want to log responses and requests with android LogCat.
How can I configure CRest (or System) to do this?
Thanks
You can do it in two ways:
execute a command on your connected device:
MBP:~ roman$adb shell setprop log.tag.CodeGist VERBOSE
set the same property programmatically using the System.setProperty() method

Categories

Resources