2012-08-08 11 views
21

我有一個Google Plus頁面打開谷歌加威盛意圖在Android中

https://plus.google.com/u/0/b/101839105638971401281/101839105638971401281/posts

和Android應用程序。 我想在我的應用程序中打開此頁面。我不想打開瀏覽器!

這將打開瀏覽器:

URL="https://plus.google.com/b/101839105638971401281/101839105638971401281/posts"; 
uri = Uri.parse(URL); 
it = new Intent(Intent.ACTION_VIEW, uri); 
startActivity(it); 

此崩潰:提前

Intent intent = new Intent(Intent.ACTION_VIEW); 

intent.setClassName("com.google.android.apps.plus",    "com.google.android.apps.plus.phone.UrlGatewayActivity"); 

intent.putExtra("customAppUri", "10183910563897140128"); 
startActivity(intent); 

謝謝!

[解決]

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://plus.google.com/101839105638971401281/posts"))); 

通過這一解決方案,用戶可以選擇谷歌加APP或打開瀏覽器。如果選擇APP,則不會崩潰。

+0

如何將WebView嵌入您的應用程序並在其中加載頁面? – Nick 2012-08-08 15:37:06

+0

因此我更願意直接打開瀏覽器。 – benoffi7 2012-08-08 16:17:27

+0

Google+社區如何?http://stackoverflow.com/questions/23075842/android-intent-to-launch-google-app-at-google-community-screen – 2014-04-15 11:37:54

回答

24

如果用戶安裝了Google+應用,你可以這樣做:

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://plus.google.com/101839105638971401281/posts"))); 

通知的URI語法,而且它不包含/b/id/

1

堆棧跟蹤在崩潰時會說什麼?

此外,我不確定這是否會有所作爲,但ID中存在拼寫錯誤。你寫道:

intent.putExtra("customAppUri", "10183910563897140128"); 

但最初的ID是101839105638971401281。你最後離開了1。

21

您必須首先檢查用戶是否已在他/她的手機上安裝了G +應用程序?如果是,那麼我們可以通過特定的意圖啓動它,或者我們可以使用瀏覽器重定向到特定頁面。

下面是這種流動的一個方法,

public void openGPlus(String profile) { 
    try { 
     Intent intent = new Intent(Intent.ACTION_VIEW); 
     intent.setClassName("com.google.android.apps.plus", 
      "com.google.android.apps.plus.phone.UrlGatewayActivity"); 
     intent.putExtra("customAppUri", profile); 
     startActivity(intent); 
    } catch(ActivityNotFoundException e) { 
     startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://plus.google.com/"+profile+"/posts"))); 
    } 
} 

現在你可以調用這個方法單純的喜歡,

//117004778634926368759 is my google plus id 
openGPlus("117004778634926368759"); 

擴展答:爲Twitter和Facebook 相同的方式,可以使用

對於推特

public void openTwtr(String twtrName) { 
     try { 
      startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("twitter://user?screen_name=" + twtrName))); 
     } catch (ActivityNotFoundException e) { 
      startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("https://twitter.com/#!/" + twtrName))); 
     } 
} 

和Facebook的

public void openFB(String facebookId) { 
    try{ 
     String facebookScheme = "fb://profile/" + facebookId; 
     Intent facebookIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(facebookScheme)); 
     startActivity(facebookIntent); 
    } catch (ActivityNotFoundException e) { 
     Intent facebookIntent = new Intent(Intent.ACTION_VIEW,Uri.parse("https://www.facebook.com/profile.php?id="+facebookId)); 
     startActivity(facebookIntent); 
    } 
} 
+2

將網關活動更改爲:com.google.android.libraries.social.gateway.GatewayActivity – Amt87 2015-06-07 12:02:04

+0

關於「com.google.android.apps.plus。 phone.UrlGatewayActivity「,你永遠不應該認爲這是活動的路徑,而應該只使用packageName,通過調用queryIntentActivities然後檢查其中哪些匹配應用程序的packageName。 – 2015-12-17 13:15:18

+0

對於fb:網絡意圖的網址應改爲「https://www.facebook.com/"+facebookId – 2016-01-26 12:21:13

3
/** 
* Intent to open the official Google+ app to the user's profile. If the Google+ app is not 
* installed then the Web Browser will be used. 
* 
* </br></br>Example usage:</br> 
* <code>newGooglePlusIntent(context.getPackageManager(), "https://plus.google.com/+JaredRummler");</code> 
* 
* @param pm 
*   The {@link PackageManager}. 
* @param url 
*   The URL to the user's Google+ profile. 
* @return The intent to open the Google+ app to the user's profile. 
*/ 
public static Intent newGooglePlusIntent(PackageManager pm, String url) { 
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
    try { 
     if (pm.getPackageInfo("com.google.android.apps.plus", 0) != null) { 
      intent.setPackage("com.google.android.apps.plus"); 
     } 
    } catch (NameNotFoundException e) { 
    } 
    return intent; 
} 
0

爲什麼不Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 。 Android操作系統查詢所有可以處理特定Uri的應用程序。作爲一款應用,Google+可以編程爲能夠處理您請求的Uri。所以它會在選擇器中顯示爲一個選項(或者如果用戶已經選擇了Google+應用程序作爲該Uri的默認選項,那麼它會顯示出來)。