A general method for devices of different Android versions to correctly obtain screen resolutions

Source: Internet
Author: User

The general method for devices of different Android versions to correctly obtain the screen resolution when talking about the android screen resolution, we all know, but how to correctly obtain the screen resolution? Does android2.2 \ 2.3 \ 4.0 \ 4.1 use the same method? The answer is no. Things are evolving and changing, and android is no exception. android2.2 and 2.3 are equipped with touch screen buttons. That is to say, BACK, HOME, and MEAN on the touch screen are beyond the range of screen resolution recognition. However, after Android, it has been canceled and the software button is used, which occupies a certain height at the bottom of the screen. Therefore, you can use the same method to retrieve the screen resolution, the possible values are not the same. On the Android platform used by the author, the screen resolution is 1280X800. I wrote a method that can be slightly intelligent and get the screen resolution correctly. I mainly modified the Code: [html]-26,12 + 26,17 import android. app. progressDialog; import android. app. alertDialog; import android. content. dialogInterface; import android. util. log; + import android. util. displayMetrics; + import java. lang. reflect. method; + import android. OS. build; + import android. view. display; public class MainActivity extends Activity {pub Lic Button Btn_Music, Btn_Movie, Btn_DVD, btn_cmb; public TextView src_title; private static final boolean DEBUG = true; private int all_src, cur_src; + private int screen_w, screen_h; public static SerialService localService = null; @-182,6 + 187,8 @ public class MainActivity extends Activity {setContentView (R. layout. src_main); + getDisplayScreenResolution (); + connected = 0; connect_retry_cn T = 0; mConnectCheckTask = new TimerTask () {@-303,6 + 310,47 @ public class MainActivity extends Activity {}}+ public int getDisplayScreenResolution () + {+ int ver = Build. VERSION. SDK_INT; ++ DisplayMetrics dm = new DisplayMetrics (); + android. view. display display = getWindowManager (). getdefadisplay display (); + display. getMetrics (dm); ++ screen_w = dm. widthPixels; ++ Log. d (TAG, "Run1 first ge T resolution: "+ dm. widthPixels + "*" + dm. heightPixels + ", ver" + ver); + if (ver <13) + {+ screen_h = dm. heightPixels; ++ else if (ver = 13) + {+ try {+ Method mt = display. getClass (). getMethod ("getRealHeight"); + screen_h = (Integer) mt. invoke (display); ++} catch (Exception e) {+ e. printStackTrace (); ++ }}+ else if (ver> 13) + {+ try {+ Method mt = display. getClass (). getMethod ("getRawH Eight "); + screen_h = (Integer) mt. invoke (display); ++} catch (Exception e) {+ e. printStackTrace (); ++ }++ Log. d (TAG, "Run2 Calibration resolution:" + screen_w + "*" + screen_h); ++ return 0; ++ void HideSrcAll () {// src_title.setVisibility (View. GONE); Btn_Music.setVisibility (View. GONE); run the following print: [plain] 01-01 09:37:40. 989 I/ActivityManager (174): Start proc com. jeavox. serial for activity Com. jeavox. serial /. mainActivity: pid = 807 uid = 10036 gids = {} 01-01 09:37:41. 059 D/OpenGLRenderer (361): Flushing caches (mode 1) 01-01 09:37:41. 189 D/OpenGLRenderer (361): Flushing caches (mode 0) 01-01 09:37:41. 209 D/MainActivity (807): Run1 first get resolution: 1280*740, ver 15 01-01 09:37:41. 209 D/MainActivity (807): Run2 Calibration resolution: 1280*800 01-01 09:37:41. 279 D/ViewRootImpl (807 ): Pckname = com. jeavox. serial 01-01 09:37:41. 299 D/ViewRootImpl (807): pckname = com. jeavox. serial 01-01 09:37:41. 319 E/SerialService (807 ): ####################- >>>>> service create <before android4.0, display. getMetrics (dm); can get the correct screen resolution, 4.0, 4.1 won't work. Therefore, in Run1 first get resolution: 1280*740, the correct resolution can be obtained after the new getRawHeight method is used. Run2 Calibration resolution: 1280*800. In the Setting apk provided by Android SDK, ScreenAdaption. the interface for obtaining screen resolution is also available in java: The main code is similar to the following: [java] WindowManager wm = (WindowManager) getSystemService (WINDOW_SERVICE); android. view. display display = wm. getdefadisplay display (); int width = display. getRawWidth (); int height = display. getRawHeight (); by using the interface provided by the author, devices of different android versions can be correctly obtained to obtain the correct screen resolution, which is not very difficult and easy to use, in fact, they are all ready-made android products, but it is very good for those who do not know about it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.