前言:在最近的工作中涉及到从u盘拷贝大量数据到车机,偶尔有失效的情况,后面发现是sdcard存储空间不足,因此想在代码中展示出当前系统分区.查看系统分区在adb中为adb shell df
我们用如下方法可执行任意的adb shell命令,在此仅用"df"举例
- 代码如下
private String[] doRuntimeCmmd(String command) {
Log.d(TAG, "doRuntimeCmmd:" + command);
Process process = null;
BufferedReader mOutReader = null;
BufferedReader mErrorReader = null;
try {
process = Runtime.getRuntime().exec(command);
Log.d(TAG, "process exec: " + process);
mOutReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
int mOutread;
char[] outBuffer = new char[4096];
StringBuffer output = new StringBuffer();
while ((mOutread = mOutReader.read(outBuffer)) > 0) {
output.append(outBuffer, 0, mOutread);
}
mErrorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
int mErrorread;
char[] errorBuffer = new char[4096];
StringBuffer error = new StringBuffer();
while ((mErrorread = mErrorReader.read(errorBuffer)) > 0) {
error.append(errorBuffer, 0, mErrorread);
}
process.waitFor();
String[] mResult = { output.toString(), error.toString() };
Log.d(TAG, command +" Result:" + mResult[0]);
Log.d(TAG, command+ " Error:" + mResult[1]);
return mResult;
} catch (Exception e) {
e.printStackTrace();
String[] mResult = { "error", "error" };
Log.d(TAG, command + " Result = " + mResult[0] + " Error = " + mResult[1]);
return mResult;
} finally {
try {
if (mOutReader != null) {
mOutReader.close();
}
if (mErrorReader != null) {
mErrorReader.close();
}
if (process != null) {
Log.d(TAG, "process destroy: " + process);
process.destroy();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
我们在代码中按如下使用
String[] runtimeCmmd = doRuntimeCmmd("df");
MySortViewOfCMD textView = new MySortViewOfCMD(getActivity());
textView.setCurrentString(runtimeCmmd[0]);
用此方法得到的文本结果,并不像adb命令行中格式化的,需要自定义控件
- 自定义view实现排版
仅供参考
public class MySortViewOfCMD extends TextView {
private String text;
int lineCount = 0;
private Paint paint = new Paint();
private ArrayMap<Integer, String[]> charByMap;
private int mWidth = 900;// px
private int mHeight = 660;
private Scroller mScroller;
private int lastPointX;
private int lastPointY;
public MySortViewOfCMD(Context context) {
super(context);
initPaint(context);
}
public MySortViewOfCMD(Context context, AttributeSet attrs) {
super(context, attrs);
initPaint(context);
}
private void initPaint(Context context) {
paint.setColor(context.getResources().getColor(R.color.white));
paint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
18, context.getResources().getDisplayMetrics()));
mScroller = new Scroller(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = measureDimension(mWidth, widthMeasureSpec);
int height = measureDimension(mHeight, heightMeasureSpec);
setMeasuredDimension(width, height);
}
private int measureDimension(int defaultSize, int measureSpec) {
int result = defaultSize;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
result = specSize;
} else if (specMode == MeasureSpec.AT_MOST) {
result = Math.min(defaultSize, specSize);
} else {
result = defaultSize;
}
return result;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (int i = 0; i < charByMap.size(); i++) {//绘制行
String[] strings = charByMap.get(i);
for (int j = 0; j < strings.length; j++) {//绘制列
if (j == 1) {//针对第二列做特殊处理,防止与第一列重影
canvas.drawText(strings[j], j * (mWidth / 3) + 60,
i * 30 + 25, paint);
continue;
}
canvas.drawText(strings[j], j * (mWidth / 3), i * 30 + 25,
paint);
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
lastPointX = (int) event.getX();
lastPointY = (int) event.getY();
return true;
case MotionEvent.ACTION_MOVE:
int mXMove = (int) event.getX();
int scrolledX = (int) (lastPointX - mXMove);
if(getScrollX() + scrolledX < 0){//左边界
scrollTo(0, 0);
return true;
}
if (getScrollX() + getWidth() + scrolledX < (mWidth / 3) * 5)//小于右边界
mScroller.startScroll(getScrollX(), 0,
lastPointX - (int) event.getX(), 0, 200);
invalidate();
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
/**
* 平滑滚动
*/
@Override
public void computeScroll() {
if (mScroller.computeScrollOffset()) {
scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
invalidate();
}
}
public void setCurrentString(String text) {
this.text = text;
charByMap = saveCharByMap(text);
invalidate();
}
/**
* 根据传进来的string保存每一行的字符
*
* @param string
* -显示的字符串 eg: file size use \n filedddd size use \n
*/
private ArrayMap<Integer, String[]> saveCharByMap(String string) {
String[] lineString = string.split("\n");
ArrayMap<Integer, String[]> charMap = new ArrayMap<Integer, String[]>();
lineCount = lineString.length;
for (int i = 0; i < lineCount; i++) {
String[] charItem = lineString[i].split("\\s+");// 按空格切出字符
charMap.put(i, charItem);
}
return charMap;
}
}
感谢android,感谢开源