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"><EditTextandroid: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"/><Buttonandroid: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;@Overrideprotected 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命令相关推荐
- 机顶盒开发中常用adb shell 命令
开启/关闭adb服务 adb start-server / adb kill-server adb连接设备/断开设备 adb connect ip / adb disconnect 显示连接设备状态/ ...
- 【android-tips】android程序执行adb shell命令(实例源码)
(转载请注明出处:http://blog.csdn.net/buptgshengod) package net.gimite.nativeexe;import java.io.BufferedRead ...
- Android 常用adb shell 命令
原文地址http://blog.csdn.net/rain_butterfly/article/details/40894807 调试Android程序有时需要adb shell 命令,adb全称An ...
- dos命令行输入adb shell命令为什么报错
在命令行(就是开始--运行--输入cmd)模式下输入adb shell命令一般会报两种错误,一是"adb不是内部命令或外部命令,也不是可运行的程序或批处理文件",二是"e ...
- adb shell读取返回值_android代码执行adb shell终端命令(linux命令)并返回执行结果...
在做Android开发板相关的开发需求的时候,我们有的时候需要去到Android系统的一个linux终端(adbshell)里面来执行一些命令,以便于查看部分文件内容或者修改部分文件权限等需求,通常我 ...
- 在命令行中通过adb shell am broadcast发送广播通知以及Android的常用adb命令
转载自:http://www.cnblogs.com/622698abc/archive/2013/06/11/3132306.html?utm_source=tuicool 通过命令行执行adb s ...
- Android 设备,如何root,执行adb shell,查看设备中的数据库信息等
有个测试机里没有自带sqlite,记录了一下安装过程.以防遗忘 (一)第一步:root 使用百度一键root 等app,一键就可root,步骤略 (二) 1.执行 adb shell su 获得roo ...
- batT脚本如何自动执行 adb shell 以后的命令(android抓包)
bat脚本自动执行 adb shell 以后的命令 @echo off echo su > test.txt echo 其它命令(如 cd /data) >> test.txt ad ...
- Android adb shell 命令
adb 概述 SDK的Tools文件夹下包含着Android模拟器操作的重要命令 adb,adb的全称为(Android Debug Bridge就是调试桥的作用.通过adb我们可以在Eclipse中 ...
- adb shell 命令详解,android, adb logcat
http://www.miui.com/article-275-1.html http://noobjava.iteye.com/blog/1914348 adb shell 命令详解,android ...
最新文章
- 恭喜,五年时间再造一家赴美上市公司
- POJ 2104 K-th Number
- Python_基础_2
- Edward Gaming, the Champion 字符串,模拟(2021.11.沈阳)
- tensorflow打印模型图_[深度学习]TensorRT加速tensorflow实例
- python定时刷新tkinter界面_如何在python中“刷新”Tkinter窗口?
- python datetime库_python datetime库使用代码详解
- Vijos CoVH之再破难关(搜索+hash)
- 雅虎网站页面性能优化的34条黄金守则
- 用拼音输入法字典库实现同音字模糊查询
- Windows驱动开发(一)WDM/WDF驱动概述
- java 生成分页sql_DataGrid连接Access的快速分页法——动态生成SQL语句
- java下载excel(excel含有多张网络图片)
- 计算机的开机键的英语,每次开机出现英文(按任意键),才能进入电脑,好烦...
- 沐风:如何创造更多的睡后收入?
- 000001历史数据_上证指数(000001) 的历史行情2000
- 被ddos攻击,附加IP有作用吗
- http 状态详细说明
- 服务器未能启动w3svc,win10,iis10无法启动,w3svc服务无法启动
- 烂泥行天下 php,烂泥:php5.6源码安装及php-fpm配置与nginx集成
热门文章
- 【树莓派】搭建局域网内网页实时视频监控
- 生产调度系统算法模型简要设计
- ps手柄震动测试软件,PS3 可实现震动 用PS3手柄连接电脑图文教程 - 电玩巴士
- MFC架构下的DirectX8
- aria2c 官方手册中文翻译版
- vb 远程连接 SQLserver数据库的连接语句
- 国家计算机考试报名照片编辑器,Artensoft Photo Editor(电脑照片编辑软件) V1.5 官方版...
- PyInstaller:编译exe与反编译
- 在xp3下,Apache , PHP, Zend Studio怎样配置环境?
- 费尔个人防火墙采用两种封包过滤技术