Can we open a browser with passing URL in golang android OS? - android

Working on a Go project and using gomobile to generate .apk file.
I am trying to open a browser in the go code with a passing URL.
I know go supports running CMD commands in different operating systems such as windows and Linux.
Wonder to know if there is any code to support Android OS as well.
In other words in following code what should I have under
case "android":
func openbrowser(url string) {
var err error
switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
default:
err = fmt.Errorf("unsupported platform")
}
if err != nil {
log.Fatal(err)
}
}

You can try using ADB Shell. Check this question Need command line to start web browser using adb
adb shell am start -a android.intent.action.VIEW -d 'http://stackoverflow.com/?uid=isme\&debug=true'

Related

Android 10 - netstat execution not working properly

I'm trying to run a shell command in my Android app in order to check if a give tcp port is opened or not:
Runtime.getRuntime().exec(arrayOf("/system/bin/sh", "-c", "netstat -tulpn | grep :8080"))
Running this command in Android version < 10, works fine. It will do a full scan and return the entry if there is the given port used.
But in Android 10, it is not working anymore. The process exec will return:
Process[pid=9534 ,hasExited=true, exitcode=1]
When reading the inputStream of the process it is always null, while executing netstat -tulpn | grep :8080 from terminal will return an entry, as expected.
Running
Runtime.getRuntime().exec(arrayOf("/system/bin/sh", "-c", "netstat -tulpn"))
alone, and parsing the output, will only show:
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program Name
and no other data at all.
I only have this problem in Android 10, because for older versions it is working.
Has anything changed in Android 10 regarding to shell commands? It is necessary for me to run this command.
Any help would be appreciated...
This is most likely because of privacy protection improved in Android 10.
Look here: https://developer.android.com/about/versions/10/privacy/changes#proc-net-filesystem
My goal was to check if a given tcp port was in use or not. I made this method that did the job that I was looking for:
private fun isPortUsed(port: Int): Boolean {
return try {
// Try to open socket at the given port
val socket = ServerSocket(port)
// Release port
socket.close()
false
} catch (ex: BindException) {
// Port is already in use
true
} catch (ex: Exception) {
// Other reason for failing
false
}
}

Perform a search on google chrome using ADB

I'm setting up a python script for doing some tests on android phones.
I can actually launch a new chrome tab on my phone with ADB:
shell am start -n com.android.chrome/org.chromium.chrome.browser.ChromeTabbedActivity -d "about:newtab" --activity-clear-task
I can't figure out the way I could perform a search on chrome with the search provider included in.
The suggested command by #oss does not care about user settings/defaults , it only require that google chrome is installed!
You may need to create a function like this:
function adbgs () {
local str="${*}";
adb shell am start -n com.android.chrome/org.chromium.chrome.browser.ChromeTabbedActivity -d "google.com/search?q=${str// /+}" --activity-clear-task
}
adbgs how to do stuff;
Or if you want search using default browser :
function adburlopen () {
adb shell am start -a android.intent.action.VIEW -d "${1}"
}
# google
function adbgs () {
local str="${*}";
adburlopen "https://www.google.com/search?q=${str// /+}";
}
# yahoo
function adbys () {
local str="${*}";
adburlopen "https://sg.search.yahoo.com/search?q=${str// /+}";
}

Android app crashes when adding commands to RootTools v4.2 shell

