Android 通過一個app 啟動另一個app
阿新 • • 發佈:2019-01-23
PackageManager packageManager = getPackageManager(); String packname="";//此處為包名 if (checkPackInfo(packname)) {//檢查是否有要開啟的app Intent intent = packageManager.getLaunchIntentForPackage(packname); startActivity(intent); } else { launchAppDetail(packname,"");//跳轉到應用市場 }
/** * 檢查包是否存在 * *@param packname * @return */ private boolean checkPackInfo(String packname) { PackageInfo packageInfo = null; try { packageInfo = getPackageManager().getPackageInfo(packname, 0); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return packageInfo != null; }
/** * 啟動到應用商店app詳情介面 * * @param appPkg 目標App的包名 * @param marketPkg 應用商店包名 ,如果為""則由系統彈出應用商店列表供使用者選擇,否則調轉到目標市場的應用詳情介面,某些應用商店可能會失敗 */ public void launchAppDetail(String appPkg, String marketPkg) { try { if (TextUtils.isEmpty(appPkg)) return; Uri uri = Uri.parse("market://details?id=" + appPkg); Intent intent = new Intent(Intent.ACTION_VIEW, uri); if (!TextUtils.isEmpty(marketPkg)) { intent.setPackage(marketPkg); } intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); } catch (Exception e) { e.printStackTrace(); } } }
另外附加以為大神的關於該功能的文章地址:http://www.jianshu.com/p/42ae7066f8f3