Android Development Useful Code accumulation

Source: Internet
Author: User

Android Development Requirements Change fast, the development cycle requirements as short as possible, the next series of articles from the actual use to summarize some of the commonly used code snippets, easy to find, but also provide a reference for later people.

1. Get basic information about manifest (Upgrade page and software about page is usually used)

Context Mcontext = Xxxapplication.getinstance (). Getapplicationcontext (); Get application context, of course, can also get the current activity of the context, application is generally a singleton PackageName = Mcontext.getpackagename (); Gets the package name, which is the value of the packages option in manifest PackageInfo info = Mcontext.getpackagemanager (). Getpackageinfo (                Mcontext.getpackagename (), 0); //

String versionname = info.versionname;
int versioncode = Info.versioncode;

2. Get Phone screen parameters (important for screen fit, after all, there are too many screen types for Android phones)

Resources resources = Xxxapplication.getinstance (). Getresources (); float scale = Resources.getdisplaymetrics (). density;//screen density factor for converting between PX and dp float scaleddensity = resources.getdisplaymetrics (). scaleddensity;float Displaywidth = Resources.getdisplaymetrics (). Widthpixels;float displayheight = Resources.getdisplaymetrics (). HeightPixels;

public static int dip2px (float dp) {
return (int) (DP * scale + 0.5f);
}

public static int Px2dip (float px) {
return (int) (Px/scale + 0.5f);
}

After a special article to introduce me to the different screen adaptation of the experience, today, first introduce the above code.

3. Hide the soft keyboard

Inputmethodmanager IMM = (inputmethodmanager) getsystemservice (Input_method_service);    

In the page containing edittext, it is possible to enter the activity on the soft keyboard to bounce out, you can use the following methods to prohibit

GetWindow (). Setsoftinputmode (WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

  

4. Install apk (for example, when you download the upgrade software, you need to install and replace the old version)

File Apkfile = new file (Apkfilepath); if (!apkfile.exists ()) {            return;}  Intent i = new Intent (Intent.action_view); I.addflags (Intent.flag_activity_new_task); I.setdataandtype (Uri.parse ("file://" + apkfile.tostring ()), "application/vnd.android.package-archive");         Mcontext.startactivity (i);

  

Android Development Useful Code accumulation

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.