How to launch or install app B in app a in Android

Source: Internet
Author: User

See other people to do the introduction, think of their own introduction also added new features, that is, how to launch or install app B in app A in Android, check out some information.

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.

/**
* Determine if the target app is installed
* @param packagename target app installed package name
* @return If the target app is installed
* @author Zuolongsnail
*/
Private Boolean Isinstallbyread (String packagename) {
return new File ("/data/data/" + PackageName). exists ();
}

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

/**
* First to determine whether the installation, installed, start the target application, or first install
* @param packagename target app installed package name
* @param appPath Target app apk installation file is in the same path
* @author Zuolongsnail
*/
private void Launchapp (String packagename, String appPath) {
Launch target App
if (Isinstallbyread (PackageName)) {
Get the intent of the target app installation package
Intent Intent = Getpackagemanager (). Getlaunchintentforpackage (
PackageName);
StartActivity (Intent);
}
Install target App
else {
Intent Intent = new Intent ();
Set the target app installation package path
Intent.setdataandtype (Uri.fromfile new File (AppPath)),
"Application/vnd.android.package-archive");
StartActivity (Intent);
}
}

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.

/** determine whether to install or overwrite the type of installation */
private static final int notinstall = 0; Not installed
private static final int installed = 1; Is installed and is a new version
private static final int oldversion = 2; Installed but older version
/**
* Determine if the app is installed or is the latest version
* @param packagename target app installed package name
* @param versioncode The specified app version number
* @return Type of installation
* @author Zuolongsnail
*/
private int Isinstallbyread (String packagename, int versioncode) {
Determine if the installation
if (New File ("/data/data/" + PackageName). Exists ()) {
Gets the collection of all app package names installed on the system
list<packageinfo> packages = Getpackagemanager (). getinstalledpackages (0);
for (int i=0;i<packages.size (); i++) {
PackageInfo PackageInfo = Packages.get (i);
Identify the specified application
if (Packagename.equals (Packageinfo.packagename)) {
if (Packageinfo.versioncode >= versioncode) {
return installed;
}else{
return oldversion;
}
}
}
}
return notinstall;
}

The Versioncode in the above program is configured in the manifest tag in the Manifest.xml file, indicating the version number of the app.


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.