Android 代码中执行adb shell命令

Posted 鹏于宴

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
    


以上是关于Android 代码中执行adb shell命令的主要内容,如果未能解决你的问题,请参考以下文章

如何在android程序中执行adb shell命令

怎么在Android Studio中执行adb命令

如何在android程序中执行adb shell命令

怎么在Android Studio中执行adb命令

如何用adb shell 指令获得android当前的activity

为啥adb shell命令模拟手机按键操作执行