Android開發程序代碼獲取root權限

把apk放到/system/app文件夾下,作為系統項目,就可以保證項目有root的權https://限。

root權限的app可以申請很多一般用戶無法申請的權限。

參考:點擊打開鏈接

可以查看哪些權限是需要root才能申請的。

package com.example.netaccesstest;

import java.io.DataOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Button open = (Button) findViewById(R.id.open);
		Button close = (Button) findViewById(R.id.close);
		open.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				try {
					CopyAppToSystem();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		close.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				RunAsRooter();
			}
		});
	}
	
	private void RunAsRooter()
	{
		try {
	          Process process = Runtime.getRuntime().exec("su");
	          process.waitFor();
	        } catch (IOException e) {
	          e.printStackTrace();
	        } catch (InterruptedException e) {
	          e.printStackTrace();
	        }
	}
	
	private void CopyAppToSystem() throws IOException
	{
		Process process = Runtime.getRuntime().exec("su");
		DataOutputStream out = new DataOutputStream(process.getOutputStream());
		out.writeBytes("mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system\n");
		out.writeBytes("cat /sdcard/myApp.adk > /system/app/myApp.adk\n");
		out.writeBytes("mount -o remount,ro -t yaffs2 /dev/block/mtdblock3 /system\n");
		out.writeBytes("exit\n");  
		out.flush();
		try {
			process.waitFor();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

發佈留言