Android uses system to call the Am command to execute the action

Source: Internet
Author: User

The following statement is displayed in the underlying code:

System ("/system/bin/sh-c \" am broadcast-a android. intent. action. AT_AIRPLANE_MODE -- ez state true \"");

What does system do ??

After google's original system is a system call, it executes a system command.

Here, the meaning of the system parameter is added:

Call system and run the executable program or script am through shell,

Send broadcast Action: android. intent. action. AT_AIRPLANE_MODE broadcast parameter -- ez key value to state true ......

This is also acceptable. I thought that the android underlying layer should take the initiative to interact with the upper layer, so I had to use socket and so on. It was also possible, so I really learned.

1 system command

System () executes a command specified in command by calling/bin/sh-c command, and returns after the command has been completed.

During execution of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT will be ignored.

The system () function calls/bin/sh to execute the command specified by the parameter./bin/sh is generally a soft connection pointing to a specific shell, such as bash, -The c option tells shell to read the command from the string command;

During the command execution, SIGCHLD is blocked, for example: hi, kernel, this will not send a SIGCHLD signal to me, wait for me to finish;

During the command execution, SIGINT and SIGQUIT are ignored, meaning that the process does not take any action after receiving these two signals.

To better understand the returned values of the system () function, you need to understand the execution process. In fact, the system () function performs three steps:

A. fork a sub-process;

B. Call the exec function in the sub-process to execute the command;

C. Call wait in the parent process to wait for the child process to end.

If fork fails, the system () function returns-1.

If exec is successfully executed, that is, the command is successfully executed, the value returned by the command through exit or return is returned.

Note: Successful command execution does not mean that the execution is successful, for example, command: "rm debuglog.txt". The command is successfully executed no matter the file does not exist.

If exec fails, that is, the command is not successfully executed. For example, if a signal is interrupted or the command does not exist, the system () function returns 127.

If the command is NULL, the system () function returns a non-0 value, generally 1.

For details, refer to this article:

Http://my.oschina.net/renhc/blog/53580

2 AM

The am code is implemented under the \ frameworks \ base \ cmds \ am \ directory:

\ Frameworks \ base \ cmds \ am:

Base =/system

Export CLASSPATH = $ base/framework/am. jar

Exec app_process $ base/bin com. android. commands. am. Am "$ @"

Use app_process to create and execute a process like com. android. commands. am. Am $ @ to pass all parameters to Am.

\ Frameworks \ base \ cmds \ am \ src

\ Frameworks \ base \ cmds \ am \ Android. mk

Compile the java layer Am as an am. jar package. Let's take a look at the functions supported by Am.

Public static void main (String [] args) {try {(new Am ()). run (args);} catch (IllegalArgumentException e) {showUsage () ;}} private void run (String [] args) throws Exception {...... // Obtain ActivityManagerService instance mAm = ActivityManagerNative. getDefault (); mArgs = args; String op = args [0]; mNextArg = 1; // if (op. equals ("start") {runStart ();} else if (op. equals ("startservice") {runStartService ();} else if (op. equals ("force-stop") {runForceStop ();} else if (op. equals ("kill") {runKill ();} else if (op. equals ("kill-all") {runKillAll ();} else if (op. equals ("instrument") {runInstrument ();} else if (op. equals ("broadcast ")){SendBroadcast();}...... // Many commands}

Let's take a look at the broadcast sending command:

Private void sendBroadcast () throws Exception {// parse the Intent intent = makeIntent (UserHandle. USER_ALL); IntentReceiver extends ER = new IntentReceiver (); // send broadcast mAm through AMS. broadcastIntent (null, intent, null, receiver, 0, null, true, false, mUserId); receiver. waitForFinish ();}

Therefore, Android also has such a tool am. By looking at showUsage, you can do a lot of things;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.