Android Get Screen dimensions from Service

后端 未结 4 2054
Happy的楠姐
Happy的楠姐 2021-02-02 08:35

I got the Screen Size by using the following from a activity,

Display display = getWindowManager().getDefaultDisplay();

But the same thing is n

相关标签:
4条回答
  • 2021-02-02 08:41

    It worked for me!

    DisplayMetrics metrics = getApplicationContext().getResources().getDisplayMetrics();
    int width = metrics.widthPixels;
    int height = metrics.heightPixels;
    

    You only needed to get the context.

    0 讨论(0)
  • 2021-02-02 08:43

    Try this:

    WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
    
    DisplayMetrics displayMetrics = new DisplayMetrics();
    
    wm.getDefaultDisplay().getMetrics(displayMetrics);
    
    int height = displayMetrics.heightPixels;
    int width = displayMetrics.widthPixels;
    
    0 讨论(0)
  • 2021-02-02 08:47

    If you are starting the service on boot (or automatically any other way) then shared prefs is a good way. However, if the only way you start the service is thru calling Context.startService() then it actually makes more sense to just place an extra in the Intent that you use to start the service.

    0 讨论(0)
  • 2021-02-02 08:51

    there totally is a way to obtain it from a service:

        WindowManager window = (WindowManager) getSystemService(Context.WINDOW_SERVICE); 
        Display display = window.getDefaultDisplay();
        int width = display.getWidth();
        int height = display.getHeight();
    
    0 讨论(0)
提交回复
热议问题