Java Swing控件实现PC端获取连接的android设备和设备中的应用包名

最近在做一款基于java swing控件的测试平台,用swing控件实现了monkey测试,日志抓取,性能监控,测试脚本执行,测试报告查看。在做monkey测试和日志抓取等操作时需要指定设备和应用包名,下面我就分享如何用java Swing控件实现当前连接的手机设备号和当前设备所有的应用包名。

下面先附上效果图:


Java Swing控件实现PC端获取连接的android设备和设备中的应用包名_第1张图片
image.png

那么再说说实现的原理
1)先编写工具类获取设备名和包名分别存入list集合中
2)Jpanel面板添加Jbutton,Jlable,Jcombox,点击刷新按钮刷新设备号,点击设备号刷新当前设备号对应的包名,点击监听使用子线程防止UI卡顿
3)主类继承Jframe,构造方法中添加Jpanel。

下面是具体代码:

1.以下代码获取当前连接android手机设备号

package DevicesPackageFlush;

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

//获取当前连接的设备,返回list集合

public class AndroidDevicesInfo {
    ListdevicesList=new ArrayList();
    public List getAndroidDevicesInfo(){
        String command="adb devices";
        String line = null;
        StringBuilder sb = new StringBuilder();
        Runtime runtime = Runtime.getRuntime();
        try {
            Process process = runtime.exec(command);
            BufferedReader  bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            while ((line = bufferedReader.readLine()) != null) {
                if(line.contains("device")){
                    if(!line.contains("devices")){
                        //截取设备字符串
                        String deviceId=line.substring(0, line.indexOf("device"));
                        devicesList.add(deviceId);
                    }
                
                }
            
            }
        } catch (IOException e) {
            
            e.printStackTrace();
        }
        return devicesList;
        
    }

}

2.以下代码是获取指定设备的所有包名

package DevicesPackageFlush;

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

//获取应用的所有包名,返回list集合

public class AndroidPackageInfo {
    ListpackageList=new ArrayList();
    public List getAndroidPackageInfo(String deviceId){
        
        //获取包名adb命令
        String command="adb -s "+deviceId+" shell pm list packages";
        String line = null;
        StringBuilder sb = new StringBuilder();
        Runtime runtime = Runtime.getRuntime();
        
        //执行命令
        try {
            Process process = runtime.exec(command);
            BufferedReader  bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            while ((line = bufferedReader.readLine()) != null) {
                if(line.contains("package")){
                    //截取包名字符串
                    String packageItem=line.substring(line.indexOf("package")+8,line.length());
                    //String packageItem=line;
                    packageList.add(packageItem);
                }
            }
        } catch (IOException e) {
            
            e.printStackTrace();
        }
        return packageList;
        
    }

}

3.Jpnal面板代码

package DevicesPackageFlush;

import java.awt.Color;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.naming.Context;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;




public class JpanelDevices extends JPanel{
    
    AndroidDevicesInfo androidDevicesInfo;
    AndroidPackageInfo androidPackageInfo;
    JComboBox devicesComBox;
    JComboBox packageComBox;
    List list;
    String devicesId;
    String devicesId_s;
    JButton jButton;
    JLabel label2,label1;

    public String getDevice(){
        //JComboBox devicesComBox=new JComboBox<>();
        //this.devicesComBox=devicesComBox;
        return (String) devicesComBox.getSelectedItem();
    }
    
    
    
    public JpanelDevices(){
        //this.context=context;
        //setBorder(BorderFactory.createTitledBorder("分组框")); //设置面板边框,实现分组框的效果,此句代码为关键代码

        //setBorder(BorderFactory.createLineBorder(Color.red));
        
        setBounds(0,0, 720,100);
        setLayout(null);
        initView();
        addView();
        clickListenser();
        getDevice();
            
    }
    
