我有一个应用程序...在我的应用程序中点击按钮我需要检查手机中是否安装了特定的应用程序。如果没有安装应用程序,它应该重定向到Play商店。
我该怎么做才能帮忙?
public class Example extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Put the package name here...
boolean installed = appInstalledOrNot("com.Ch.Example.pack");
if(installed) {
//This intent will help you to launch if the package is already installed
Intent LaunchIntent = getPackageManager()
.getLaunchIntentForPackage("com.Ch.Example.pack");
startActivity(LaunchIntent);
System.out.println("App already installed on your phone");
}
else {
System.out.println("App is not installed on your phone");
}
}
private boolean appInstalledOrNot(String uri) {
PackageManager pm = getPackageManager();
boolean app_installed = false;
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
app_installed = true;
}
catch (PackageManager.NameNotFoundException e) {
app_installed = false;
}
return app_installed ;
}
}
以上示例是否适用于检查应用程序是否已安装我的应用程序。
答案 0 :(得分:2)
要检查设备中是否安装了应用,请使用以下方法和参数
GooglePlayStorePackageNameOld
& GooglePlayStorePackageNameNew
都包含应用的包名称
private static final String GooglePlayStorePackageNameOld = "com.google.market";
private static final String GooglePlayStorePackageNameNew = "com.android.vending";
相应地使用和修改以下方法以检查应用程序当前是否已安装在设备中
public boolean isAppInstalled() {
PackageManager packageManager = getApplication().getPackageManager();
List<PackageInfo> packages = packageManager
.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);
for (PackageInfo packageInfo : packages) {
if (packageInfo.packageName.equals(GooglePlayStorePackageNameOld)
|| packageInfo.packageName
.equals(GooglePlayStorePackageNameNew)) {
googlePlayStoreInstalled = true;
break;
} else {
googlePlayStoreInstalled = false;
}
}
return googlePlayStoreInstalled;
}
并将用户导航到特定的Play商店链接,请使用以下代码
final String appPackageName = getPackageName(); // 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)));
}
修改强> 根据你的代码
Intent LaunchIntent = getPackageManager()
.getLaunchIntentForPackage("com.Ch.Example.pack");
startActivity(LaunchIntent);
将启动包名为com.Ch.Example.pack
的应用答案 1 :(得分:0)
检查应用程序是否已安装的方式是标准方式。要重定向到playStore,您可以使用以下代码段
private goToPlayStore(String packageName) {
Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + packageName))
startActivity(intent);
}
对于评论,您应首先检查市场是否已安装,包名称应为 com.android.vending 。如果市场不可用,此代码将抛出异常。