android系统运行于Dalvik VM中,有着与Linux雷士的Shell Command指令,可通过Runtime().getRuntime().exec()来运行指令。
下面我们就通过代码来实现这一功能,体验一下命令行。
1.activity_main.xml:
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
-
- <EditText
- android:id="@+id/et_input"
- android:layout_width="match_parent"
- android:layout_height="100dp"
- android:layout_margin="4dp"
- android:hint="Input Command Lines" />
-
- <Button
- android:id="@+id/btn_run"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@+id/et_input"
- android:text="Run" />
-
- <TextView
- android:id="@+id/tv_result"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_below="@+id/btn_run"
- />
-
- </RelativeLayout>
2.MainActivity.java:
- package com.example.runcommand;
-
- import java.io.DataInputStream;
- import java.io.IOException;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.text.TextUtils;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
-
- public class MainActivity extends Activity {
- private EditText mInputET;
- private TextView mResult;
- private Button mRun;
- private String input;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- initViews();
- }
-
- private void initViews() {
- mInputET = (EditText) findViewById(R.id.et_input);
- mResult = (TextView) findViewById(R.id.tv_result);
- mRun = (Button) findViewById(R.id.btn_run);
- mRun.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View v) {
- input = mInputET.getText().toString();
- if (!TextUtils.isEmpty(input)) {
- runRootCommand(input);
- mInputET.setText("");
-
-
- }
- }
-
- });
-
- }
-
- protected void runRootCommand(String command) {
- Process process = null;
-
- try {
- process = Runtime.getRuntime().exec(command);
-
- StringBuffer output = new StringBuffer();
-
- DataInputStream stdout = new DataInputStream(process.getInputStream());
- String line;
-
- while ((line = stdout.readLine()) != null) {
- output.append(line).append("\n");
-
- }
-
- process.waitFor();
-
- mResult.setText(output.toString());
- } catch (IOException e) {
- mResult.setText("权限不足或系统出错!");
- e.printStackTrace();
- } catch (InterruptedException e) {
- e.printStackTrace();
- } finally {
- process.destroy();
- }
- }
-
- }
首先运行一下程序,输入ls -l看一下结果如下:
再次运行一下:
am start -a android.intent.action.CALL -d tel:10086 可以跳转到拨号界面。
下面我们介绍一下常见的am 命令
打开一个网页: am start -a android.intent.action.VIEW -d http://www.baidu.com (这里-d表示传入的data)
3. 打开音乐播放器:am start -a android.intent.action.MUSIC_PLAYER 或者
am start -n com.android.music/om.android.music.MusicBrowserActivity
4. 启动一个服务: am startservice <服务名称>
例如:am startservice -n com.android.music/com.android.music.MediaPlaybackService (这里-n表示组件)
或者 am startservice -a com.smz.myservice (这里-a表示动作,就是你在Androidmanifest里定义的)
5. 发送一个广播: am broadcast -a <广播动作>
例如: am broadcast -a com.smz.mybroadcast