`
dowhathowtodo
  • 浏览: 779013 次
文章分类
社区版块
存档分类
最新评论

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下编译运行通过

如共享到期,不能下载请留言。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics