Android 代码中执行adb shell命令

Android 代码中执行adb shell命令,第1张

效果图

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: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;
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/992579.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存