2011-11-23 12 views
2

Je suis nouveau développeur d'applications Android
Je veux savoir comment exécuter un script bash de mon activité androidComment exécuter un script bash d'activité android

Je l'ai fait que je trouve ici

public void RunAsRoot(String[] cmds) { 
    Process p = Runtime.getRuntime().exec("su"); 
    DataOutputStream os = new DataOutputStream(p.getOutputStream());    
    for (String tmpCmd : cmds) { 
     os.writeBytes(tmpCmd+"\n"); 
    }   
    os.writeBytes("exit\n"); 
    os.flush(); 
} 

mais maintenant je suis beaucoup d'erreurs comme ceci:

http://i.stack.imgur.com/4N4KC.png

Répondre

2

Je crois que vous avez juste besoin de surr les OUND avec un bloc try/catch, il est une règle Java, essayez ce code à la place:

public void RunAsRoot(String[] cmds) { 
    Process p = null; 
    try { 
     p = Runtime.getRuntime().exec("su"); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    DataOutputStream os = new DataOutputStream(p.getOutputStream());    
    for (String tmpCmd : cmds) { 
     try { 
     os.writeBytes(tmpCmd+"\n"); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    }   
    try { 
     os.writeBytes("exit\n"); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    try { 
     os.flush(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

En second lieu, exécuter manuellement su, l'identifiant de commande pour voir si peut devenir une racine, juste au cas où.

Questions connexes