Android installs and launches another application

Source: Internet
Author: User

Sometimes an app needs to launch another app to get things done, like turning on alarms, cameras, calendars, and so on in some apps.

How to start or install the appropriate application:

STEP1: Determine if the target app is installed. As long as you know the installation package name of the target application can be achieved by judging whether "/data/data/packagename" exists.

[Java]View Plaincopy
  1. /** .
  2. 02. * Determine if the target application is installed
  3. * @param packagename target app installed package name
  4. * @return If the target app is installed
  5. * @author Zuolongsnail
  6. 06. */
  7. .Private Boolean isinstallbyread (String packagename) {
  8. . return new File ("/data/data/" + PackageName). exists ();
  9. 09.}


STEP2: The target app is installed and starts, otherwise it is installed first.

[Java]View Plaincopy
  1. /** .
  2. 02. * First to determine whether to install, installed to start the target application, or first install
  3. * @param packagename target app installed package name
  4. * @param appPath Target app apk installation file is in the same path
  5. * @author Zuolongsnail
  6. 06. */
  7. .private void Launchapp (String packagename, String appPath) {
  8. . //Start target application
  9. The. if (Isinstallbyread (PackageName)) {
  10. Intent//Get the target app install package
  11. Intent Intent = Getpackagemanager (). Getlaunchintentforpackage (
  12. PackageName);
  13. StartActivity (Intent);
  14. 14.}
  15. Installation Target Application
  16. . else {
  17. Intent Intent = new Intent ();
  18. //Set the target app installation package path
  19. Intent.setdataandtype (uri.fromfilenew File (AppPath)),
  20. "Application/vnd.android.package-archive");
  21. StartActivity (Intent);
  22. 22.}
  23. 23.}

Determine if the app is installed or is the latest version

Sometimes we need to check whether the app is installed when we install it, install it without it, and overwrite the installation with the old version.

[Java]View Plaincopy
    1. /** decide whether to install or overwrite the type of installation */
    2. Theprivate static final int notinstall = 0; //Not installed
    3. Theprivate static final int installed = 1; //installed and new version
    4. private static final int oldversion = 2; //Installed but older version
    5. /** .
    6. 06. * Determine if the application is installed or is the latest version
    7. . * @param packagename target app installed package name
    8. . * @param versioncode The specified app version number
    9. *. * @return Type of installation
    10. * @author Zuolongsnail
    11. 11. */
    12. private int Isinstallbyread (String packagename, int versioncode) {
    13. //Determine if the installation
    14. if (new File ("/data/data/" + PackageName). Exists ()) {
    15. //Get a collection of all app package names installed in the system
    16. list<packageinfo> packages = Getpackagemanager (). Getinstalledpackages (0);
    17. . for (int i=0;i<packages.size (); i++) {
    18. PackageInfo PackageInfo = Packages.get (i);
    19. //Identify the specified application
    20. if (packagename.equals (Packageinfo.packagename)) {
    21. if (Packageinfo.versioncode >= versioncode) {
    22. - return installed;
    23. .}else{
    24. . return oldversion;
    25. 25.}
    26. 26.}
    27. 27.}
    28. 28.}
    29. . return Notinstall;
    30. 30.}

http://blog.csdn.net/gf771115/article/details/6637693

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.