效果如图,返回list:
代码如下:
package com.java.app01; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; public class GetScreenSize { public static void main(String[] args) { ArrayList<String> screenSize = getScreenSize(); System.out.println(screenSize); } public static ArrayList<String> getScreenSize() { Runtime runtime = Runtime.getRuntime(); ArrayList<String> list = new ArrayList<>(); try { Process wmSize = runtime.exec("adb shell wm size"); InputStream inputStream = wmSize.getInputStream(); InputStreamReader reader = new InputStreamReader(inputStream); BufferedReader bf = new BufferedReader(reader); String mark; StringBuilder buffer = new StringBuilder(); while ((mark = bf.readLine()) != null) { buffer.append(mark); } String s = buffer.toString(); for (String size : s.replaceAll("[^0-9]", ",").split(",")) { if (size.length() > 0) list.add(size); } } catch (IOException e) { e.printStackTrace(); } return list; } }