Android basic Tools class method and%s magical

Source: Internet
Author: User

1. Get phone resolution Public static string getdisplaymetrix (Context context)  {     if  (constant.screen.screen_width == 0 | |  constant.screen.screen_height == 0)  {        if   (Context != null)  {             int width = 0;            int  height = 0;             Sharedpreferences diaplaymetrixinfo = context.getsharedpreferences ("Display_metrix_info",  0);            if  (context instanceof  activity)  {                 WindowManager windowManager =  (Activity)  contexT). Getwindowmanager ();                 display display = windowmanager.getdefaultdisplay ();                 DisplayMetrics dm = new  Displaymetrics ();                 display.getmetrics (DM);                 width = dm.widthPixels;                 height = dm.heightPixels;                 Editor editor =  Diaplaymetrixinfo.edit ();                 editor.putint ("width",  widTH);                 Editor.putint ("height",  height);                 editor.commit ();             } else {                 width = diaplaymetrixinfo.getint ("width",  0);                 height = diaplaymetrixinfo.getint (" Height ",  0);            }             Constant.Screen.SCREEN_WIDTH = width;             Constant.Screen.SCREEN_HEIGHT =  Height;        }    }    return constant.screen.screen_width  +  "X"  + constant.screen.screen_height;} 2. Turn off the system's soft keyboard Public static void dismisssoftkeyboard (activity activity)  {     view view = activity.getwindow (). Peekdecorview ();    if  ( View != null)  {        InputMethodManager  inputmanger =  (Inputmethodmanager)  activity.getsystemservice (Context.input_method_service);         inputmanger.hidesoftinputfromwindow (View.getWindowToken (),  0),     }}3, dp-px Mutual conversion public static int dp2px (Context context,  float dpvalue)  {    final float scale =  Context.getresources (). Getdisplaymetrics () .density;    return  (int)   (DpValue * scale + 0.5f);} PUBLIC&NBSP;STATIC&NBSP;INT&NBSP;PX2DP (Context context, float pxvalue)  {     final float scale = context.getresources (). Getdisplaymetrics () .density;     return  (int)   (pxvalue / scale + 0.5f);} 4, get device unique encoding/** *  based on MAC address +deviceid *  get device unique encoding  * *  @return  */public  Static string getdevicekey ()  {    if  ("". Equals (Devicekey))  {         String macAddress =  "";         WifiManager wifiMgr =  (Wifimanager)  mainapplication.getins (). Getsystemservice (Mainapplication.wifi_service);        wifiinfo  info =  (Null == wifimgr ? null : wifimgr.getconnectioninfo ());         if  (Null != info)  {             macaddress = info.getmacaddress ();         }        telephonymanager telephonymanager =                  ( Telephonymanager)  mainapplication.getins (). Getsystemservice (Mainapplication.telephony_service);         string deviceid = telephonymanager.getdeviceid (); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;DEVICEKEY&NBSP;=&NBSP;MD5UTIL.TOMD5 ("Android"  +  Constant.appkey + constant.apppwd + macaddress + deviceid);     }    return devicekey;} 5, access to mobile phone and SIM card related information Public static map<string, string> getphoneinfo (CONTEXT&NBSp;context)  {    map<string, string> map = new hashmap <String, String> (;    telephonymanager tm = ) (Telephonymanager)  context            .getsystemservice ( Context.telephony_service);     string imei = tm.getdeviceid ();     string imsi = tm.getsubscriberid ();     string phonemode  = android.os.Build.MODEL;    String phoneSDk =  Android.os.build.version.release;    map.put ("IMEI",  imei);     Map.put ("Imsi",  imsi);     map.put ("Phonemode", phonemode +  "# #"  + &NBSP;PHONESDK)     map.put ("model",  phonemode);     map.put ("SDK" , &NBSP;PHONESDK);    Return map;} 6, Installation Apkpublic void install (context context, string filename)  {     if  (Textutils.isempty (fileName)  | |  context == null)  {        return;     }    try {        Intent  Intent = new intent (Intent.action_view);         Intent.setflags (Intent.flag_activity_new_task);         Intent.setaction (Android.content.Intent.ACTION_VIEW);         Intent.setdataandtype (Uri.fromfile (New file (fileName)),  "application/vnd.android.package-archive");         context.startactivity (Intent);    }  catch  (exception e)  {        e.printstAcktrace ();     }}public void install (Context context, file file)  {    try {        intent intent  = new intent (Intent.action_view);         Intent.setdataandtype (uri.fromfile (file),  "application/vnd.android.package-archive");         context.startactivity (Intent);    } catch  ( exception e)  {        e.printstacktrace ();    The magic of "%s" in  }}7, Strings.xml add string <string name= "text" in Strings.xml >Hello,%s!</string> Textview.settext in Code (String.Format (Getresources () getString (R.string.text), "Android"); Output: hello,android!


Android basic Tools class method and%s magical

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.