Android运行adb Shell命令(范例),包含代码和XML

上图大家看效果

Android运行adb Shell命令(范例),包含代码和XML_第1张图片

1、直接上java代码,MainActivity.java

package com.runqian.adbshell;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

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;

public class MainActivity extends AppCompatActivity {

private TextView outputView;
private Button localRunButton;
private EditText localPathEdit;
Handler handler = new Handler();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    outputView = (TextView)findViewById(R.id.outputView);
    localPathEdit = (EditText)findViewById(R.id.localPathEdit);
    localRunButton = (Button)findViewById(R.id.localRunButton);
    localRunButton.setOnClickListener(onLocalRunButtonClick);
}

private View.OnClickListener onLocalRunButtonClick = new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        String output = exec(localPathEdit.getText().toString());
        output(output);
    }
};

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() {
        @Override
        public void run() {
            outputView.setText(str);
        }
    };
    handler.post(proc);
}
}

2、xml文件

(1)activity_main.xml






    
    

(2)button_bg.xml






(3)button_1_focused.xml(button_1_normal自己仿照写一个)






3、AndroidManifest.xml






    
        
            

            
        
    



4、验证在TV端进行(也可以手机直接运行),所以用到了截屏和保存本地,命令如下:

adb shell /system/bin/screencap -p /sdcard/screenshot.png
adb pull /sdcard/screenshot.png D:\Yobbom

此步骤大家可以忽略,仅个人做记录使用。

你可能感兴趣的:(Android基础)