2013-04-23 38 views
7

我的要求是在社交網站上分享。所以,我已經完成了Facebook和Twitter。但我堅持與Google+。我有以下代碼分享Google+,但應用程序forcecloses當我開始活動。只有當Google+ app尚未安裝在設備上時,纔會發生這種情況。我知道這種分享意圖需要已安裝的Google+來啓動該活動。未安裝Google +時App Force關閉

現在我需要做的是至少通過單擊確定關閉對話框上通知的google+共享要求已經通過對話或烤麪包,而不是讓強制關閉安裝google+ app用戶(如果可能的話應該重新定向到谷歌+上谷歌播放)。如果已經安裝了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); 

任何幫助表示讚賞。提前致謝。

+0

看那logcat的,並使用一個調試器。最有可能引發未處理的異常。 – Axel 2013-04-23 10:02:01

+0

@Axel它給ActivityNotFound異常,因爲它無法找到我指定的設備上的活動。因爲google +沒有安裝。請閱讀我的問題並提出建議。 – ArtificialIntelligence 2013-04-23 10:07:17

+0

在ShareIntent之前檢查是否安裝了g +。如果是的話執行你的意圖。如果否,請通知用戶.. – Jviaches 2013-04-23 10:10:31

回答

5

UPDATE 以下答案已過時。您現在可以通過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 
} 

OLD ANSWER

你可以建立某種形式的檢查,看看是否安裝了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; 
    } 
} 
+0

這似乎很好。要嘗試一下。但是,如果他沒有安裝Google +,當他點擊好的按鈕時,是否可以將用戶重定向到谷歌+在他的Play商店應用上播放? – ArtificialIntelligence 2013-04-23 10:42:38

+0

要使用以下代碼: '意圖myIntent =新意圖(Intent.ACTION_VIEW,Uri.parse(「https://play.google.com/store/apps/details?id=com.google.android .apps.plus「)); startActivity(myIntent);' – 2013-04-23 11:00:10

+0

謝謝,你的幫助很大,另一個答案api明智,它給了我現成的警報對話框,通過按下該對話框的按鈕獲得重定向到谷歌+應用程序。 – ArtificialIntelligence 2013-04-23 11:45:49