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

简介: <div class="markdown_views"><h1 id="android实训案例二android下的cmd命令之关机重启以及重启recovery">Android实训案例(二)——Android下的CMD命令之关机重启以及重启recovery</h1><hr><blockquote> <p>Android刚兴起的时候,着实让一些小众软件火了一把,切水果

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


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

这里写图片描述

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

截图

这里写图片描述

一.了解CMD 命令

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

重启:su -c reboot

关机:reboot -p

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

activity_main.xml

<LinearLayout xmlns:android="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" >

    <Button
        android:id="@+id/btn_reboot"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:background="@drawable/btn_bg"
        android:text="重启" />

    <Button
        android:id="@+id/btn_power"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:background="@drawable/btn_bg"
        android:text="关机" />

    <Button
        android:id="@+id/btn_recovery"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:background="@drawable/btn_bg"
        android:text="recovery" />

    <Button
        android:id="@+id/btn_finish"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:background="@drawable/btn_bg"
        android:text="退出" />

</LinearLayout>

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;

        }
    }
}

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

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

目录
相关文章
|
Android开发
Android Mediatek 增加Recovery模式下读cmdline的强制工厂重置选项
Android Mediatek 增加Recovery模式下读cmdline的强制工厂重置选项
397 0
|
监控 Android开发 C语言
深度解读Android崩溃日志案例分析2:tombstone日志
深度解读Android崩溃日志案例分析2:tombstone日志
1949 0
|
缓存 Unix Android开发
Android安卓项目调试之Gradle 与 Gradle Wrapper的概念以及常用gradle命令深度详解-优雅草卓伊凡
Android安卓项目调试之Gradle 与 Gradle Wrapper的概念以及常用gradle命令深度详解-优雅草卓伊凡
1571 8
|
Android开发
定制Android关机界面
定制Android关机界面
697 0
|
Java Android开发 Spring
Android Spingboot 实现SSE通信案例
【7月更文挑战第14天】以下是使用Android和Spring Boot实现SSE(Server-Sent Events)通信的案例摘要: 在`MainActivity`中: - 初始化界面元素并设置按钮点击事件。 - `startSseRequest`方法创建`WebClient`对象,设置请求头,发送请求,并处理响应和错误。 请确保将`your-server-url`替换为实际的服务器地址。
937 14
|
安全 API Android开发
Android打开USB调试命令
【6月更文挑战第20天】
1512 1
|
JSON Java API
【Android】使用 Retrofit2 发送异步网络请求的简单案例
**摘要:** Retrofit是Android和Java的HTTP客户端库,简化了RESTful API交互。它通过Java接口定义HTTP请求,并提供注解管理参数、HTTP方法等。要使用Retrofit,首先在AndroidManifest.xml中添加`INTERNET`权限,然后在`build.gradle`中引入Retrofit和Gson依赖。创建服务器响应数据类和描述接口的接口,如`Result`和`Api`。通过Retrofit.Builder配置基础URL并构建实例,之后调用接口方法创建Call对象并发送异步请求。
834 1
|
Java Android开发
Android 对adb命令的拦截
Android 对adb命令的拦截
568 2
android-agent-web中js-bridge案例
android-agent-web中js-bridge案例
480 2
|
安全 Java Shell
Android13 adb input 调试命令使用和源码解析
Android13 adb input 调试命令使用和源码解析
4594 1

热门文章

最新文章