Java tutorial
//package com.java2s; /* * Copyright (C) 2016 jarlen * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ import android.content.Context; import android.content.Intent; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.text.TextUtils; public class Main { /** * start app * * @param ctx * @param packageName */ public static void startApp(Context ctx, String packageName) { if (!isAppInstalled(ctx, packageName)) { return; } PackageManager packageManager = ctx.getPackageManager(); Intent intent = packageManager.getLaunchIntentForPackage(packageName); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_CLEAR_TOP); ctx.startActivity(intent); } /** * check if installed * @param context * @param packageName * @return */ public static boolean isAppInstalled(Context context, String packageName) { if (TextUtils.isEmpty(packageName)) { return false; } PackageManager packageManager = context.getPackageManager(); ApplicationInfo applicationInfo = null; try { applicationInfo = packageManager.getApplicationInfo(packageName, 0); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return applicationInfo != null; } /** * get info of app * * @param context * @param packageName * @return */ public static ApplicationInfo getApplicationInfo(Context context, String packageName) { PackageManager pm = context.getPackageManager(); ApplicationInfo appInfo = null; try { appInfo = pm.getApplicationInfo(packageName, 0); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return appInfo; } }