How can I start or install app B in APP A in Android?

Source: Internet
Author: User

How can I start or install app B in APP A in Android?

When I saw other people's game strategies and thought about how to add new features to my game strategy, I checked some information about how to start or install app B in APP A in Android.

How to start or install an application:

Step 1: Determine whether to install the target application. You only need to know the installation package name of the target application by checking whether "/data/packagename" exists.

/**
* Determine whether to install the target application
* @ Param packageName the package name after the target application is installed
* @ Return whether the target application has been installed
* @ Author zuolongsnil
*/
Private boolean isInstallByread (String packageName ){
Return new File ("/data/" + packageName). exists ();
}

Step 2: If the target application is installed, start the application. Otherwise, install the application first.

/**
* Check whether the application is installed. If the application is installed, start the target application. Otherwise, install the application first.
* @ Param packageName the package name after the target application is installed
* @ Param appPath path of the apk installation file of the target application
* @ Author zuolongsnil
*/
Private void launchApp (String packageName, String appPath ){
// Start the target application
If (isInstallByread (packageName )){
// Obtain the Intent of the installation package of the target application
Intent intent = getPackageManager (). getLaunchIntentForPackage (
PackageName );
StartActivity (intent );
}
// Install the target application
Else {
Intent intent = new Intent ();
// Set the path of the target application installation package
Intent. setDataAndType (Uri. fromFile (new File (appPath )),
"Application/vnd. android. package-archive ");
StartActivity (intent );
}
}

Determine whether the application is installed or whether it is the latest version

When installing an application, we sometimes need to check whether the application is installed. If the application is not installed, the old version overwrites the installation.

/** Determine whether to install or overwrite the installation type */
Private static final int NOTINSTALL = 0; // Not Installed
Private static final int INSTALLED = 1; // INSTALLED and new
Private static final int OLDVERSION = 2; // installed but old
/**
* Determine whether the application is installed or the latest version.
* @ Param packageName the package name after the target application is installed
* @ Param versionCode the specified application version number
* @ Return indicates the installation type.
* @ Author zuolongsnil
*/
Private int isInstallByread (String packageName, int versionCode ){
// Determine whether to install
If (new File ("/data/" + packageName). exists ()){
// Obtain the set of all application packages installed in the system
List <PackageInfo> packages = getPackageManager (). getInstalledPackages (0 );
For (int I = 0; I <packages. size (); I ++ ){
PackageInfo packageInfo = packages. get (I );
// Find 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 application version number.



In android development, how does one Enable User-installed applications?

When you call a user program through intent, you need to know the action, category, and data of the user program. . You can call

How can I enable the service of an android app after it is overwritten and installed?

Use the following intent-filter
<Intent-filter>
<Action android: name = "android. intent. action. PACKAGE_REPLACED"/>

<Data android: scheme = "package"/>
</Intent-filter>
 

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.