[英]Android: Check to see if Facebook app is present on user's device
我保留了一個按鈕,將用戶帶到我的臉書頁面。 為了打開官方的Facebook應用程序,我使用以下網址:
fb://pages/PAGE_ID
而不是http://facebook.com/PAGE_ID
因為在這種情況下你會得到一個瀏覽器列表來打開網址而不是teh facebook app。
如果用戶安裝了facebook應用程序,它將起作用。 但是,如果用戶沒有Facebook應用程序,它會崩潰。
有什么方法可以檢查用戶是否有facebook應用程序?
在原生Android應用程序中,這很容易實現:
Uri dataUri = Uri.parse("fb://....");
Intent receiverIntent = new Intent(Intent.ACTION_VIEW, dataUri);
PackageManager packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(receiverIntent, 0);
if (activities.size() > 0) {
startActivity(receiverIntent);
} else {
Uri webpage = Uri.parse("http://www.facebook.com/...");
Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);
packageManager = getPackageManager();
activities = packageManager.queryIntentActivities(webIntent, 0);
if (activities.size() > 0) {
startActivity(webIntent);
}
}
我想你可以重復使用我編寫的代碼來檢查設備上是否安裝了twitter應用程序,以檢查是否安裝了facebook應用程序。 對於twitterApps列表,您必須用“com.facebook.katana”替換值。
public Intent findTwitterClient() {
final String[] twitterApps = { "com.twitter.android", "com.handmark.tweetcaster", "com.seesmic", "com.thedeck.android", "com.levelup.touiteur", "com.thedeck.android.app" };
Intent tweetIntent = new Intent(Intent.ACTION_SEND);
tweetIntent.putExtra(Intent.EXTRA_TEXT, "#hashtagTest");
tweetIntent.setType("text/plain");
final PackageManager packageManager = getPackageManager();
List<ResolveInfo> list = packageManager.queryIntentActivities(tweetIntent, PackageManager.MATCH_DEFAULT_ONLY);
for (int i = 0; i < twitterApps.length; i++) {
for (ResolveInfo resolveInfo : list) {
String p = resolveInfo.activityInfo.packageName;
if (p != null && p.startsWith(twitterApps[i])) {
tweetIntent.setPackage(p);
return tweetIntent;
}
}
}
return null;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.