Common Code blocks and codes

Source: Internet
Author: User

Common Code blocks and codes

1. Obtain the system version:

PackageInfo info = this.getPackageManager().getPackageInfo(this.getPackageName(), 0);int versionCode=nfo.versionCodestring versionName=info.versionName

2. Obtain system information:

String archiveFilePath = "sdcard/download/Law.apk"; // installation PackageManager pm = getPackageManager (); PackageInfo info = pm. Equals (archiveFilePath, PackageManager. GET_ACTIVITIES); if (info! = Null) {ApplicationInfo appInfo = info. applicationInfo; String appName = pm. getApplicationLabel (appInfo ). toString (); String packageName = appInfo. packageName; // get the installation package name String version = info. versionName; // get the version information Toast. makeText (test4.this, "packageName:" + packageName + "; version:" + version, Toast. LENGTH_LONG ). show (); Drawable icon = pm. getApplicationIcon (appInfo); // get the icon information TextView TV = (TextView) findViewById (R. id. TV); // display the icon TV. setBackgroundDrawable (icon );

3. Obtain the installation path and list of installed programs

(1) Get the current program path getApplicationContext () in android (). getFilesDir (). getAbsolutePath () (2) android obtains the List of installed programs <PackageInfo> packageInfoList = getPackageManager (). getInstalledPackages (0

4. Image Retrieval, application name, and package name

PackageManager pManager = MessageSendActivity.this.getPackageManager();  List<PackageInfo> appList = Utils.getAllApps(MessageSendActivity.this);       for(int i=0;i<appList.size();i++) {          PackageInfo pinfo = appList.get(i);           ShareItemInfo shareItem = new ShareItemInfo();           //set Icon            shareItem.setIcon(pManager.getApplicationIcon(pinfo.applicationInfo));

5. solve the problem that the Item itself cannot be clicked when there is a button on the item on the listview:

1. Add the code android: descendantFocusability = "blocksDescendants" 2. Add the code android: focusable = "true" to the listview"

6. Do not enter Chinese characters in the text box:

Android: digits = "1234567890qwertyuiopasdfghjklzxcvbnm '-= [] \;,./~! @ # $ % ^ * () _ + }{:? & <> "'" Does not input Chinese characters.

7. Obtain the screen width and height

DisplayMetrics displayMetrics = new DisplayMetrics();  this.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);  int height = displayMetrics.heightPixels;  int width = displayMetrics.widthPixels;

8. Obtain the device model, SDK version, and system version.

String device_model = Build. MODEL; // device MODEL String version_sdk = Build. VERSION. SDK; // device sdk version String version_release = Build. VERSION. RELEASE; // system VERSION of the device

9. obtain all the activities under the application.

public static ArrayList<String> getActivities(Context ctx) {     ArrayList<String> result = new ArrayList<String>();     Intent intent = new Intent(Intent.ACTION_MAIN, null);     intent.setPackage(ctx.getPackageName());for (ResolveInfo info : ctx.getPackageManager().queryIntentActivities(intent, 0)) {     result.add(info.activityInfo.name);     }     return result;}

  

  

  

  

  

  

  

  

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.