如何打开亚马逊'以编程方式在Playstore上的app?

时间:2017-09-07 00:55:29

标签: android android-intent android-package-managers

我试图以编程方式打开亚马逊应用程序,如果手机中找不到该应用程序,则要求用户从游戏商店安装它,但问题是当我试图打开它时Playstore,显示Item not found错误。

这是我的代码:

public void openApp(final String packageName, final String appName) {
        Intent intent = getBaseContext().getPackageManager().getLaunchIntentForPackage(packageName);
         if (intent != null) {
             startActivity(intent);
         } else {
             AlertDialog.Builder builder;
             builder = new AlertDialog.Builder(MainActivity.this);
             builder.setMessage(appName + " not found. Would you like to install " + appName + " from play store?")
                     .setPositiveButton("YES", new DialogInterface.OnClickListener() {
                         public void onClick(DialogInterface dialog, int which) {
                             try {
                                 startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + packageName)));
                             } catch (android.content.ActivityNotFoundException anfe) {
                                 startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + packageName)));
                             }
                         }
                     })
                     .setNegativeButton("NO", new DialogInterface.OnClickListener() {
                         public void onClick(DialogInterface dialog, int which) {

                         }
                     })
                     .show();
         }
    }

其中packageNamein.amazon.mShop.android.shoppingappNameAmazon

我从app的Playstore链接获得了包名,即https://play.google.com/store/apps/details?id=in.amazon.mShop.android.shopping&hl=en

这里出了什么问题,如果在设备上找不到,我怎样才能在Playstore上成功打开亚马逊应用程序?

1 个答案:

答案 0 :(得分:0)

 private boolean appInstalledOrNot(String uri) {
        PackageManager pm = getPackageManager();
        try {
            pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
        }

        return false;
    }

使用此方法查找是否已安装应用,然后

boolean isAppInstalled = appInstalledOrNot(" com.check.application");

    if(isAppInstalled) {
        //This intent will help you to launch if the package is already installed
        Intent LaunchIntent = getPackageManager()
            .getLaunchIntentForPackage("com.check.application");
        startActivity(LaunchIntent);

        Log.i("Application is already installed.");       
    } else {
        // Do whatever we want to do if application not installed
        // For example, Redirect to play store

        Log.i("Application is not currently installed.");
    }