如果没有安装在设备中,如何在浏览器中启动谷歌播放?

时间:2016-11-09 05:34:20

标签: android

我有一个应用程序,我想将用户重定向到Play商店中的应用程序链接工作正常,但如果没有在设备中安装Play商店,那么我必须将用户重定向到浏览器中的应用程序链接。如何存档这个问题。

代码: -

private void startNewActivity(Context context, String packageName) {
    Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
    if (intent == null) {
        // Bring user to the market or let them choose an app?
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("market://details?id=" + packageName));
        Log.e(TAG,"Package name::"+packageName);
    }

    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);
}

并在onclick中调用此方法

startNewActivity(mContext, "market://details?id=com.example.something"));

2 个答案:

答案 0 :(得分:1)

private void startNewActivity(String packageName) {
    try { 
      Intent intent = new Intent(Intent.ACTION_VIEW);
      intent.setData(Uri.parse("market://details?id=packageName"));
      startActivity(intent);
    } catch (Exception e) { //google play app is not installed
      Intent intent = new Intent(Intent.ACTION_VIEW);
      intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=packageName"));
      startActivity(intent);
    }
}

或者你可以尝试这样。

下面提到检查是否安装了应用。

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

现在检查并使用此类替代品。

boolean installed = appInstalledOrNot("Your_Package_Name");  
        if(installed) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
      intent.setData(Uri.parse("market://details?id=packageName"));
      startActivity(intent);

            System.out.println("App is already installed on your phone");         
        } else {
Intent intent = new Intent(Intent.ACTION_VIEW);
      intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=packageName"));
      startActivity(intent);
            System.out.println("App is not currently installed on your phone");
        }

答案 1 :(得分:0)

这是我的功能......只需复制并粘贴......传递您需要的包作为参数

 public Intent getShareIntent(String applicationName, @Nullable Uri localImageUri, @Nullable Uri contentToShare, String textToShare) {
    Intent intent = new Intent(Intent.ACTION_SEND);
    boolean haveTheApp = false;
    if(localImageUri != null){
        intent.setType("image/*");
    }else{
        intent.setType("text/plain");
    }
    List<ResolveInfo> matches = context.getPackageManager().queryIntentActivities(intent, 0);
    for (ResolveInfo info : matches) {
        if (info.activityInfo.packageName.toLowerCase().contains(applicationName)) {
            intent.setPackage(info.activityInfo.packageName);
            intent.setClassName(info.activityInfo.packageName, info.activityInfo.name);
            if(contentToShare != null){
                intent.putExtra(Intent.EXTRA_TEXT, textToShare + ": " + contentToShare.toString());
            }else{
                intent.putExtra(Intent.EXTRA_TEXT, textToShare + " #App");
            }
            if(localImageUri != null){
               intent.putExtra(Intent.EXTRA_STREAM, localImageUri);
           }
            haveTheApp = true;
            break;
        }
    }
    if(!haveTheApp){
        intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=" + applicationName));
    }
    return intent;
}