Android实战简易教程<四十三>(Shell Script 运行Command)

来源:互联网 发布:禁毒网络知识答题 编辑:程序博客网 时间:2024/06/04 17:59

android系统运行于Dalvik VM中,有着与Linux雷士的Shell Command指令,可通过Runtime().getRuntime().exec()来运行指令。

下面我们就通过代码来实现这一功能,体验一下命令行。

1.activity_main.xml:

[html] view plaincopy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.   
  6.     <EditText  
  7.         android:id="@+id/et_input"  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="100dp"  
  10.         android:layout_margin="4dp"  
  11.         android:hint="Input Command Lines" />  
  12.   
  13.     <Button  
  14.         android:id="@+id/btn_run"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:layout_below="@+id/et_input"  
  18.         android:text="Run" />  
  19.   
  20.     <TextView  
  21.         android:id="@+id/tv_result"  
  22.         android:layout_width="match_parent"  
  23.         android:layout_height="match_parent"   
  24.         android:layout_below="@+id/btn_run"  
  25.         />  
  26.   
  27. </RelativeLayout>  
2.MainActivity.java:

[java] view plaincopy
  1. package com.example.runcommand;  
  2.   
  3. import java.io.DataInputStream;  
  4. import java.io.IOException;  
  5.   
  6. import android.app.Activity;  
  7. import android.os.Bundle;  
  8. import android.text.TextUtils;  
  9. import android.view.View;  
  10. import android.view.View.OnClickListener;  
  11. import android.widget.Button;  
  12. import android.widget.EditText;  
  13. import android.widget.TextView;  
  14.   
  15. public class MainActivity extends Activity {  
  16.     private EditText mInputET;  
  17.     private TextView mResult;  
  18.     private Button mRun;  
  19.     private String input;  
  20.   
  21.     @Override  
  22.     protected void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.activity_main);  
  25.         initViews();  
  26.     }  
  27.   
  28.     private void initViews() {  
  29.         mInputET = (EditText) findViewById(R.id.et_input);  
  30.         mResult = (TextView) findViewById(R.id.tv_result);  
  31.         mRun = (Button) findViewById(R.id.btn_run);  
  32.         mRun.setOnClickListener(new OnClickListener() {  
  33.   
  34.             @Override  
  35.             public void onClick(View v) {  
  36.                 input = mInputET.getText().toString();  
  37.                 if (!TextUtils.isEmpty(input)) {  
  38.                     runRootCommand(input);  
  39.                     mInputET.setText("");  
  40.                       
  41.   
  42.                 }  
  43.             }  
  44.   
  45.         });  
  46.   
  47.     }  
  48.   
  49.     protected void runRootCommand(String command) {  
  50.         Process process = null;  
  51.   
  52.         try {  
  53.             process = Runtime.getRuntime().exec(command);  
  54.   
  55.             StringBuffer output = new StringBuffer();  
  56.   
  57.             DataInputStream stdout = new DataInputStream(process.getInputStream());  
  58.             String line;  
  59.   
  60.             while ((line = stdout.readLine()) != null) {  
  61.                 output.append(line).append("\n");  
  62.   
  63.             }  
  64.   
  65.             process.waitFor();  
  66.   
  67.             mResult.setText(output.toString());  
  68.         } catch (IOException e) {  
  69.             mResult.setText("权限不足或系统出错!");  
  70.             e.printStackTrace();  
  71.         } catch (InterruptedException e) {  
  72.             e.printStackTrace();  
  73.         } finally {  
  74.             process.destroy();  
  75.         }  
  76.     }  
  77.   
  78. }  
首先运行一下程序,输入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

0 0
原创粉丝点击