安卓(Android) 怎么打开Facebook意图(intent)

2019年10月19日12:08:29安卓(Android) 怎么打开Facebook意图(intent)已关闭评论 411 1643字阅读5分28秒

1.简介

安卓(Android) 怎么打开Facebook意图(intent)?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

import java.util.List;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.util.Log;

public class Main{
    private static final String TAG = "calendar IntentHelper ";
    private static final String FB_APP_PROFILE_URL = FB_APP_BASE_URL
            + "profile/";
    private static final String FB_APP_EVENT_URL = FB_APP_BASE_URL
            + "event/";
    private static final String FB_WEB_EVENT_URL = FB_WEB_BASE_URL
            + "events/";
    private static final String FB_WEB_PROFILE_URL = FB_WEB_BASE_URL;
    
    public static void openFacebook(Context context, String id, Facebook fb) {
        String url = "";

        if (isFacebookInstalled(context)) {
            
            Log.d(TAG, "openFacebook() facebook is installed");
            switch (fb) {
            case EVENT:
                url = FB_APP_EVENT_URL + id;
                break;
            case PROFILE:
                url = FB_APP_PROFILE_URL + id;
                break;
            default:
                Log.e(TAG, "openFacebook() cannot open!");
                break;
            }
        }
        else {
            Log.d(TAG, "openFacebook() facebook is NOT installed");
            switch (fb) {
            case EVENT:
                url = FB_WEB_EVENT_URL + id;
                break;
            case PROFILE:
                url = FB_WEB_PROFILE_URL + id;
            default:
                Log.e(TAG, "openFacebook() cannot open!");
                break;
            }

        }

        Log.d(TAG, "openFacebook() url = " + url);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(url));
        context.startActivity(intent);
    }
    
    private static boolean isFacebookInstalled(Context context) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        final String urlFb = "fb:
        intent.setData(Uri.parse(urlFb));

        
        final PackageManager packageManager = context.getPackageManager();

        List<ResolveInfo> list = packageManager.queryIntentActivities(
                intent, PackageManager.MATCH_DEFAULT_ONLY);
        if (list != null && list.size() > 0) {
            return true;
        } else {
            return false;
        }
    }
}

 
继续阅读
编程十万个怎么办