Android实战简易教程-第四十三枪(Shell Script 运行Command)
来源:互联网 发布:linux 打开url命令 编辑:程序博客网 时间:2024/06/04 19:57
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;@Overrideprotected 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() {@Overridepublic 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
喜欢的朋友关注我,谢谢!
- Android实战简易教程-第四十三枪(Shell Script 运行Command)
- Android实战简易教程<四十三>(Shell Script 运行Command)
- Android实战简易教程<十三>(五大布局研究)
- Android简易实战教程--第四十三话《上拉加载与下拉刷新》
- Android实战简易教程-第三十三枪(自定义View实现控件晃动提示效果)
- Android实战简易教程-第六十三枪(动画实现唱片播放界面)
- Android实战简易教程-第七十三枪(拨打、接听电话自动免提)
- linux command Line and shell script bible(笔记十三)
- Android实战简易教程-第四十一枪(显示倒计时的Button-适用于获取验证码)
- Android实战简易教程-第四十四枪(Ripple Effect-为控件增加涟漪效果)
- Android实战简易教程-第四十五枪(SlideSwitch-好看又实用的开关按钮)
- Android实战简易教程-第四十六枪(自定义控件体验之罗盘)
- Android实战简易教程-第四十七枪(ListView多选-实现点餐系统)
- Android实战简易教程-第四十八枪(App引导页面效果实现)
- Android实战简易教程-第四十九枪(两种方式实现网络图片异步加载)
- Android简易实战教程--第三十三话《 AsyncTask异步倒计时》
- Android简易实战教程--第五十三话《多彩波浪》
- Android实战简易教程-第五十三枪(通过实现OnScrollListener接口实现上拉加载更多功能)
- 《学习OpenCV》第四章课后题6
- Unix网络编程开篇
- cocos2d-x 通过JNI实现c/c++和Android的java层函数互调
- (2.1.19)注释与注解
- Linux使用wget请求地址时报错
- Android实战简易教程-第四十三枪(Shell Script 运行Command)
- Unity3D开发——安卓版的虚拟按键实现
- Memcached 在windows下的java使用
- 直接插入排序和希尔排序的总结
- 多态性和虚函数
- openfire,XMPP,smack,android下离线消息的接收
- git 运用技巧
- 面向对象方法-------RUP
- Shell脚本——批量安装Apk