    public void initView(){
        jButton=new JButton("刷新设备");
        
        label2=new JLabel("设备id:");
        label2.setBounds(120, 35,50, 25);
        
        label1=new JLabel("应用包名:");
        label1.setBounds(368, 35, 60, 25);
        //第一个,第二个参数表示坐标,后两个参数是高度和长度
        jButton.setBounds(10,35,100,25);
        
        //s=deviceArray();
        devicesComBox=new JComboBox();
        
        packageComBox=new JComboBox();
        devicesComBox.setBounds(170, 35,192, 25);
        packageComBox.setBounds(438, 35,265, 25);
    }
    
    public void addView(){
         add(jButton);

         add(label1);
         add(label2);

         add(devicesComBox);
         add(packageComBox);
            
    }
    private void clickListenser(){
        jButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            new DevicesThread(devicesComBox).start();
                                                            
            }
        });

        devicesComBox.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                new PackageThread(devicesComBox,packageComBox,devicesId).start();
                
            }
        });
        
    }
    
    
}

4.以下是加载设备的线程

package DevicesPackageFlush;

import java.util.List;


import javax.swing.JComboBox;



//加载设备线程
public class DevicesThread extends Thread{
    AndroidDevicesInfo androidDevicesInfo;
    
    Object obj;
    
    String[] s;
    
    JComboBox devicesComBox;
    
    
    List list;
    
    String devicesId;
    
    

    public  DevicesThread(JComboBox devicesComBox) {
        // TODO Auto-generated constructor stub
        this.devicesComBox=devicesComBox;
        //System.out.println("aa");
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        super.run();
        // TODO Auto-generated method stub
        devicesId=(String) devicesComBox.getSelectedItem();
        System.out.println(devicesId);
        
        androidDevicesInfo=new AndroidDevicesInfo();
        list=androidDevicesInfo.getAndroidDevicesInfo();
        devicesComBox.removeAllItems();
        
        if(list.size()==0){
            devicesComBox.addItem("无设备连接");
        }
        else{
            for (String item : list) {
                System.out.println(item);
                devicesComBox.addItem(item);
            }
        }
        
    }
    }

5.以下是加载包名的线程

package DevicesPackageFlush;

import java.util.List;

import javax.swing.JComboBox;


//加载包名线程

public class PackageThread extends Thread {
    AndroidDevicesInfo androidDevicesInfo;
    AndroidPackageInfo androidPackageInfo;
    Object obj;
    
    String[] s;
    
    JComboBox devicesComBox;
    JComboBox packageComBox;
    
    List list;
    
    String devicesId;
    
    public PackageThread(JComboBox devicesComBox,JComboBox packageComBox,String devicesId) {
        // TODO Auto-generated constructor stub
        this.devicesComBox=devicesComBox;
        this.packageComBox=packageComBox;
        this.devicesId=devicesId;
        
    }
    @Override
    public void run() {
        // TODO Auto-generated method stub
        super.run();
        // TODO Auto-generated method stub
        devicesId=(String) devicesComBox.getSelectedItem();
        System.out.println(devicesId);
        
        androidPackageInfo=new AndroidPackageInfo();
        list=androidPackageInfo.getAndroidPackageInfo(devicesId);
        packageComBox.removeAllItems();
        if(list.size()==0){
            packageComBox.addItem("未识别应用包名");
        }
        else{
            for (String item : list) {
                System.out.println(item);
                packageComBox.addItem(item);
            }
        }
        
    }

}

6.最后在主类中添加Jpanal面板

package DevicesPackageFlush;

import java.awt.Container;

import javax.swing.JFrame;
import javax.swing.WindowConstants;



public class TestMain extends JFrame {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new TestMain();

    }
    
    public TestMain(){
        setTitle("测试");
        setLayout(null);
        
        //声明整个Jframe窗体大小
        setBounds(0,0, 800,500);
        JpanelDevices jpanelDevices=new JpanelDevices();
        
        Container container=getContentPane();
        
        //将Jpanal面板添加到Jframe容器中
        container.add(jpanelDevices);
        
        //设置布局未默认布局
        setLayout(null);
        
        //设置
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
    }

}

你可能感兴趣的:(Java Swing控件实现PC端获取连接的android设备和设备中的应用包名)