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

相关文章

文章浏览阅读8.8k次,点赞9次,收藏20次。本文操作环境:win1...
文章浏览阅读1.2w次,点赞15次,收藏69次。实现目的:由main...
文章浏览阅读3.8w次。前言:最近在找Android上的全局代理软件...
文章浏览阅读2.5w次,点赞17次,收藏6次。创建项目后,运行项...
文章浏览阅读8.9w次,点赞4次,收藏43次。前言:在Android上...
文章浏览阅读1.1w次,点赞4次,收藏17次。Android Studio提供...