我的要求是在社交网站上分享。所以,我已经用完了Facebook和Twitter。但是我被Google+
困住了。我有以下代码分享Google+
,但应用程序forcecloses
时,我开始活动。只有在设备上没有安装Google+ app
时才会发生这种情况。我知道这个分享的意图需要已经安装的Google+来启动这个活动。
现在我需要做的是至少通知用户,google+
共享需要已经安装了google+ app
通过对话框或toast而不是强制关闭(如果可能的话,点击对话框上的ok应该重定向到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);
任何帮助都是感激的。
UPDATE下面的答案已经过时了。你现在可以通过Google Play Services库(通过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;
}
}