Android Apk 重啓安卓終端(開機重啓)(需獲得root權限)



閒來無事,編寫了一個小小的流氓軟件,無限重啓android設備,但由於android的權限問題使得需要獲取設備的root權限。

本app設計,涉及知識點1.調用系統shell裏的reboot命令 2.檢查系統是否Root 3.監聽開機通知,使apk開機運行

重啓代碼:

String cmd = "su -c reboot";
		try {
			Runtime.getRuntime().exec(cmd);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			new AlertDialog.Builder(this).setTitle("Error").setMessage(
                    e.getMessage()).setPositiveButton("OK", null).show();
		}

判斷系統是否獲得root權限:

TextView tv = (TextView)findViewById(R.id.whetherRoot);
		if(!isRooted()) {
			tv.setText("本機沒有Root!\n如需繼續使用本軟件請自行Root!");
		}


public DataInputStream Terminal(String command) throws Exception {
		Process process = Runtime.getRuntime().exec("su");
		// 執行到這,Superuser會跳出來,選擇是否允許獲取最高權限
		OutputStream outstream = process.getOutputStream();
		DataOutputStream DOPS = new DataOutputStream(outstream);
		InputStream instream = process.getInputStream();
		DataInputStream DIPS = new DataInputStream(instream);
		String temp = command + "\n";
		// 加回車
		DOPS.writeBytes(temp);
		// 執行
		DOPS.flush();
		// 刷新,確保都發送到outputstream
		DOPS.writeBytes("exit\n");
		// 退出
		DOPS.flush();
		process.waitFor();
		return DIPS;
	}

	public boolean isRooted() {
		// 檢測是否ROOT過
		DataInputStream stream;
		boolean flag = false;
		try {
			stream = Terminal("ls /data/");
			// 目錄哪都行,不一定要需要ROOT權限的
			if (stream.readLine() != null)
				flag = true;
			// 根據是否有返回來判斷是否有root權限
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();

		}

		return flag;
	}

監視開機通知:

(android開機事件會發送一個叫做Android.intent.action.BOOT_COMPLETED的廣播信息。只要我們接收這個action並在receiver中啓動我們自己的程序就可以實現了。)

設置一個receiver接收系統發出的廣播消息

StartupReceiver.java

public class StartupReceiver extends BroadcastReceiver {  
  
    @Override  
    public void onReceive(Context context, Intent intent) {  
        // TODO Auto-generated method stub   
        Intent i = new Intent(context, RebootActivity.class);  
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
        //將intent以startActivity傳送給操作系統   
        context.startActivity(i);  
    }  
} 

然後在AndroidManifest.xml中添加(receiver部分)

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"  
      package="mzz.startup"  
      android:versionCode="1"  
      android:versionName="1.0">  
    <application android:icon="@drawable/icon" android:label="@string/app_name">  
        <activity android:name=".Hello"  
                  android:label="@string/app_name">  
            <intent-filter>  
                <action android:name="android.intent.action.MAIN" />  
                <category android:name="android.intent.category.LAUNCHER" />  
            </intent-filter>  
        </activity>  
        <receiver android:name=".StartupReceiver">  
            <intent-filter>  
                <action android:name="android.intent.action.BOOT_COMPLETED" />  
                <category android:name="android.intent.category.HOME" />  
            </intent-filter>  
        </receiver>  
    </application>  
    <uses-sdk android:minSdkVersion="8" />  
  
</manifest> 

這樣啓動了一次該程序之後,以後開機就會自動啓動該程序了。


重啓apk文件:http://115.com/file/dpvmy8yo#Reboot.apk

工程文件:http://115.com/file/c2a3bjh9#Reboot.zip  Android 2.1下編譯運行通過

如共享到期,不能下載請留言。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章