标签: androidcmd开发人员 914人阅读 评论(0) 收藏 举报  分类:Android(110) 

目录(?)[+]

Android实训案例(二)——Android下的CMD命令之关机重启以及重启recovery


Android刚兴起的时候,着实让一些小众软件火了一把,切水果,Tom猫,吹裙子就是其中的代表,当然还有实用性很强的关机重启软件,我们去百度上搜索一下

Android实训案例(二)——Android下的CMD命令之关机重启以及重启recovery_第1张图片

上百万的下载量是开发者都不敢想象的成绩,今天,我们就来剖析一下这款软件的开发

截图

Android实训案例(二)——Android下的CMD命令之关机重启以及重启recovery_第2张图片

一.了解CMD 命令

我们在cmd下进行的操作什么的,这里就不一一细说了我们只要知道下面这几条命令就可以了

重启:su -c reboot

关机:reboot -p

有了这个思路,我们就可以去实现了

activity_main.xml

"http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="clip_vertical"    android:orientation="vertical"    android:padding="15dp" >    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

MainActivity

package com.lgl.power;import java.io.DataOutputStream;import java.io.IOException;import android.app.Activity;import android.app.AlertDialog;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity implements OnClickListener {    private Button btn_reboot, btn_power, btn_recovery, btn_finish;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();    }    private void initView() {        btn_reboot = (Button) findViewById(R.id.btn_reboot);        btn_reboot.setOnClickListener(this);        btn_power = (Button) findViewById(R.id.btn_power);        btn_power.setOnClickListener(this);        btn_recovery = (Button) findViewById(R.id.btn_recovery);        btn_recovery.setOnClickListener(this);        btn_finish = (Button) findViewById(R.id.btn_finish);        btn_finish.setOnClickListener(this);    }    @Override    public void onClick(View v) {        switch (v.getId()) {        // 重启        case R.id.btn_reboot:            // cmd命令            String cmd = "su -c reboot";            try {                // 发送请求                Runtime.getRuntime().exec(cmd);            } catch (IOException e) {                new AlertDialog.Builder(MainActivity.this).setTitle("很抱歉")                        .setMessage("你的手机未ROOT,无法实现该功能!")                        .setPositiveButton("OK", null).show();            }            break;        // 关机        case R.id.btn_power:            try {                // 获取管理员权限su                Process process = Runtime.getRuntime().exec("su");                // 输入命令                DataOutputStream out = new DataOutputStream(                        process.getOutputStream());                out.writeBytes("reboot -p\n");                // 结束                out.writeBytes("exit\n");                out.flush();            } catch (IOException e) {                new AlertDialog.Builder(MainActivity.this).setTitle("很抱歉")                        .setMessage("你的手机未ROOT,无法实现该功能!")                        .setPositiveButton("OK", null).show();            }            break;        // recovery        case R.id.btn_recovery:            try {                // 同关机原理                Process process = Runtime.getRuntime().exec("su");                DataOutputStream out = new DataOutputStream(                        process.getOutputStream());                out.writeBytes("reboot recovery\n");                out.writeBytes("exit\n");                out.flush();            } catch (IOException e) {                new AlertDialog.Builder(MainActivity.this).setTitle("很抱歉")                        .setMessage("你的手机未ROOT,无法实现该功能!")                        .setPositiveButton("OK", null).show();            }            break;        // 退出        case R.id.btn_finish:            finish();            break;        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95

还等什么?赶紧去试试吧吧,因为我们是直接取得su权限发送脚本命令,所以我们并不需要其他的权限

Demo下载:http://download.csdn.net/detail/qq_26787115/9400681

更多相关文章

  1. android “分享”功能的实现
  2. android平台的三个编译命令----make,mm,mmm
  3. 穿越之旅之--android中如何执行java命令
  4. Android:模拟器使用命令安装apk
  5. Android 中各种JAVA 包的功能描述
  6. [入门二]Android中各种JAVA包的功能描述
  7. 初学者关于学习android中关于实现用户输入内容有提示功能(AutoCom
  8. Android中各种JAVA包的功能描述

随机推荐

  1. 课程表学习代码发布
  2. 表单的练习
  3. Java并发编程学习笔记2
  4. Puppet自动化集群管理进阶篇
  5. 用 Go 构建一个 SQL 解析器
  6. 浅谈Cgroups V2
  7. istio组件mixer初学篇
  8. 九、输入输出重定向
  9. pandas.DataFrame.sample随机抽样
  10. 常见的监控软件及特点