Android中级篇之用JAVA代码执行shell命令

[日期:2011-12-08] 来源:Linux社区 作者:y13872888163

在Android可能有的系统信息没有直接提供API接口来访问,为了获取系统信息时我们就要在用shell指令来获取信息,这时我们可以在代码中来执行命令 ,这里主要用到ProcessBuilder 这个类.


代码部分 :

1.package com.yin.system_analysis;  2.import java.io.File;  3.import java.io.IOException;4.import java.io.InputStream;5.import android.app.Activity;6.import android.os.Bundle;7.import android.util.Log;  8.import android.view.View;9.import android.view.View.OnClickListener;10.import android.widget.Button;  11.import android.widget.TextView;12.public class MainActivity extends Activity {13.    private final static String[] ARGS = {"ls","-l"};14.    private final static String TAG = "com.yin.system";15.    Button mButton;  16.    TextView myTextView;  17.    public void onCreate(Bundle savedInstanceState) {18.        super.onCreate(savedInstanceState);19.        setContentView(R.layout.main);  20.        mButton = (Button) findViewById(R.id.myButton);  21.        myTextView = (TextView) findViewById(R.id.textView);  22.          23.        mButton.setOnClickListener(new OnClickListener() {  24.              25.            public void onClick(View v) {  26.                  27.                myTextView.setText(getResult());  28.            }  29.        });  30.    }  31.    public String getResult(){32.         ShellExecute cmdexe = new ShellExecute ( );33.         String result="";34.         try {35.            result = cmdexe.execute(ARGS, "/");36.        } catch (IOException e) {  37.            Log.e(TAG, "IOException");38.            e.printStackTrace();  39.        }  40.        return result;41.    }  42.    private class ShellExecute {43.        /*44.         * args[0] : shell 命令  如"ls" 或"ls -1";45.         * args[1] : 命令执行路径  如"/" ;46.         */  47.        public String execute ( String [] cmmand,String directory)  48.        throws IOException {  49.        String result = "" ;  50.        try {  51.        ProcessBuilder builder = new ProcessBuilder(cmmand);  52.          53.        if ( directory != null )  54.        builder.directory ( new File ( directory ) ) ;  55.        builder.redirectErrorStream (true) ;  56.        Process process = builder.start ( ) ;  57.          58.        //得到命令执行后的结果   59.        InputStream is = process.getInputStream ( ) ;  60.        byte[] buffer = new byte[1024] ;  61.        while ( is.read(buffer) != -1 ) {  62.        result = result + new String (buffer) ;  63.        }  64.        is.close ( ) ;  65.        } catch ( Exception e ) {  66.            e.printStackTrace ( ) ;  67.        }  68.        return result ;  69.        }  70.    }  71.}  



布局文件很简单就不列出了

本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接:http://www.linuxidc.com/Linux/2011-12/48958.htm

这篇是转载别人的,:-D

更多相关文章

  1. 〖Android〗OK6410a的Android HAL层代码编写笔记
  2. Android iPhone 手机查看基站信息
  3. Android dumpsys命令详细使用
  4. Android常用adb命令总结(二)
  5. Android:如何从堆栈中还原ProGuard混淆后的代码
  6. android recovery 主系统代码分析
  7. Android Stuido Ndk-Jni 开发(二):Jni中打印log信息
  8. android ndk开发中常用的系统自带网络命令
  9. 史上最强劲之android模拟器命令详解

随机推荐

  1. mysql从入门到优化(4)视图的基本操作
  2. 熬夜多疯狂,体检报告就有多刺激!我怂了
  3. Android(安卓)Wifi方法大全
  4. 中年程序员的困惑
  5. 我们都是IT民工--流浪人IDE开发札记--牛
  6. 去芜湖、看房子、拿体检报告,魔幻的2021开
  7. 一个带有邮戳的锁StampedLock(jdk1.8出现)
  8. Android常用功能实例
  9. Flux
  10. Redux