(转载请注明出处:http://blog.csdn.net/buptgshengod)

package net.gimite.nativeexe;import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;import net.gimite.nativeexe.R;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;public class MainActivity extends Activity {private TextView outputView;private Button localRunButton;private EditText localPathEdit;private Handler handler = new Handler();private EditText urlEdit;private Button remoteRunButton;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);outputView = (TextView)findViewById(R.id.outputView);localPathEdit = (EditText)findViewById(R.id.localPathEdit);localRunButton = (Button)findViewById(R.id.localRunButton);localRunButton.setOnClickListener(onLocalRunButtonClick);}private OnClickListener onLocalRunButtonClick = new OnClickListener() {public void onClick(View v) {String output = exec(localPathEdit.getText().toString());output(output);
//            try {
//
//               // Process process = Runtime.getRuntime().exec(localPathEdit.getText().toString());
//
//            } catch (IOException e) {
//                // TODO Auto-generated catch block
//                e.printStackTrace();
//            }}};// Executes UNIX command.private String exec(String command) {try {Process process = Runtime.getRuntime().exec(command);BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));int read;char[] buffer = new char[4096];StringBuffer output = new StringBuffer();while ((read = reader.read(buffer)) > 0) {output.append(buffer, 0, read);}reader.close();process.waitFor();return output.toString();} catch (IOException e) {throw new RuntimeException(e);} catch (InterruptedException e) {throw new RuntimeException(e);}}private void download(String urlStr, String localPath) {try {URL url = new URL(urlStr);HttpURLConnection urlconn = (HttpURLConnection)url.openConnection();urlconn.setRequestMethod("GET");urlconn.setInstanceFollowRedirects(true);urlconn.connect();InputStream in = urlconn.getInputStream();FileOutputStream out = new FileOutputStream(localPath);int read;byte[] buffer = new byte[4096];while ((read = in.read(buffer)) > 0) {out.write(buffer, 0, read);}out.close();in.close();urlconn.disconnect();} catch (MalformedURLException e) {throw new RuntimeException(e);} catch (IOException e) {throw new RuntimeException(e);}}private void output(final String str) {Runnable proc = new Runnable() {public void run() {outputView.setText(str);}};handler.post(proc);}}

要加入权限

<uses-permission android:name="android.permission.INTERNET"></uses-permission>


效果图


源码下载地址

【android-tips】android程序执行adb shell命令(实例源码)相关推荐

  1. android afw 权限,如何从adb shell命令启动AFW徽章应用程序?

    我无法启动Android从Adb shell命令启用工作的应用程序? 有人可以举个例子吗? 请参阅附加屏幕,我想启动Playstore应用程序. 没有徽章ICON Play商店应用程序也具有与Badg ...

  2. 工具类微信小程序-你记我账-实例源码

    自从腾讯推出微信小程序后我就不知不觉的有点着迷,从一开始的不完善到现在的风靡后来又推出了云开发(十足十的为开发者考虑,只用考虑前端逻辑,后台的一些麻烦问题都不用管).我自己对前端还行,后端就不咋地了, ...

  3. dos命令行输入adb shell命令为什么报错

    在命令行(就是开始--运行--输入cmd)模式下输入adb shell命令一般会报两种错误,一是"adb不是内部命令或外部命令,也不是可运行的程序或批处理文件",二是"e ...

  4. android ADB shell 命令启动 android程序

    今天用eclipase写了个android程序,主要用到了service.activity.broadcast,在安卓系统手机上调试通过,现在想用ADB shell命令启动这个程序,使达到的效果和在e ...

  5. batT脚本如何自动执行 adb shell 以后的命令(android抓包)

    bat脚本自动执行 adb shell 以后的命令 @echo off echo su > test.txt echo 其它命令(如 cd /data) >> test.txt ad ...

  6. Android adb shell 命令

    adb 概述 SDK的Tools文件夹下包含着Android模拟器操作的重要命令 adb,adb的全称为(Android Debug Bridge就是调试桥的作用.通过adb我们可以在Eclipse中 ...

  7. adb shell 命令详解,android, adb logcat

    http://www.miui.com/article-275-1.html http://noobjava.iteye.com/blog/1914348 adb shell 命令详解,android ...

  8. Android 常用adb shell 命令

    原文地址http://blog.csdn.net/rain_butterfly/article/details/40894807 调试Android程序有时需要adb shell 命令,adb全称An ...

  9. Android SDK: adb shell 命令的使用(am、pm、wm、screencap、monkey等)

    本文链接: https://blog.csdn.net/xietansheng/article/details/86675136 adb其他命令介绍详见上一篇: Android SDK: adb 常用 ...

最新文章

  1. 苹果8怎么投屏到电视_苹果电脑电视不在一个房间可以投屏
  2. awk2.0 — awk分隔符
  3. 背景和文字分离的matlab实现
  4. java的准动态(反射)
  5. 【Python+selenium Wendriver API】之鼠标悬停事件
  6. 邮件MIME格式分析
  7. Struts 中的自定义异常
  8. Kernel那些事儿之内存管理(2) --- 百闻不如一见
  9. CLIP-ViL:CLIP对视觉和语言任务有多大的好处?UC BerkeleyUCLA团队给出了答案!
  10. 一个页面上有大量的图片,加载很慢,你有哪些方法优化这些图片的加载?
  11. 密码字典生成工具—Crunch的使用
  12. Linux基础知识总结一
  13. 计算机地图概括的原理,第五章地图概括与自动综合
  14. python快递价格查询系统
  15. 【机器学习理论】换底公式--以e,2,10为底的对数关系转化
  16. UE:无法解析的外部符号 “public: __cdecl FKey::~FKey(void)“ (??1FKey@@QEAA@XZ)
  17. 互斥事件的概念和公式_互斥事件
  18. 洛谷P2556 [AHOI2002]黑白图像压缩(简单模拟题)
  19. 腾讯云远程链接桌面教程 腾讯云桌面远程链接桌面
  20. pdf阅读器如何语音朗读文件

热门文章

  1. python安装os模块_python的os模块(ipython,文件,目录,权限,管理)
  2. 计算机及网络技术发展趋势,网络技术发展对计算机技术的影响
  3. java压缩文件_Linux 系统学习--Java学习第118天
  4. uwsgi php,详解uWSGI的编码问题解决方法
  5. python获取列表中指定元素的下标
  6. java字符常量括_Java常量 变量 数据类型
  7. 51单片机中的定时器
  8. #error使用分析
  9. 字符设备驱动初体验(hello驱动)
  10. hql 字符串where语句_hibernate的hql查询语句总结