Android cainiao notes-get the uninstalled APK icon, version, package name, name, whether to install, install, and open

Source: Internet
Author: User

I had nothing to do over the weekend. I took out the basic knowledge of Android and reviewed it. Today's topic is "getting uninstalled APK icons, versions, package names, names, whether to install, jump to install, open".

1. Get the APK icon

You can use getApplicationIcon (ApplicationInfo) in PackageManager to obtain a drawable. However, in actual use, you can only get one default icon, which is not an APK icon at all.

Refer to Xiaomi Open Source File Manager and use it in combination with practices. The Code is as follows:

/** A new method is used to obtain the APK icon. The previous failure is caused by a BUG in android. * appInfo is used. publicSourceDir = apkPath; To fix this problem, see: * http://code.google.com/p/android/issues/detail? Id = 9151 */public static Drawable getApkIcon (Context context, String apkPath) {PackageManager pm = context. getPackageManager (); PackageInfo = pm. getPackageArchiveInfo (apkPath, PackageManager. GET_ACTIVITIES); if (info! = Null) {ApplicationInfo appInfo = info. applicationInfo; appInfo. sourceDir = apkPath; appInfo. publicSourceDir = apkPath; try {return appInfo. loadIcon (pm);} catch (OutOfMemoryError e) {Log. e ("ApkIconLoader", e. toString () ;}} return null ;}

In the following code segment, PackageManager, PackageInfo, and ApplicationInfo are all the same as above.
2. Get the APK name

String label = appInfo.loadLabel(mPackManager).toString();


3. Get the APK package name
String packageName = appInfo.packageName;


4. Obtain the APK version

String version = info.versionName==null?"0":info.versionName

5. Determine whether the APK is installed

private boolean isApkInstalled(String packagename)  {    PackageManager localPackageManager = getPackageManager();    try    {      PackageInfo localPackageInfo = localPackageManager.getPackageInfo(packagename, PackageManager.GET_UNINSTALLED_PACKAGES);      return true;    }    catch (PackageManager.NameNotFoundException localNameNotFoundException)    {     return false;    }      }


6. Install APK

 private void installAPK(String apkPath) { Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setDataAndType(Uri.parse("file://" + apkPath),"application/vnd.android.package-archive"); mContext.startActivity(intent); }

7. Open APK

</pre><pre name="code" class="java"> private void openAPK(String packagename) {   PackageManager packageManager = mContext.getPackageManager();    Intent intent=new Intent();    intent =packageManager.getLaunchIntentForPackage(packagename);    mContext.startActivity(intent); }

Ps. The Silent Installation of APK is still under study...



Related Article

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.