2010-05-16 9 views
12

एंड्रॉइड ऐप के माध्यम से टर्मिनल को कमांड कैसे भेजें और आउटपुट वापस प्राप्त करें? उदाहरण के लिए, "एलएस /" भेजना और जीयूआई में इसे प्रिंट करने के लिए आउटपुट प्राप्त करना? मैं उन्हें अभी तक परीक्षण नहीं किया है http://code.google.com/p/market-enabler/wiki/ShellCommands :एंड्रॉइड एप्लिकेशन में टर्मिनल कमांड कैसे चलाएं?

उत्तर

11

आप प्रतिबिंब का उपयोग करने के android.os.Exec.createSubprocess (कॉल करने के लिए) है:

public String ls() { 
    Class<?> execClass = Class.forName("android.os.Exec"); 
    Method createSubprocess = execClass.getMethod("createSubprocess", String.class, String.class, String.class, int[].class); 
    int[] pid = new int[1]; 
    FileDescriptor fd = (FileDescriptor)createSubprocess.invoke(null, "/system/bin/ls", "/", null, pid); 

    BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fd))); 
    String output = ""; 
    try { 
     String line; 
     while ((line = reader.readLine()) != null) { 
      output += line + "\n"; 
     } 
    } 
    catch (IOException e) {} 
    return output; 
} 
+0

execClass क्या है? –

+0

ओह, यह परावर्तित वर्ग होगा। –

+2

मुझे "java.lang.ClassNotFoundException: android.os.Exec" मिल रहा है " – ademar111190

1

अलग समाधान यहां पाया जा सकता है।

1

एंड्रॉयड पर खोल आदेशों को चलाने के लिए रास्ता नहीं है इस जवाब का प्रयास करें प्रोग्राम के रूप में https://stackoverflow.com/a/3350332/2425851

संबंधित मुद्दे