apk使用adb shell命令 Process p = Runtime.getRuntime().exec(cmd)

时间:2022-12-04 20:29:48

code:

package android.af.adbshell;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class MainActivity extends Activity {
	TextView editText1,editText2;
	private WebView webView1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		sh();
	}

	private void sh() {
		editText1 = (TextView) findViewById(R.id.editText1);
		editText2 = (TextView) findViewById(R.id.editText2);

		webView1 =(WebView)findViewById(R.id.webView1);
		
		Button btn_ls = (Button) findViewById(R.id.button1);
		btn_ls.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				do_exec(editText1.getText().toString());
			}
		});
		Button btn_ls1 = (Button) findViewById(R.id.button2);
		btn_ls1.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				readme();
			}
		});
		editText1.setText(listString[0].toString());
	}

	String do_exec(String cmd) {
		Log.d("[adbshell] cmd=",cmd);
		String s = "<PRE>";
		try {
			Process p = Runtime.getRuntime().exec(cmd);
			BufferedReader in = new BufferedReader(new InputStreamReader(
					p.getInputStream()));
			String line = null;
			while ((line = in.readLine()) != null) {
				s += line + "</br>"; // + "\n";
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.d("[adbshell]", "error:" + e.getMessage());
		}
		s+=s+"</PRE>";
		//editText2.setText(s);
		webView1.loadData(s, "text/html", "utf-8");
		return cmd;
	}

	String [] listString={"ls /mnt/sdcard -l","cat /proc/version","rm /mnt/sdcard/1.jpg","/system/bin/sh /mnt/sdcard/test.sh 123"};
	public Dialog show_list_ex() {
				int layoutlist =R.layout.layout_dialog_list1;
				int layoutSubId=R.layout.layout_dialog_list_item;
				final Dialog dialog = new Dialog(this, R.style.NoTitleDialog);
				View layout = LayoutInflater.from(this).inflate(layoutlist, null);
				
				TextView textviewTitle = (TextView) layout.findViewById(R.id.textviewTitle);
				textviewTitle.setText("select");
				
				ImageButton buttonCancel = (ImageButton) layout.findViewById(R.id.buttonCancel);
				buttonCancel.setOnClickListener(new Button.OnClickListener() {
					@Override
					public void onClick(View v) {
						dialog.dismiss();
					}
				});

				final ListView listView1=(ListView) layout.findViewById(R.id.listView1);
				
					List<Map<String, Object>> listData = new ArrayList<Map<String, Object>>();
					String key = "afei";
					if (listString.length == 0) {
					} else {
						for (int i = 0; i < listString.length; i++) {
							Map<String, Object> map = null;
							map = new HashMap<String, Object>();
							map.put(key, listString[i].toString());
							listData.add(map);
						}
					}
				SimpleAdapter adapter = new SimpleAdapter(this, listData,
						layoutSubId, new String[] {"afei"},
			            new int[] { R.id.textView1 });
			 
				listView1.setCacheColorHint(0);
				listView1.setFadingEdgeLength(0);		
				listView1.setDividerHeight(1);
				listView1.setAdapter(adapter);
				listView1.setOnItemClickListener(new ListView.OnItemClickListener(){
					@Override
					public void onItemClick(AdapterView<?> arg0, View v, int position,
							long item_line_id) {
							editText1.setText(listString[position].toString());
							dialog.dismiss();
					}
				});
				
				dialog.setContentView(layout);
				dialog.show();
				return dialog;
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_MENU) {
			show_list_ex();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	private void readme(){
		String s="  String s = \" < PRE > \" try {     Process p = Runtime.getRuntime().exec(cmd);     BufferedReader in =new BufferedReader(new InputStreamReader(p.getInputStream()));     String line = null;     while ((line = in.readLine()) != null) {         s += line + \"</br>\"; // + \"\n\";     }  } catch(IOException e) {     // TODO Auto-generated catch block     e.printStackTrace();     Log.d(\"[adbshell]\", \"error:\" + e.getMessage()); } s += s + \"</PRE>\"; new xDialog().show_message_error(this, \"错误\", \"设置失败!\", null);";
		//new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.NoTitleDialog)).setTitle("code").setMessage(s)	.setPositiveButton("确认", null).show();
		webView1.loadData(s, "text/html", "utf-8");
	}
}


activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <EditText
                android:id="@+id/editText1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textMultiLine"
                android:maxLines="10"
                android:minLines="3" >

                <requestFocus />
            </EditText>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <Button
                    android:id="@+id/button1"
                    android:layout_width="161dp"
                    android:layout_height="wrap_content"
                    android:text="Run" />

                <Button
                    android:id="@+id/button2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="50dp"
                    android:text="  code " />

            </LinearLayout>

        </LinearLayout>

        <WebView
            android:id="@+id/webView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />

        <EditText
            android:id="@+id/editText2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:editable="false"
            android:ems="10"
            android:inputType="textMultiLine"
            android:scrollbars="horizontal|vertical" >
        </EditText>

 

    </LinearLayout>

</RelativeLayout>

layout_dialog_list1.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#FF5566AA"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp" >
                <ImageView
                    android:id="@+id/imageView1"
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true"
                    />
                <TextView
                    android:id="@+id/textviewTitle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="10dp"
                    android:layout_toRightOf="@+id/imageView1"
                    android:text="title"
                    android:textAppearance="?android:attr/textAppearanceMedium" />
            <ImageButton
                android:id="@+id/buttonCancel"
                android:layout_width="50dp"
                android:layout_height="40dp"
                android:layout_alignParentRight="true"
                android:src="@android:drawable/ic_delete" />
        </RelativeLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="5dp"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <ListView
                android:id="@+id/listView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp" >
            </ListView>
        </LinearLayout>


    </LinearLayout>

</LinearLayout>

layout_dialog_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/dialog_list_item"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
    	android:layout_marginTop="4dp"
    	android:layout_marginBottom="4dp"        
        android:text="TextView" />

</LinearLayout>