2013-04-23 70 views
7

我的要求是在社交网站上分享。所以,我已经完成了Facebook和Twitter。但我坚持与Google+。我有以下代码分享Google+,但应用程序forcecloses当我开始活动。只有当Google+ app尚未安装在设备上时,才会发生这种情况。我知道这种分享意图需要已安装的Google+来启动该活动。未安装Google +时App Force关闭

现在我需要做的是至少通过单击确定关闭对话框上通知的google+共享要求已经通过对话或烤面包,而不是让强制关闭安装google+ app用户(如果可能的话应该重新定向到谷歌+上谷歌播放)。如果已经安装了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); 

任何帮助表示赞赏。提前致谢。

+0

看那logcat的,并使用一个调试器。最有可能引发未处理的异常。 – Axel 2013-04-23 10:02:01

+0

@Axel它给ActivityNotFound异常,因为它无法找到我指定的设备上的活动。因为google +没有安装。请阅读我的问题并提出建议。 – ArtificialIntelligence 2013-04-23 10:07:17

+0

在ShareIntent之前检查是否安装了g +。如果是的话执行你的意图。如果否,请通知用户.. – Jviaches 2013-04-23 10:10:31

回答

5

UPDATE 以下答案已过时。您现在可以通过Google Play服务库(通过Android SDK提供)来检查是否安装了Google+应用。有关如何将其添加到项目中的信息,请参阅here

例子:

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 
} 

OLD ANSWER

你可以建立某种形式的检查,看看是否安装了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; 
    } 
} 
+0

这似乎很好。要尝试一下。但是,如果他没有安装Google +,当他点击好的按钮时,是否可以将用户重定向到谷歌+在他的Play商店应用上播放? – ArtificialIntelligence 2013-04-23 10:42:38

+0

要使用以下代码: '意图myIntent =新意图(Intent.ACTION_VIEW,Uri.parse(“https://play.google.com/store/apps/details?id=com.google.android .apps.plus“)); startActivity(myIntent);' – 2013-04-23 11:00:10

+0

谢谢,你的帮助很大,另一个答案api明智,它给了我现成的警报对话框,通过按下该对话框的按钮获得重定向到谷歌+应用程序。 – ArtificialIntelligence 2013-04-23 11:45:49