如何在rooted android设备上以编程方式运行ADB命令

时间:2018-03-13 06:26:51

标签: android command-line adb-shell cosu

我需要在rooted android设备上以编程方式运行adb命令。

以下不起作用,只是不起作用的例外:

Process process = Runtime.getRuntime().exec(command);
process.waitFor();

另外,如果我想将命令作为特定用户角色运行,我该如何执行呢?

2 个答案:

答案 0 :(得分:2)

您可以使用:

Process p = Runtime.getRuntime().exec( "su" );
DataOutputStream os = new DataOutputStream(p.getOutputStream());
os.writeBytes(command1 + "\n");
os.writeBytes(command2 + "\n");
os.writeBytes("exit\n");
os.flush();

由于您的设备已植根,因此必须使用su命令为root设备使用命令。我使用此解决方案从我们的应用程序重新启动我们的root设备,它工作。您可以在代码中看到添加多个命令。希望它有效。

<强>更新

您也可以使用命令数组。像这样:

Process proc = Runtime.getRuntime()
              .exec(new String[] { "su", "-c", command1,command2,"exit" });
proc.waitFor();

或者如果需要,可以使用额外的exec命令:

Runtime runtime = Runtime.getRuntime();
runtime.exec("su");
runtime.exec("export LD_LIBRARY_PATH=/vendor/lib:/system");
runtime.exec("pm clear "+PACKAGE_NAME);

这个样本也会起作用,我以前用过它们。

答案 1 :(得分:1)

我想回答你的问题第二部分,即如何将命令作为特定用户角色运行?为此你可以使用以下内置在android中的Linux命令。

run-as com.example.root.app --user u0_a86 env 

作为例子

    import sys
from PyQt5.QtWidgets import *
from bs4 import BeautifulSoup
import requests
from requests.compat import urljoin
import time
from PyQt5 import QtCore
global Currency

class MyWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.setupUI()
        self.Searching_Currency()
    def setupUI(self):
        self.setGeometry(800, 200, 300, 100)
        self.label1 = QLabel("Currency: ")
        self.label2 = QLabel("Searching for")
        self.label3 = QLabel("")
        self.lineEdit2 = QLineEdit()
        self.pushButton1= QPushButton("Search")
        self.pushButton2= QPushButton("to get alert")
        self.pushButton3= QPushButton("reset")
        self.pushButton4= QPushButton("quit")
        self.pushButton1.clicked.connect(self.btn1_clicked)
        self.pushButton2.clicked.connect(self.btn2_clicked)
        self.pushButton3.clicked.connect(self.btn3_clicked)
        self.pushButton4.clicked.connect(self.btn4_clicked)


        layout = QGridLayout()

        layout.addWidget(self.label1, 0, 0)
        layout.addWidget(self.label3, 0, 1)
        layout.addWidget(self.pushButton1, 0, 2)

        layout.addWidget(self.label2, 1, 0)
        layout.addWidget(self.lineEdit2, 1, 1)
        layout.addWidget(self.pushButton2, 1, 2)
        layout.addWidget(self.pushButton3, 2, 0)
        layout.addWidget(self.pushButton4, 2, 1)
        self.setLayout(layout)

    def btn1_clicked(self):
        global Currency
        self.Searching_Currency()
        self.label3.setText(str(Currency))

    def btn2_clicked(self):
        global Currency
        textboxValue = float(self.lineEdit2.text())
        if textboxValue > Currency:
            QMessageBox.question(self, 'alert', "done. it is  " + str
            (textboxValue), QMessageBox.Ok, QMessageBox.Ok)    
        #   #### I don't know how to use timer.. to check every 1mins.   
        # self.timer = QtCore.QTimer()
        # self.timer.timeout.connect(self.update)
        # self.timer.start(5000)
        QMessageBox.question(self, 'alert', "test message  " + str
            (textboxValue), QMessageBox.Ok, QMessageBox.Ok)      
         #trigger every minute    
        # while textboxValue < Currency:
        #         time.sleep(3)
        #         Currency=Currency-0.10
        #         break
        # QMessageBox.question(self, 'alert', "it is under the " + str(textboxValue), QMessageBox.Ok, QMessageBox.Ok)    


    def btn3_clicked(self):
        self.label3.clear()

    def btn4_clicked(self):
        sys.exit(0)

    def Searching_Currency(self):
        base_url = "https://www.finanzen100.de/waehrungen/euro-britisches-pfund-eur-gbp-_H1677059499_11341217/?ac=1"
        header = {'User-Agent': 'Mozilla/5.0'}
        r = requests.get(base_url,headers=header).text
        soup=BeautifulSoup(r,'html.parser')
        MyCurrency= soup.select(".quote__price__price")[0]
        MyCurrency=MyCurrency.get_text()
        MyCurrency =MyCurrency.replace(',','.')
        global Currency
        Currency = float(MyCurrency)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    mywindow = MyWindow()
    mywindow.show()
    app.exec_()