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