Java教程

JAVA,获取手机屏幕大小

本文主要是介绍JAVA,获取手机屏幕大小,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

效果如图,返回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;

    }
}

 

这篇关于JAVA,获取手机屏幕大小的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!