[英]How to write adb commands inside a Android application?
我想知道如何從我的代碼中執行adb命令。 例如,我想在adb shell內推送一個文件,我這樣寫:
package org.example.adbshell;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Process process;
try {
process = Runtime.getRuntime().exec("adb push C:/Users/Savio/Desktop/savio.xml /storage/sdcard0/");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
我應該能夠在/ storage / sdcard0文件夾中看到savio.xml。 但是由於某種原因,我看不到該文件。 我猜該命令沒有得到執行。 我在這里做錯了什么?
共有三種命令
mv/edit/cp/cd
等 嘗試這個
Process process Runtime.getRuntime().exec("your command");
//or
Process process Runtime.getRuntime().exec("/path/your_command");
它為我復制文件的工作:
Process process Runtime.getRuntime().exec("/system/bin/mv my_file_path");
您可以在process
對象的幫助下讀取輸出數據
// Use buffer reader for the same.
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
Android方面沒有ADB命令。 ADB僅是Android SDK的一部分,並且始終在非Android的計算機上運行。
同樣,Android對C:/Users/Savio/Desktop/savio.xml也不了解,因為它不是其文件系統中的文件。
您應該使用HTTP(或類似服務器)實現桌面應用程序,並使用它從Android下載文件並將其上傳到PC。 您可以從NanoHTTP(輕量級HTTP實現)開始。
或者,您可以使用服務器在Android與計算機之間進行通信,並根據Android的請求在該計算機上運行ADB命令。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.