当未安装Google+时,应用程序强制关闭。

7

我的需求是在社交网络上分享。我已经在Facebook和Twitter上完成了分享。但是我卡在了Google+身上。我有以下代码可以在Google+上分享,但是当我启动活动时,应用程序会强制关闭。这仅在设备上尚未安装Google+应用程序时发生。我知道此共享意图需要已安装Google+才能启动活动。

现在我需要做的是至少通过对话框或toast告知用户google+分享需要已安装google+应用程序,而不是被强制关闭(如果可能,通过单击对话框上的“确定”应将其重定向到Google Play上的Google+)。如果已经安装了Google+应用程序,则按照通常方式进行操作。

Intent shareIntent = ShareCompat.IntentBuilder.from(this)
             .setText("Hello there! This is a pic of the lazy cat")
             .setType("image/jpeg")
             .setStream(Uri.parse(path))
             .getIntent()
             .setPackage("com.google.android.apps.plus");
 startActivity(shareIntent);

非常感谢您的帮助。提前致谢。


在使用ShareIntent之前,先检查是否安装了g+。如果已安装,则执行您的Intent。如果没有安装,则通知用户。 - Jviaches
您可以使用 GooglePlusUtil.checkGooglePlusApp 轻松检查用户是否已安装 Google+ 并根据此采取相应措施 https://developer.android.com/reference/com/google/android/gms/plus/GooglePlusUtil.html 另请参阅 https://developers.google.com/+/mobile/android/getting-started#frequently_asked_questions - Scarygami
我无法导入这个包。它显示错误:com.google.android.gms.plus.GooglePlusUtil; 看起来我缺少一些库文件。请建议。 - ArtificialIntelligence
啊,是的,抱歉,你需要启用Google Play服务SDK,并在你的项目中包含<android-sdk-folder>/extras/google/google_play_services/libproject/google-play-services_lib:https://developer.android.com/google/play-services/setup.html - Scarygami
谢谢,我会去检查一下。 - ArtificialIntelligence
显示剩余3条评论
1个回答

5

更新 下面的答案已经过时。你现在可以通过Google Play服务库(可通过Android SDK获得)检查是否安装了Google+应用程序。请参见此处获取有关如何将其添加到您的项目的信息。

示例:

int errorCode = GooglePlusUtil.checkGooglePlusApp(mContext);
if (errorCode != GooglePlusUtil.SUCCESS) {
  //Google+ is either not present or another error occured, show the error dialog
  GooglePlusUtil.getErrorDialog(errorCode, this, 0).show();
}
else{
  //Your Google+ related code here
}

旧答案

您可以创建某种检查来查看是否安装了Google+应用程序:

public void loadGooglePlus()
{
    if(isGooglePlusInstalled())
    {
        Intent shareIntent = ShareCompat.IntentBuilder.from(this)
               .setText("Hello there! This is a pic of the lazy cat")
               .setType("image/jpeg")
               .setStream(Uri.parse(path))
               .getIntent()
               .setPackage("com.google.android.apps.plus");
       startActivity(shareIntent);
   }
   else{
      //Notify user
   }
}

public boolean isGooglePlusInstalled()
{
    try
    {
        getPackageManager().getApplicationInfo("com.google.android.apps.plus", 0 );
        return true;
    } 
    catch(PackageManager.NameNotFoundException e)
    {
        return false;
    }
}

这看起来不错。我要试一下。但是,如果用户在他的Play商店应用程序上单击“确定”按钮时,他没有安装Google+,是否可能将用户重定向到Google Play上的Google+? - ArtificialIntelligence
使用以下代码来实现:`Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=com.google.android.apps.plus"));startActivity(myIntent);` - Leon Lucardie
谢谢,你帮了我很大的忙,另一个答案在API方面起作用,并且它提供了现成的警报对话框,通过按下该对话框的获取Google+按钮可以重定向到Google+应用程序。+1 - ArtificialIntelligence
2
或者使用 Play 服务:GooglePlusUtil.checkGooglePlusApp() - mente

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接