我的代码应该检查是否安装了特定的应用程序:
是 - 启动它
否 - 启动Play商店并搜索应用
public void checkXposedInstaller() {
String packageName = "de.robv.android.xposed.installer";
//check if app is installed
try {
PackageManager manager = getPackageManager();
Intent i = manager.getLaunchIntentForPackage(packageName);
if (i == null)
throw new PackageManager.NameNotFoundException();
i.addCategory(Intent.CATEGORY_LAUNCHER);
checkXposedFramework();
} catch (PackageManager.NameNotFoundException e) {
Toast.makeText(this, "app not found", Toast.LENGTH_LONG).show(); //*** download and root install apk
// search on browser/market
try {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + packageName));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
} catch (android.content.ActivityNotFoundException e1) {
Toast.makeText(this, "no app found to handle request", Toast.LENGTH_LONG).show();
}
}
}
但它没有做到这一点。然而,奇怪的是,当我将包名称与com.google.app
之类的东西交换时,它就像一个魅力!我在这做错了什么?
PS: try块什么都不做,没有崩溃,没有抓到日志
答案 0 :(得分:0)
试试这个:
public static void launchPackageOrGotoStore(Context context, String packageName) {
try {
Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
context.startActivity(intent);
} catch (ActivityNotFoundException | NullPointerException ex) {
try {
context.startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("market://details?id=" + packageName))
);
} catch (ActivityNotFoundException e) {
context.startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("http://play.google.com/store/apps/details?id=" + packageName)));
}
}
}
用法:
launchPackageOrGotoStore(context, "com.facebook.katana");
尝试启动程序包名称的Intent
并捕获ActivityNotFoundException
。然后首先启动Play商店应用,如果应用和Play商店应用都不存在,最后一次尝试是启动游戏商店网站。
答案 1 :(得分:0)
检查以下代码,您可以检查该应用是否安装,如果没有,那么您可以重定向到Playstore:
boolean isAppInstalled = appInstalledOrNot("de.robv.android.xposed.installer");
if (isAppInstalled) {
//This intent will help you to launch if the package is already installed
Intent LaunchIntent = getPackageManager()
.getLaunchIntentForPackage("de.robv.android.xposed.installer");
startActivity(LaunchIntent);
} else {
// Do whatever we want to do if application not installed
// For example, Redirect to play store
final String appPackageName = "de.robv.android.xposed.installer"; // getPackageName() from Context or Activity object
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
} catch (android.content.ActivityNotFoundException anfe) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + appPackageName)));
}
}
功能检查应用程序是否安装:
private boolean appInstalledOrNot(String uri) {
PackageManager pm = getPackageManager();
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
return true;
} catch (PackageManager.NameNotFoundException e) {
}
return false;
}