I have an Android application utilizing RootTools v4.2 (the latest I know of) and I have followed their documentation on how to execute shell commands as root. Sometimes the commands execute just fine, other times the app crashes with the following exception.
java.lang.IllegalStateException: Unable to add commands to a closed shell
Here is the actual code the exception is being throw on:
RootTools.getShell(true).add(cmd);
So I'm wondering since the docs make no mention of this sort of problem if there is something else I'm doing wrong? Looking through the docs I see nothing on how to ensure I get an open shell before I start adding commands.
This code is working with me . Try to install the Library again may be its not vaild .
if(RootTools.isAccessGiven()){
try {
Shell shell = RootTools.getShell(true);
JavaCommand cmd = new JavaCommand(0,this,"input keyevent 26");
shell.add(cmd);
}
catch (Exception e){
Log.d("ERRORS : ",e.getMessage());
}
}

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?
You can do it with this:
Process process = Runtime.getRuntime().exec("your command");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
Don't forget to surround it with a try and catch statement.
Edit:
#Phix is right, ProcessBuilder would be better to use.
Normal Android apps have different privileges to processes started via adb, e.g., processes started via adb are allowed to the capture the screen whereas normal apps aren't. So, you can execute commands from your app via Runtime.getRuntime().exec(), but they won't have the same privileges as if you had executed from an adb shell.
i came across this post looking for a different query, but i have worked specifically with input on android before, so I'd just like to put some clarity on the matter.
The reason why
Runtime.getRuntime().exec("adb shell input keyevent 120");
Is not working, is because you are not removing
adb shell
The ADB part is only for use on your computer, if you have incorrectly installed ADB, the command would actually be a path to the adb.exe file on your computer, like this
C:\XXXX\ADB Files\adb.exe shell
or
C:\XXXX\ADB Files\adb shell
The shell part tells the ADB program on your computer to access the devices shell, so your device will not know what shell is either...
Using sh /path/to/commandList.sh will execute the commads listed in commandList.sh as it is a shell script (a .batch file on windows is similar )
The command you want to use is
Runtime.getRuntime().exec("input keyevent 120");
However this will cause Environment null and working directory null, you can bypass this by writing the commands to a shell script ( .sh file ) and then running the script with
Runtime.getRuntime().exec("sh path/to/shellScript.sh");
Sometimes the sh is not needed, but i use it just incase.
I hope this clears at least something up :)
adb shell invoked in Runtime.getRuntime().exec is not running under shell user. It provide shell but with same process owner user (like u0_a44). That's the reason all command did not work.
This is what I do in Kotlin, I also get command responses too
fun runShellCommand(command: String) {
// Run the command
val process = Runtime.getRuntime().exec(command)
val bufferedReader = BufferedReader(
InputStreamReader(process.inputStream)
)
// Grab the results
val log = StringBuilder()
var line: String?
line = bufferedReader.readLine()
while (line != null) {
log.append(line + "\n")
line = bufferedReader.readLine()
}
val Reader = BufferedReader(
InputStreamReader(process.errorStream)
)
// if we had an error during ex we get here
val error_log = StringBuilder()
var error_line: String?
error_line = Reader.readLine()
while (error_line != null) {
error_log.append(error_line + "\n")
error_line = Reader.readLine()
}
if (error_log.toString() != "")
Log.info("ADB_COMMAND", "command : $command $log error $error_log")
else
Log.info("ADB_COMMAND", "command : $command $log")
}
Executing
Runtime.getRuntime().exec("adb shell input keyevent 120");
I got the following error:
java.io.IOException: Cannot run program "adb": error=13, Permission denied.
Executing
Runtime.getRuntime().exec("adb shell input keyevent 120");
There is no error but at the same time, my request is not processed to take the screenshot.
I found out this was working in earlier versions of android but later it was removed. Though I'm not able to provide the source here why it is not working.
Hope this helps someone like me who is trying to use this approach to take the screenshot when the app is not in the foreground.
Process process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
string cmd = "/system/bin/input keyevent 23\n";
os.writeBytes(cmd);
the phone must be rooted. here I have executed adb command "input keyevent 23".
remember when you execute adb command through su you does not need to add "adb shell input keyevent 23"

How can I run Linux commands on an Android device?

On some Android devices, in the ADB shell, I can only run echo, cd, ls. When I run:
tar -cvf //mnt/sdcard/BackUp1669/apk/test.tar /mnt/sdcard/test.apk
Or the command cp, it returns:
sh: tar: not found
Why can I not run these commands? Some devices support these commands. My end goal is to copy a file from the /data/data folder to SD card. I got su and I got the following code:
int timeout = 1000;
String command = "tar -cvf /" + Environment.getExternalStorageDirectory() + "/cp/"
+ packageName + ".tar" + " " + path;
DataOutputStream os = new DataOutputStream(process.getOutputStream());
BufferedReader is = new BufferedReader(new InputStreamReader(new DataInputStream(
process.getInputStream())), 64);
String inLine;
try {
StringBuilder sbCommand = new StringBuilder();
sbCommand.append(command).append(" ");
sbCommand.append("\n");
os.writeBytes(command.toString());
if (is != null) {
for (int i = 0; i < timeout; i++) {
if (is.ready())
break;
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (is.ready()) {
inLine = is.readLine();
} else {
}
}
} catch (IOException e) {
e.printStackTrace();
}
It always stops in is.ready(), and when I changed it to process.waitfor() it also stopped. Why?
As far as i know, the only way to run shell commands is:
Process proc = Runtime.getRuntime().exec("your command");
You can run Linux commands on Android. But there are usually just very few pre-installed.
If you want to add more commands you might want to root your device and install busybox on it.
This is not for productive use within an application but can help you to work with your device.
If you have the binaries for your system, you can run anything on your system.
Saying that you have to understand that you have to find the binaries for tar.
Look here http://forum.xda-developers.com/showthread.php?t=872438
And possibly other places..
You can probably get this done by using a Terminal Emulator app. As you wrote above, I don't know how well DOS commands will work. But, a Terminal Emulator works without root.
You can install Termux app on your android device and run Linux command by using that app
Install busybox, then type the command in the following format:
busybox [linux command]
You cannot use all the linux commands without busybox, because Android doesn't have all the binaries that are available in a standard linux operating system.
FYI, a binary is just a file that contains compiled code. A lot of the default binaries are stored in /system/bin/sh directory. All these commands like 'cp' 'ls' 'get' etc, are actually binaries. You can view them through:
ls -a /system/bin/sh
Hope this helps.
In reply to Igor Ganapolsky, You would have to have a database set up for locate.
Probably find would be adequate for your needs.
example:
find -name *.apk

Categories

Resources