【问题标题】:App force closes when google+ is not installed未安装 google+ 时应用程序强制关闭
【发布时间】:2013-04-23 09:57:35
【问题描述】:

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

现在我需要做的至少是通知用户google+ 共享需要已经安装google+ app 通过对话框或吐司而不是强制关闭(如果可能通过单击对话框上的确定应该重定向到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);

感谢任何帮助。提前致谢。

【问题讨论】:

  • 查看 logcat 并使用调试器。很可能会引发未处理的异常。
  • @Axel 它给出了 ActivityNotFound 异常,因为它无法在我指定的设备上找到活动。因为没有安装 google+。请阅读我的问题并提出建议。
  • 在 ShareIntent 之前检查是否安装了 g+。如果是这样,那么执行你的意图。如果否,通知用户..
  • 您可以使用 GooglePlusUtil.checkGooglePlusApp 轻松检查用户是否安装了 Google+ 并做出相应的反应developer.android.com/reference/com/google/android/gms/plus/… 另见developers.google.com/+/mobile/android/…
  • 我无法导入这个包。它显示错误。 com.google.android.gms.plus.GooglePlusUtil;似乎我缺少一些库文件。请提出建议。

标签: android android-intent google-plus


【解决方案1】:

更新 下面的答案已经过时了。您现在可以通过 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
}

老答案

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

【讨论】:

  • 这似乎很好。要试试看。但是,如果用户在尚未安装 google+ 的情况下单击“确定”按钮,是否可以将用户重定向到其 Play 商店应用上的 google+?
  • 为此使用以下代码:Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=com.google.android.apps.plus")); startActivity(myIntent);
  • 谢谢,你帮了大忙,另一个答案是 api 明智的,它给了我现成的警报对话框,通过按下该对话框的获取 google+ 按钮重定向到 google+app。+1
  • 或使用播放服务:GooglePlusUtil.checkGooglePlusApp()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-29
  • 1970-01-01
  • 2014-12-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多