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
- /** .
- 02. * Determine if the target application is installed
- * @param packagename target app installed package name
- * @return If the target app is installed
- * @author Zuolongsnail
- 06. */
- .Private Boolean isinstallbyread (String packagename) {
- . return new File ("/data/data/" + PackageName). exists ();
- 09.}
STEP2: The target app is installed and starts, otherwise it is installed first.
[Java]View Plaincopy
- /** .
- 02. * First to determine whether to install, installed to 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
- 06. */
- .private void Launchapp (String packagename, String appPath) {
- . //Start target application
- The. if (Isinstallbyread (PackageName)) {
- Intent//Get the target app install package
- Intent Intent = Getpackagemanager (). Getlaunchintentforpackage (
- PackageName);
- StartActivity (Intent);
- 14.}
- Installation Target Application
- . else {
- Intent Intent = new Intent ();
- //Set the target app installation package path
- Intent.setdataandtype (uri.fromfilenew File (AppPath)),
- "Application/vnd.android.package-archive");
- StartActivity (Intent);
- 22.}
- 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
- /** decide whether to install or overwrite the type of installation */
- Theprivate static final int notinstall = 0; //Not installed
- Theprivate static final int installed = 1; //installed and new version
- private static final int oldversion = 2; //Installed but older version
- /** .
- 06. * Determine if the application 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
- 11. */
- private int Isinstallbyread (String packagename, int versioncode) {
- //Determine if the installation
- if (new File ("/data/data/" + PackageName). Exists ()) {
- //Get a collection of all app package names installed in 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;
- 25.}
- 26.}
- 27.}
- 28.}
- . return Notinstall;
- 30.}
http://blog.csdn.net/gf771115/article/details/6637693