androidadbshell
A. 如何在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 是奇數開始。
B. 如何在android程序中執行adb shell命令
Android中執行adb shell命令的方式如下:<pre t="code" l="java"> /**
* 執行一個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 這個方法來執行對應的指令。還可以通過如下方式執行:
<pre t="code" l="java">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);
C. 如何在android程序中執行adb shell命令
android程序執行adbshell命令(實例源碼)packagenet.gimite.nativeexe;importjava.io.BufferedReader;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.net.HttpURLConnection;importjava.net.MalformedURLException;importjava.net.URL;importnet.gimite.nativeexe.R;importandroid.app.Activity;importandroid.os.Bundle;importandroid.os.Handler;importandroid.view.View;importandroid.view.View.OnClickListener;importandroid.widget.*;{privateTextViewoutputView;privateButtonlocalRunButton;privateEditTextlocalPathEdit;privateHandlerhandler=newHandler();privateEditTexturlEdit;privateButtonremoteRunButton;/**.*/@OverridepublicvoidonCreate(BundlesavedInstanceState){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);}=newOnClickListener(){publicvoidonClick(Viewv){Stringoutput=exec(localPathEdit.getText().toString());output(output);//try{//////Processprocess=Runtime.getRuntime().exec(localPathEdit.getText().toString());////}catch(IOExceptione){////TODOAuto-generatedcatchblock//e.printStackTrace();//}}};//ExecutesUNIXcommand.privateStringexec(Stringcommand){try{Processprocess=Runtime.getRuntime().exec(command);BufferedReaderreader=newBufferedReader(newInputStreamReader(process.getInputStream()));intread;char[]buffer=newchar[4096];StringBufferoutput=newStringBuffer();while((read=reader.read(buffer))>0){output.append(buffer,0,read);}reader.close();process.waitFor();returnoutput.toString();}catch(IOExceptione){thrownewRuntimeException(e);}catch(InterruptedExceptione){thrownewRuntimeException(e);}}privatevoiddownload(StringurlStr,StringlocalPath){try{URLurl=newURL(urlStr);HttpURLConnectionurlconn=(HttpURLConnection)url.openConnection();urlconn.setRequestMethod("GET");urlconn.setInstanceFollowRedirects(true);urlconn.connect();InputStreamin=urlconn.getInputStream();FileOutputStreamout=newFileOutputStream(localPath);intread;byte[]buffer=newbyte[4096];while((read=in.read(buffer))>0){out.write(buffer,0,read);}out.close();in.close();urlconn.disconnect();}catch(MalformedURLExceptione){thrownewRuntimeException(e);}catch(IOExceptione){thrownewRuntimeException(e);}}privatevoidoutput(finalStringstr){Runnableproc=newRunnable(){publicvoidrun(){outputView.setText(str);}};handler.post(proc);}}
D. 安卓怎麼進入adb shell
如果你配置了adb的環境變數 那麼你連接手機以後,直接執行 adb shell 則進入命令模式了 如果你沒有配置環境變數,那麼,你需要進入sdk\platform-tools目錄下 再執行 adb shell
E. 如何在android程序中執行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(' ');
}
//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命令,應用在附件。
F. android 進入adb shell
手機設置--開發者選項,進入之後勾選USB調試功能,沒有開發者選項的進入關於手機---軟體版本,不停的點擊版本號即可調處開發者選項。
載入ADB工具,快捷鍵WIN+R打開,輸入cmd,打開命令控制台,輸入cd+空格+adb工具包的路徑,比如cd c:/urser/administrator/desktop/adb 這個路徑是放在桌面上的路徑,如果用一些刷機軟體直接進入adb控制台的就不需要載入了。
連接手機,首先輸入dab devices,如果彈出XXXXXdeviecs,X表示機型以及一些雜七雜八的英文,表示連接,直接輸入adb shell即可進入linux環境直行shell命令。
http://wenku..com/link?url=vbDamH6atI1cxrY64c7Yi_lZ7pHCQPavGUQMKw_-S6web0H7wOcFJkYzN-sK這個是主要使用的一些shell命令。
G. 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
H. 如何進入Android adb shell 命令行模式
如果你配置了adb的環境變數
那麼你連接手機以後,直接執行
adb
shell
則進入命令模式了
如果你沒有配置環境變數,那麼,你需要進入sdk\platform-tools目錄下
再執行
adb
shell
I. android如何通過adb shell 模擬home鍵盤切換應用
1:查看當前模擬器或者Android設備實例的狀態
一般在使用前都會使用adb devices這個命令查看一下模擬器的狀態,通過這個命令得到ADB的回應信息,可以看到ADB作為回應為每個實例制定了相關的信息
1.1:emulator-5554為實例名稱
1.2:device為實例連接狀態,device表示此實例正與adb相連接,offline表示此實例沒有與adb連接或者無法響應
2:安裝和卸載APK應用程序
你可以從電腦上復制一個APK應用到模擬器或者Android設備上,通過adb install <path_to_apk>安裝軟體,adb uninstall <packageName>卸載軟體,如果你不知道這個包名,在AndroidManifest.xml里的找package=""就可以了
2.1: 先把apk文件拷貝到sdk目錄下的tools
2.2: 進入dos下切換到SDK的安裝路徑下的tools目錄
2.3 :執行安裝命令
adb install <path_to_apk>
發生的錯誤,因為我連接了真機,而且也打開了模擬器,所以adb給我的回應信息是「比一個多的驅動設備和模擬器」,我最後關閉掉了模擬器在運行安裝命令,就提示安裝成功了
卸載APK
3:從本機上復制文件到模擬器或者Android設備
adb push <本地路徑><遠程路徑>,<本地路徑>指的是自己的機器上或者模擬器上的目標文件,<遠程路徑>指的是遠程設備實例上的目標文件
4:從模擬器復制文件到模擬器或者Android設備
adb pull <遠程路徑><本地路徑>,<本地路徑>指的是自己的機器上或者模擬器上的目標文件,<遠程路徑>指的是遠程設備實例上的目標文件
5:使用shell命令
輸入adb shell就可以進入shell命令行了,可以使用一些常用的shell命令,如:ls命令列出了文件
J. 如何在android程序中執行adb 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);
}
}
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
效果圖: