Android編程擷取手機root許可權問題

來源:互聯網
上載者:User

public class DemoActivity extends Activity { 
    public final String rootPowerCommand = "chmod 777 /dev/block/mmcblk0";// 授權root許可權命令 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
       new AlertDialog.Builder(this).setMessage(rootCommand(rootPowerCommand)+"....").show(); 
       File []files =  new File("/root").listFiles(); 
       if(files==null){//<strong><span style="font-size:18px;color:#ff0000;">說明是NULL。。。。就是不能訪問其下的檔案了 
</span></strong>           new AlertDialog.Builder(this).setMessage(".OK...").show(); 
       } 
      // files[0].getName(); 
    } 
    /** 
     * 授權root使用者權限 
     *  
     * @param command 
     * */ 
    public boolean rootCommand(String command) { 
        Process process = null; 
        DataOutputStream dos = null; 
        try { 
            process = Runtime.getRuntime().exec("su"); 
            dos = new DataOutputStream(process.getOutputStream()); 
            dos.writeBytes(command + "\n"); 
            dos.writeBytes("exit\n"); 
            dos.flush(); 
            process.waitFor(); 
        } catch (Exception e) { 
            return false; 
        } finally { 
            try { 
                if (dos != null) { 
                    dos.close(); 
                } 
                process.destroy(); 
            } catch (Exception e) { 
            } 
        } 
        return true; 
    } 
 

最近再寫一個檔案管理工具,需要訪問到root許可權才能訪問的檔案夾,  雖說擷取許可權成功,但是還是不能訪問到需root許可權才能訪問的檔案夾,同樣的問題我同學寫的一模一樣的擷取許可權方法,他就可以,奇怪。。  我放到已經root過的手機上測試,沒提示說要擷取root,直接就報錯了。。。。
 
今天改了一下,把dos.writeBytes("exit\n");去掉了,發現手機上提示擷取許可權成功,但是問題來了,手機黑屏,程式還在運行,就是黑屏。。。。。等下還會跳出是否強制關閉。。。這句話是啥意思呢????謝謝各位給點建議

 求解釋啊。。。。。。 


摘自  魏藝榮的專欄 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.