android – 未安装Google时App力关闭

我的要求是在社交网站上分享.所以,我已经完成了Facebook和Twitter.但我被谷歌困住了.我在Google上分享了以下代码,但是当我开始活动时,应用强制关闭.只有在设备上尚未安装Google应用时才会出现这种情况.我知道这种共享意图需要已经安装Google来启动活动.

现在我需要做的是至少告知用户谷歌共享需要已经通过对话或吐司安装谷歌应用程序,而不是强行关闭(如果可能的话,点击对话框上的确定应该重定向到Google Play上的谷歌) .如果已安装谷歌应用程序,它会像往常一样.

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服务库检查Google应用是否已安装(可通过Android SDK获得).有关如何将其添加到项目的信息,请参见 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
}

老答复

您可以创建某种检查以查看是否已安装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;
    }
}

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...