Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can I use adb shell to send commands to my application

I want to find a way to create commands that I can send to my application using the adb shell or similar. This way I can do some small changes to my program without having to reload my application every time I change anything.

Is there a way to open the adb shell and send a command to a running application ?

If that is not possible, then what is a possible way to send commands to my application so I can do things like (Move the UI Elements) or (Create a file from a URL) or a number of other things.... Essentially I want to be able to send string commands to my application.....

If I can do that with the command line tools that would be sweet. otherwise What would be a good way to go about this ?

like image 607
The Lazy Coder Avatar asked Apr 27 '11 23:04

The Lazy Coder


People also ask

Can an app run adb commands?

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().

What can I do with adb shell?

Android Debug Bridge (adb) is a versatile command-line tool that lets you communicate with a device. The adb command facilitates a variety of device actions, such as installing and debugging apps, and it provides access to a Unix shell that you can use to run a variety of commands on a device.


2 Answers

From an adb shell, type am. You can start activities and services as well as send broadcast intents. No arguments will print usage info.

From the host machine you can do adb shell am <command> if you want to invoke this from scripts.

like image 123
adamp Avatar answered Oct 01 '22 17:10

adamp


The adb shell am option is probably the cleanest/best/most awesome option, but you could have a Thread in your app checking for the presence of a specific file on the SD card. Something like (DON'T USE THIS CODE WITHOUT SERIOUS MODIFICATIONS):

public void run()
{
   File f = new File("/sdcard/appcommands");
   while(!stop){
      if(f.exists()){
         // read file
         // obey commands
         // remove file or use some other timing monitor
      }
   }
}

then you can just use adb push to write command files. Truly inefficient, but it can be made to work in a pinch. You can also do more complex things like creating local TCP/HTTP servers and using adb port forwarding, but that may just be overkill.

like image 31
Femi Avatar answered Oct 01 '22 17:10

Femi