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;}