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>