導航:首頁 > 程序命令 > 安卓shell命令

安卓shell命令

發布時間:2022-04-28 09:28:19

android執行shell命令的代碼該怎麼寫

Android中執行adb shell命令的方式如下:

041424344

/** * 執行一個shell命令,並返回字元串值 * * @param cmd * 命令名稱&參數組成的數組(例如:{"/system/bin/cat", "/proc/version"}) * @param workdirectory * 命令執行路徑(例如:"system/bin/") * @return 執行結果組成的字元串 * @throws IOException */ public static synchronized String run(String[] cmd, String workdirectory) throws IOException { StringBuffer result = new StringBuffer(); try { // 創建操作系統進程(也可以由Runtime.exec()啟動) // Runtime runtime = Runtime.getRuntime(); // Process proc = runtime.exec(cmd); // InputStream inputstream = proc.getInputStream(); ProcessBuilder builder = new ProcessBuilder(cmd); InputStream in = null; // 設置一個路徑(絕對路徑了就不一定需要) if (workdirectory != null) { // 設置工作目錄(同上) builder.directory(new File(workdirectory)); // 合並標准錯誤和標准輸出 builder.redirectErrorStream(true); // 啟動一個新進程 Process process = builder.start(); // 讀取進程標准輸出流 in = process.getInputStream(); byte[] re = new byte[1024]; while (in.read(re) != -1) { result = result.append(new String(re)); } } // 關閉輸入流 if (in != null) { in.close(); } } catch (Exception ex) { ex.printStackTrace(); } return result.toString(); }

android系統底層採用的是linux,所以adb這樣的linux指令是可以在java代碼中調用的,可以使用ProcessBuilder 這個方法來執行對應的指令。還可以通過如下方式執行:

12345678910111213141516
Process p = Runtime.getRuntime().exec("ls"); String data = null; BufferedReader ie = new BufferedReader(new InputStreamReader(p.getErrorStream())); BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); String error = null; while ((error = ie.readLine()) != null && !error.equals("null")) { data += error + "\n"; } String line = null; while ((line = in.readLine()) != null && !line.equals("null")) { data += line + "\n"; } Log.v("ls", data);

㈡ 如何在Android Shell命令行中斷運行中的程序

android程序執行adb shell命令(實例源碼
package net.gimite.nativeexe;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import net.gimite.nativeexe.R;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;

public class MainActivity extends Activity {

private TextView outputView;
private Button localRunButton;
private EditText localPathEdit;
private Handler handler = new Handler();
private EditText urlEdit;
private Button remoteRunButton;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

outputView = (TextView)findViewById(R.id.outputView);
localPathEdit = (EditText)findViewById(R.id.localPathEdit);
localRunButton = (Button)findViewById(R.id.localRunButton);
localRunButton.setOnClickListener(onLocalRunButtonClick);

}

private OnClickListener onLocalRunButtonClick = new OnClickListener() {
public void onClick(View v) {
String output = exec(localPathEdit.getText().toString());
output(output);
// try {
//
// // Process process = Runtime.getRuntime().exec(localPathEdit.getText().toString());
//
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
}
};

// Executes UNIX command.
private String exec(String command) {
try {
Process process = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
int read;
char[] buffer = new char[4096];
StringBuffer output = new StringBuffer();
while ((read = reader.read(buffer)) > 0) {
output.append(buffer, 0, read);
}
reader.close();
process.waitFor();
return output.toString();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}

private void download(String urlStr, String localPath) {
try {
URL url = new URL(urlStr);
HttpURLConnection urlconn = (HttpURLConnection)url.openConnection();
urlconn.setRequestMethod("GET");
urlconn.setInstanceFollowRedirects(true);
urlconn.connect();
InputStream in = urlconn.getInputStream();
FileOutputStream out = new FileOutputStream(localPath);
int read;
byte[] buffer = new byte[4096];
while ((read = in.read(buffer)) > 0) {
out.write(buffer, 0, read);
}
out.close();
in.close();
urlconn.disconnect();
} catch (MalformedURLException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private void output(final String str) {
Runnable proc = new Runnable() {
public void run() {
outputView.setText(str);
}
};
handler.post(proc);
}

}

㈢ 如何在android裡面執行adb shell命令

ADB介面的作用主要是讓電腦等其它設備控制安卓系統的,所以,稱為「中間橋」;
不是為安卓自已用的,自已可直接執行稱為SHELL,這與ADB無關。
所以安卓JAVA不一定有封裝的ADB類。電腦上有ADB服務程序,埠5037,
它是中間程序,與安卓系統上守護進程(Daemon)通訊。
如果要在自已的手機上應該也能執行adb命令,應該直接跟守護進程
(Daemon)通訊了。網路上可以搜到的方法並不滿意。

樓主用exec執行CMD命令,這已不是ADB介面了,這是系統的SHELL了!!!

自已用socket/tcp直接發命令效果不知怎樣,地址用127.0.0.1, 安卓daemon進程的埠
5555 是奇數開始。

㈣ 如何編寫安卓程序執行shell腳本

android系統執行shell腳本,需要首先確認用戶具有修改shell的許可權,使用 process來執行指令,如下代碼:public void execShell(String cmd){ try{ //許可權設置 Process p = Runtime.getRuntime().exec("su"); //開始執行shell腳本 //獲取輸出流 O...

㈤ 如何讓Android系統或Android應用執行shell腳本

android系統執行shell腳本,需要首先確認用戶具有修改shell的許可權,使用 process來執行指令,如下代碼:
public void execShell(String cmd){
try{
//許可權設置
Process p = Runtime.getRuntime().exec("su"); //開始執行shell腳本
//獲取輸出流
OutputStream outputStream = p.getOutputStream();
DataOutputStream dataOutputStream=new DataOutputStream(outputStream);
//將命令寫入
dataOutputStream.writeBytes(cmd);
//提交命令
dataOutputStream.flush();
//關閉流操作
dataOutputStream.close();
outputStream.close();
}
catch(Throwable t)
{
t.printStackTrace();
}
}

㈥ android apk 怎麼執行adb shell命令

android中執行shell命令有兩種方式: 1.直接在代碼中用java提供的Runtime 這個類來執行命令,以下為完整示例代碼。 public void execCommand(String command) throws IOException { // start the ls command running //String[] args = new String[]{"sh", "-c", command}; Runtime runtime = Runtime.getRuntime(); Process proc = runtime.exec(command); //這句話就是shell與高級語言間的調用 //如果有參數的話可以用另外一個被重載的exec方法 //實際上這樣執行時啟動了一個子進程,它沒有父進程的控制台 //也就看不到輸出,所以需要用輸出流來得到shell執行後的輸出 InputStream inputstream = proc.getInputStream(); InputStreamReader inputstreamreader = new InputStreamReader(inputstream); BufferedReader bufferedreader = new BufferedReader(inputstreamreader); // read the ls output String line = ""; StringBuilder sb = new StringBuilder(line); while ((line = bufferedreader.readLine()) != null) { //System.out.println(line); sb.append(line); sb.append('\n'); } //tv.setText(sb.toString()); //使用exec執行不會等執行成功以後才返回,它會立即返回 //所以在某些情況下是很要命的(比如復制文件的時候) //使用wairFor()可以等待命令執行完成以後才返回 try { if (proc.waitFor() != 0) { System.err.println("exit value = " + proc.exitValue()); } } catch (InterruptedException e) { System.err.println(e); } } } 2.直接安裝shell模擬器,即已經開發好的android應用,啟動後類似windows的dos命令行,可以直接安裝使用,可執行常用的linux命令,應用在附件。 shell.apk大小:455.51K所需財富值:5 已經過網路安全檢測,放心下載 點擊下載下載量:1

㈦ 怎麼讓Android系統或Android應用執行shell腳本

一、Android應用啟動服務執行腳本
1 如何寫服務和腳本
在android源碼根目錄下有/device/tegatech/tegav2/init.rc文件相信大家對這個文件都不陌生(如果不明白就仔細研讀下android啟動流程)。如果在該腳本文件中添加諸如以下服務:
service usblp_test /data/setip/init.usblpmod.sh
oneshot
disabled
註解:每個設備下都會有自己對應的init.rc,init.設備名.rc腳本文件。oneshot disabled向我們說明了在系統啟動的時候這個服務是不會自動啟動的。並且該服務的目的是執行/data/setip/init.usblpmod.sh腳本。腳本的內容你可以隨便寫,只要符合shell語法就可以了,比如腳本可以是簡單的設置eth0:
# ! /system/bin/sh //腳本的開頭必須這樣寫。
Ifconfig eth0 172.16.100.206 netmask 255.255.0.0 up//設置ip的命令
2、如何在應用中啟動服務
1)首先了解下在服務啟動的流程
1. 在你的應用中讓init.rc中添加的服務啟動起來。
首先了解下在服務啟動的流程:
在設備目錄下的init.c(切記並不是system/core/init/init.rc)
Main函數的for(;;)循環中有一個handle_property_set_fd(),函數:
for (i = 0; i < fd_count; i++) {
if (ufds[i].revents == POLLIN) {
if (ufds[i].fd == get_property_set_fd())
handle_property_set_fd();
else if (ufds[i].fd == get_keychord_fd())
handle_keychord();
else if (ufds[i].fd == get_signal_fd())
handle_signal();
}
}
這個函數的實現也在system/core/init目錄下,該函數中的check_control_perms(msg.value, cr.uid, cr.gid)函數就是檢查該uid是否有許可權啟動服務(msg.value就是你服務的名字),如果應用為root或system用戶則直接返回1.之後就是調用handle_control_message((char*) msg.name + 4, (char*) msg.value),該函數的參數就是去掉1.ctl.後的start和2.你服務的名字。這個函數的詳細內容:
void handle_control_message(const char *msg, const char *arg)
{
if (!strcmp(msg,"start")) {
msg_start(arg);
} else if (!strcmp(msg,"stop")) {
msg_stop(arg);
} else if (!strcmp(msg,"restart")) {
msg_stop(arg);
msg_start(arg);
} else {
ERROR("unknown control msg '%s'\n", msg);
}
}
匹配start後調用msg_start.服務就這樣起來了,我們的解決方案就是在檢查許可權的地方「下點功夫」,因為我們不確定uid,所以就讓check_control_perms這個函數不要檢查我們的uid,直接檢查我們服務的名字,看看這個函數:
static int check_control_perms(const char *name, unsigned int uid, unsigned int gid) {
int i;
if (uid == AID_SYSTEM || uid == AID_ROOT)
return 1;
/* Search the ACL */
for (i = 0; control_perms[i].service; i++) {
if (strcmp(control_perms[i].service, name) == 0) {
if ((uid && control_perms[i].uid == uid) ||
(gid && control_perms[i].gid == gid)) {
return 1;
}
}
}
return 0;
}
這個函數裡面是必須要檢查uid的,我們只要在for循環上寫上。
if(strcmp(「usblp_test」,name)==0) //usblp_test就是我們服務的名字。
return 1;
這樣做不會破壞android原本的結構,不會有什麼副作用。
init.c和init.rc都改好了,現在就可以編譯源碼了,編譯好了裝到機子開發板上就可以了。

㈧ 如何進入Android adb shell 命令行模式

  1. 首先打開adb,輸入adb devices,如果出現XXXXX devices表示手機已經連接,如果出現XXXXX offline表示斷線。

  2. 手機確定連接之後直接輸入 adb shell,就可以進入linux 的shell命令了,後面直接可以輸入shell命令

  3. http://www.open-open.com/lib/view/1327557366686這個是adb shell的一些常用的命令。

㈨ 一共有幾種命令行 安卓的shell命令、back track的終端、還有cdlinux終端等等

如串口線不一樣的軟體,但都是linux的虛擬終端,
androidshell命令,一般是在一個軟體中運行,也是一個虛擬終端。
linux,如back track的終端、還有cdlinux終端,有很多很多的不同終端可用,其作用是一樣的。
android的命令解釋器,和各種,命令是與linux不同的。

事實上,在個人電腦出現前,人們通過一個設備,使用短的連線(如串口線),或者遠距離的電話線連接大型計算機,來使用這個大型計算機;這個設備有鍵盤,顯示器,但是沒有處理能力,即沒有處理器,沒有存儲器,……,僅僅是用來駛入命令,顯示運行結果的設備,這就是終端。

後來,有了個人計算機,終端與主機一體了,但linux與unix有親緣關系,而unix就是很多大型機的操作系統,linux也就延續了終端的設計,保留了終端的框架,這是個低層的設計。linux所有的命令,都是通過終端輸入,輸出的。而且,現在的Linux仍然像早期的大型機一樣,可以連接很多很多的終端。

而在圖形界面下,不能直接使用終端的,就使用軟體終端的方法來實現,這個就是虛擬終端。不管是cdlinux,back track,ubuntu,fedroa,……,所有的linux,unix,bsd,在圖形界面下的命令的輸入輸出軟體,都是虛擬終端。有很多不同版本的虛擬終端,雖然界面,菜單,……,不同,但功能是一樣的。

android使用了linux的內核,雖然其他的都是自己搞的,但既然內核是linux的,android系統也使用了很多linux的概念的框架,基本與linux是一致的。

實際上,有另一個概念,虛擬終端僅僅是給shell的運行創造了一個環境,真正實現命令解釋的,是shell。linux有很多不同的shell,如,sh,bash,ash,csh,zsh,busybox,等等。
android自己做了一個shell,與linux的基本一樣,但不與linux完全相同;不過,android可以使用busybox,也可以通過chroot 運行一個完全的linux

㈩ 如何用shell命令打開某個手機app

android中執行shell命令有兩種方式:1.直接在代碼中用java提供的Runtime這個類來執行命令,以下為完整示例代碼。publ

閱讀全文

與安卓shell命令相關的資料

熱點內容
伺服器如何看日活數 瀏覽:684
數控車床原理圖及編程 瀏覽:287
java文件流下載 瀏覽:336
編程工作工資多少 瀏覽:437
專業安全文件夾 瀏覽:777
表格里的根號演算法怎麼打 瀏覽:193
javacorepdf 瀏覽:573
pdf轉換word編輯 瀏覽:446
35歲程序員實習期恐慌 瀏覽:701
如何做一個系統u盤文件夾名字 瀏覽:968
如何確認哪個ip重啟了伺服器 瀏覽:130
照片壓縮軟體綠色版 瀏覽:109
pgp基於什麼體系加密 瀏覽:637
python合法賦值語句格式 瀏覽:715
程序員數學線性代數 瀏覽:624
看幀率app如何使用 瀏覽:525
從DHC伺服器租用IP地址 瀏覽:477
編譯怎麼學 瀏覽:333
數碼管顯示0到9plc編程 瀏覽:667
伺服器是為什麼服務的 瀏覽:769