Giới thiệu
Ví dụ này minh họa cách mở một trang Facebook cụ thể từ ứng dụng Android, tự động phát hiện ứng dụng Facebook đã cài đặt hay chưa để mở bằng Intent phù hợp.
Các bước thực hiện
Bước 1: Tạo dự án mới
Mở Android Studio, chọn File → New Project, điền thông tin tạo dự án.
Bước 2: Giao diện (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/rlMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp"
android:onClick="fbClick"
android:text="Mở Facebook" />
</LinearLayout>
Bước 3: Code MainActivity (src/MainActivity.java)
package app.com.sample;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void fbClick(View view) {
startActivity(getOpenFacebookIntent());
}
public Intent getOpenFacebookIntent() {
try {
// Kiểm tra app Facebook đã cài đặt
getPackageManager().getPackageInfo("com.facebook.katana", 0);
// Mở bằng Facebook App (deep link)
return new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/426253597411506"));
} catch (Exception e) {
// Fallback: mở bằng trình duyệt
return new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/appetizerandroid"));
}
}
}
Bước 4: Manifest (androidManifest.xml)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.sample">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Chạy ứng dụng
Kết nối thiết bị thật, nhấn Run trong Android Studio. Nhấn nút "Mở Facebook" để kiểm tra.

Giải thích kỹ thuật
- fb://page/[ID]: Deep link scheme của Facebook App để mở trực tiếp trang fanpage.
- getPackageInfo(): Kiểm tra package "com.facebook.katana" (Facebook App) có tồn tại không.
- Fallback HTTPS: Nếu chưa cài Facebook App, mở trang web trên trình duyệt.
Lưu ý: Thay thế ID trang "426253597411506" và URL "appetizerandroid" bằng ID/username trang Facebook của bạn.