2014-11-08 182 views
0

我是新的android開發如何打開Facebook的個人主頁/頁從的ImageButton(安卓)

我發展與ImageButton的新應用Facebook的個人主頁/頁

我想這個代碼,但這個代碼開放在瀏覽器中的Facebook

public class AboutActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_about); 


    ImageButton f = (ImageButton)findViewById(R.id.f_logo); 
     f.setOnClickListener(new OnClickListener() 
     { 
      public void onClick(View arg0) 
       { 
        Intent browserIntent = new Intent(Intent.ACTION_VIEW,Uri.parse(
        "http://www.facebook.com/sarmad.waleed.7")); 
        startActivity(browserIntent); 

        } 
      }); 
      } 
     } 

我的問題是如何打開Facebook的個人主頁/ 頁從FB應用(如果安裝)的ImageButton如果沒有的話在打開它瀏覽器

我也檢查了這

how to link the image button to a facebook page in android

,但其同樣的Facebook頁面在瀏覽器

然後我用「com.facebook.katana」試圖打開,但我不知道如何去做

+1

答案可以在這裏找到http://stackoverflow.com/questions/10788247/opening-facebook-app-on-specified-profile-page – 2014-11-08 17:44:32

+1

http://stackoverflow.com/questions/4191492/launch -face-app-from-other-app – 2014-11-08 17:44:45

回答

2

Facebook Android應用程序不支持此操作的隱式意圖機制,因爲1.9.11版本。 Facebook現在使用相同的iPhone方案機制fb://facebook://來處理提及的所有操作here

在這裏你可以看到他們支持fb和facebook方案。

<activity android:name="com.facebook.katana.IntentUriHandler"> 
     <intent-filter> 
      <action android:name="android.intent.action.VIEW" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
      <data android:scheme="facebook" /> 
     </intent-filter> 
     <intent-filter> 
      <action android:name="android.intent.action.VIEW" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
      <category android:name="android.intent.category.BROWSABLE" /> 
      <data android:scheme="fb" /> 
     </intent-filter> 
    </activity> 

根據您的要求,此方法將處理這兩種情況。首先它會檢查facebook應用程序是否安裝,否則它將在瀏覽器中打開Facebook配置文件頁面。

​​

爲了與用戶配置文件的所有你需要做的打開Facebook的應用程序是:

Intent facebookIntent = getFBIntent(this, "2347633432"); 
startActivity(facebookIntent); 

** 編輯 **

這是你如何調用以上方法在你的活動中。而已!

public class AboutActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_about); 

    ImageButton f = (ImageButton)findViewById(R.id.f_logo); 
    f.setOnClickListener(new OnClickListener() 
    { 
     public void onClick(View arg0) 
     { 
      // Get the intent 
      Intent intent = getFBIntent(AboutActivity.this, "sarmad.waleed.7"); 

      // Start the activity 
      if (intent != null) 
       startActivity(intent); 
     } 
    }); 
    } 

    /** 
    * Get the facebook intent for the given facebook 
    * profile id. If the facebook app is installed, then 
    * it will open the facebook app. Otherwise, it will 
    * open the facebook profile page in browser. 
    * 
    * @return - the facebook intent 
    */ 
    private Intent getFBIntent(Context context, String facebookId) { 

     try { 
     // Check if FB app is even installed 
     context.getPackageManager().getPackageInfo("com.facebook.katana", 0); 

     String facebookScheme = "fb://profile/" + facebookId; 
     return new Intent(Intent.ACTION_VIEW, Uri.parse(facebookScheme)); 
     } 
     catch(Exception e) { 

     // Cache and Open a url in browser 
     String facebookProfileUri = "https://www.facebook.com/" + facebookId; 
     return new Intent(Intent.ACTION_VIEW, Uri.parse(facebookProfileUri)); 
    } 

    return null; 
    } 
} 
+0

你的代碼根本不工作:/我有錯誤! – sham3on 2014-11-08 23:30:55

+0

我剛剛寫了代碼給你一個想法,它是如何工作的。爲了在沒有任何編譯器錯誤的情況下正確工作,我的假設是,只要有必要,它都會進行相應的調整。你能讓我知道什麼樣的錯誤? – Deminem 2014-11-08 23:45:46

+0

我知道謝謝你,我明白這一點,但我吮吸java所以我不知道我可以把方法 – sham3on 2014-11-08 23:53:20