Android apps-package install uninstall

Source: Internet
Author: User

1: The package name obtains the environment variables of the package.

You can obtain the resource of the corresponding package based on this variable.

Context authcontext = createpackagecontext (DESC. packagename, 0 );
Icon = authcontext. getresources (). getdrawable (DESC. iconid );

Label = authcontext. getresources (). gettext (DESC. labelid );

 

2: Get app Information Based on APK URI

/*
* Utility method to get application information for a given packageuri
*/
Public static applicationinfo getapplicationinfo (URI packageuri ){
Final string archivefilepath = packageuri. getpath ();
Packageparser = new packageparser (archivefilepath );
File sourcefile = new file (archivefilepath );
Displaymetrics metrics = new displaymetrics ();
Metrics. settodefaults ();
Packageparser. Package PKG = packageparser. parsepackage (sourcefile, archivefilepath, metrics, 0 );
If (PKG = NULL ){
Return NULL;
}
Return PKG. applicationinfo;

APK installed

Public static Boolean ispackagealreadyinstalled (activity context, string pkgname ){
List <packageinfo> installedlist = context. getpackagemanager (). getinstalledpackages (
Packagemanager. get_uninstalled_packages );
Int installedlistsize = installedlist. Size ();
For (INT I = 0; I <installedlistsize; I ++ ){
Packageinfo TMP = installedlist. Get (I );
If (pkgname. inclusignorecase (TMP. packagename )){
Return true;
}
}
Return false;
}

Get app icon and name

Public static view initsnippetforinstalledapp (activity pcontext,
Applicationinfo appinfo, int snippetid ){
View topology nippet = pcontext. findviewbyid (snippetid );
String pkgname = appinfo. packagename;
Packagemanager PM = pcontext. getpackagemanager ();
Charsequence label = appinfo. loadlabel (PM );
Drawable icon = appinfo. loadicon (PM );
(Imageview) using nippet. findviewbyid (R. Id. app_icon). setimagedrawable (icon );
(Textview) extends nippet. findviewbyid (R. Id. app_name). settext (Label );
Return response nippet;
}

 

Install app code

Packagemanager PM = getpackagemanager ();

Installflags | = packagemanager. install_replace_existing;

String installerpackagename = getintent (). getstringextra (
Intent. extra_installer_package_name );
Packageinstallobserver observer = new packageinstallobserver ();
PM. installpackage (mpackageuri, observer, installflags, installerpackagename );

Class packageinstallobserver extends ipackageinstallobserver. Stub {
Public void packageinstalled (string packagename, int returncode ){

}
}

Uninstall an app

Packagedeleteobserver observer = new packagedeleteobserver ();
Getpackagemanager (). deletepackage (mappinfo. packagename, observer, 0 );

Class packagedeleteobserver extends ipackagedeleteobserver. Stub {
Public void packagedeleted (Boolean succeeded ){
Message MSG = mhandler. obtainmessage (uninstall_complete );
MSG. arg1 = succeeded? Succeeded: failed;
Mhandler. sendmessage (MSG );
}
}

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.