首页 > 系统相关 >Android实战简易教程-第四十三枪(Shell Script 运行Command)

Android实战简易教程-第四十三枪(Shell Script 运行Command)

时间:2022-11-11 11:37:50浏览次数:60  
标签:Shell layout Script am Command import android com id


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看一下结果如下:

Android实战简易教程-第四十三枪(Shell Script 运行Command)_Android

再次运行一下:

 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

 喜欢的朋友关注我,谢谢!




标签:Shell,layout,Script,am,Command,import,android,com,id
From: https://blog.51cto.com/u_15866446/5843746

相关文章

  • 安装 TypeScript 并编译成JS
    官网:https://github.com/microsoft/TypeScriptTypeScript是一种由微软开发的开源、跨平台的编程语言。它是JavaScript的超集,最终会被编译为JavaScript代码。TypeScript......
  • python和shell产生随机密码,哪个更方便
    一、Python#@File:生成随机密码.py#@desc:importstringimportrandom####侯选all_words=list(string.ascii_lowercase+string.ascii_uppercase+string.digits......
  • 给windows服务加描述 sc description xxxxxxxxxx "需要修改成的描述"
    给windows服务加描述scdescriptionxxxxxxxxxx"需要修改成的描述"给windows服务加描述修改windows服务的描述scdescription xxxxxxxxxx "需......
  • OJ中Typescript语法整理
    基础原始类型原始类型:number/string/boolean/null/undefined/symbol对象类型:oject(数组,对象,函数等)自定义复杂的对象类型:typeCustomArray=(number|string)let......
  • [Typescript] 95. Hard - Required Keys
    Implementtheadvancedutiltype RequiredKeys<T>,whichpicksalltherequiredkeysintoaunion.ForexampletypeResult=RequiredKeys<{foo:number;bar?:......
  • [Typescript] 96. Hard - Optional Keys
    Implementtheadvancedutiltype OptionalKeys<T>,whichpicksalltheoptionalkeysintoaunion. /*_____________YourCodeHere_____________*/typeOp......
  • 获取shell脚本所在目录
    前几天写的​​七牛​​​的参赛demo,用bash写了一个便捷安装的脚本,涉及到了路径相关的判断,从​​stackoverflow​​,加上自己的实践整理一下。简单版下面是一个最简单的实现,......
  • Android中Java和JavaScript交互
    Android提供了一个很强大的WebView控件用来处理Web网页,而在网页中,JavaScript又是一个很举足轻重的脚本。本文将介绍如何实现Java代码和Javascript代码的相互调用。如何实现......
  • typescript 泛型
    一、泛型与any类型的区别泛型是等待确定的占位类型,可以理解为函数的形参;所以泛型是固定的某一个类型,实例化的时候确定其实际类型any类型是顶级类型,它包括了所有的基......
  • [Typescript] 93. Hard - Get Required
    Implementtheadvancedutiltype GetRequired<T>,whichremainsalltherequiredfieldsForexampletypeI=GetRequired<{foo:number,bar?:string}>//exp......