Android 代码中执行adb shell命令

xml代码:

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
    android:paddingVertical="25dp"
    android:paddingHorizontal="25dp"
    tools:context=".AdbRunActivity">

    <EditText
        android:id="@+id/activity_adb_run_cmd_et"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:hint="@string/activity_adb_run_cmd"/>

    <Button
        android:id="@+id/activity_adb_run_cmd_bt"
        android:layout_marginTop="40dp"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:text="@string/activity_adb_run_cmd_run"
        android:textAllCaps="false"
        android:background="@color/purple_200"/>

</LinearLayout>

主代码:

需要输入正确的adb shell 命令,实际运行只需要adb shell 后面的命令

package com.fzw.csdnapplication;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.IOException;

public class AdbRunActivity extends Activity {
    private static final String TAG = "CsdnApplication AdbRunActivity";

    private EditText cmdEt;

    private Button runCmdBt;

    private String result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG,"onCreate");
        setContentView(R.layout.activity_adb_run_acivity);
        initView();
        clickEvent();
    }

    private void initView() {
        Log.i(TAG,"initView");
        cmdEt = findViewById(R.id.activity_adb_run_cmd_et);
        runCmdBt = findViewById(R.id.activity_adb_run_cmd_bt);
    }

    private void clickEvent() {
        runCmdBt.setOnClickListener(view -> {
            result = runCmd(cmdEt.getText().toString());
            showToast();
            Log.d(TAG, "run result: " + result);
        });
    }

    private void showToast() {
        Log.i(TAG, "show Toast");
        String toastText = "执行失败,请检查shell命令!";
        if (!TextUtils.isEmpty(result)) {
            toastText = "执行成功!";
        }
        Toast.makeText(this,toastText, Toast.LENGTH_SHORT).show();
    }

    private String runCmd(String cmd) {
        Log.d(TAG, "runCmd: " + cmd);
        if (TextUtils.isEmpty(cmd) || cmd.length() < 11) {
            return "";
        }
        String cmdHead = cmd.substring(0, 9);
        if (!"adb shell".equals(cmdHead)) {
            return "";
        }
        return execRootCmd(cmd);
    }

    /**
     * 执行命令并且输出结果
     */
    public static String execRootCmd(String cmd) {
        String content = "";
        try {
            cmd = cmd.replace("adb shell","");
            Process process = Runtime.getRuntime().exec(cmd);
            Log.d(TAG,"process " + process.toString());
            content = process.toString();
        } catch (IOException e) {
            Log.d(TAG,"exception " + e.toString());
            e.printStackTrace();
        }
        return content;
    }
}

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值