First look:
It is actually an application of the displaymetrics class. The Code is as follows:
Package YC. Android. Resolution;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. util. displaymetrics;
Import Android. View. view;
Import Android. widget. Button;
Import Android. widget. textview;
Public class theresolutionactivity extends activity {
Private textview TV;
Private button BTN;
// Obtains the cell phone screen resolution.
Private displaymetrics DM;
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
// Obtain the textview and button objects in the layout.
TV = (textview) findviewbyid (R. Id. TV );
BTN = (button) findviewbyid (R. Id. btnok );
// Add a button Event Response
BTN. setonclicklistener (New View. onclicklistener (){
Public void onclick (view v ){
Dm = new displaymetrics ();
Getwindowmanager (). getdefaultdisplay (). getmetrics (DM );
// Obtain the phone width and height in pixels.
String strpm = "cell phone screen resolution:" + DM. widthpixels + "*"
+ DM. heightpixels;
TV. settext (strpm );
}
});
}
}
You can also do this:
Package YC. Android. Resolution;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. View. display;
Import Android. View. view;
Import Android. View. windowmanager;
Import Android. widget. Button;
Import Android. widget. textview;
Public class theresolutionactivity extends activity {
Private textview TV;
Private button BTN;
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
// Obtain the textview and button objects in the layout.
TV = (textview) findviewbyid (R. Id. TV );
BTN = (button) findviewbyid (R. Id. btnok );
// Add a button Event Response
BTN. setonclicklistener (New View. onclicklistener (){
Public void onclick (view v ){
Windowmanager = getwindowmanager ();
Display dm = windowmanager. getdefaultdisplay ();
// Obtain the phone width and height in pixels.
String strpm = "cell phone screen resolution:" + DM. getwidth () + "*"
+ DM. getheight ();
TV. settext (strpm );
}
});
}
}
Both methods can obtain the resolution of the Android mobile phone screen.