7

My requirement is to share on Social networking sites. So, I have already done with Facebook and Twitter. But I am stuck with Google+. I have the following code to share on Google+, but the app forcecloses when I start activity. This occurs only when the Google+ app isn't installed on device already. I know this sharing intent requires the Google+ already installed to start the activity.

Now what I need to do is at least to inform the user that the google+ sharing requires already installed google+ app through dialog or toast instead of getting force closed (If possible by clicking on ok on the dialog should redirect to the google+ on google play). If the google+ app is installed already it goes as usual.

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);

Any help is appreciated. Thanks in advance.

4

1 回答 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;
    }
}
于 2013-04-23T10:36:01.617 回答