在Android应用程序中创建facebook事件

时间:2012-11-30 06:06:17

标签: android facebook events

我是Android新手,我只想从我要开发的应用程序创建 Facebook事件。稍后我会添加邀请朋友的功能。 我想将邀请作为通知或以任何好的方式显示。

我在互联网上做了很多研究,但找不到解决方案。

我真的很感激任何解决方案......

谢谢!

4 个答案:

答案 0 :(得分:2)

package com.tharaka.facebook;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.AsyncFacebookRunner.RequestListener;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;
import com.tharaka.facebook.R;

public class MainActivity extends Activity implements OnClickListener
{

    // Your Facebook APP ID
    private static String APP_ID = "392736034134808"; // Replace your App ID
                                                      // here

    // Instance of Facebook Class
    private Facebook facebook;
    private AsyncFacebookRunner mAsyncRunner;
    String FILENAME = "AndroidSSO_data";
    private SharedPreferences mPrefs;
    private Button btnFbLogin;
    private Button btnCreateEvent;
    private String TAG="MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initialize();
        setListeners();
    }

    private void initialize()
    {
        facebook = new Facebook(APP_ID);
        mAsyncRunner = new AsyncFacebookRunner(facebook);
        btnFbLogin = (Button) findViewById(R.id.button_FbLogin);
        btnCreateEvent = (Button) findViewById(R.id.button_createEvent);
    }

    private void setListeners()
    {
        btnFbLogin.setOnClickListener(this);
        btnCreateEvent.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public void onClick(View view)
    {
        switch (view.getId())
        {
            case R.id.button_FbLogin:
                loginToFacebook();
                break;            
            case R.id.button_createEvent:
                createEvent();
                Toast.makeText(getApplicationContext(), "New Event Created!!", Toast.LENGTH_LONG).show();
                break;
            default:
                break;
        }
    }

    private void loginToFacebook()
    {
        mPrefs = getPreferences(MODE_PRIVATE);
        String access_token = mPrefs.getString("access_token", null);
        long expires = mPrefs.getLong("access_expires", 0);

        if (access_token != null)
        {
            facebook.setAccessToken(access_token);
        }

        if (expires != 0)
        {
            facebook.setAccessExpires(expires);
        }

        if (!facebook.isSessionValid())
        {
            facebook.authorize(this, new String[] {
                "email", "publish_stream", "create_event"
            }, new DialogListener()
            {

                @Override
                public void onCancel()
                {
                    // Function to handle cancel event
                }

                @Override
                public void onComplete(Bundle values)
                {
                    // Function to handle complete event
                    // Edit Preferences and update facebook acess_token
                    SharedPreferences.Editor editor = mPrefs.edit();
                    editor.putString("access_token", facebook.getAccessToken());
                    editor.putLong("access_expires", facebook.getAccessExpires());
                    editor.commit();
                }

                @Override
                public void onError(DialogError error)
                {
                    // Function to handle error

                }

                @Override
                public void onFacebookError(FacebookError fberror)
                {
                    // Function to handle Facebook errors

                }

            });
        }
    }

    private void createEvent()
    {
        try
        {
            Bundle params = new Bundle();
            params.putString("name", "This is a test event");
            params.putString("start_time", "2013-12-02T18:00:00+0530");
            params.putString("end_time", "2013-12-02T20:00:00+0530");
            params.putString("description", "This is test description yeah?.");
            params.putString("location", "Mount Lavinia");
            //params.putString("location_id", "");
            params.putString("privacy_type", "OPEN");
            mAsyncRunner.request("me/events", params, "POST", new RequestListener()
            {

                @Override
                public void onMalformedURLException(MalformedURLException e, Object state)
                {

                }

                @Override
                public void onIOException(IOException e, Object state)
                {

                }

                @Override
                public void onFileNotFoundException(FileNotFoundException e, Object state)
                {

                }

                @Override
                public void onFacebookError(FacebookError e, Object state)
                {

                }

                @Override
                public void onComplete(String response, Object state)
                {
                    try
                    {
                        JSONObject event = new JSONObject(response);
                        String event_id = event.getString("id");
                        Log.i(TAG, "Event id => "+event_id);

                       //Toast.makeText(getApplicationContext(), "New Event Created!!", Toast.LENGTH_LONG).show();
                    }
                    catch (Exception e)
                    {

                    }
                }
            }, null);
        }
        catch (Exception e)
        {

        }
    }
}

答案 1 :(得分:0)

Google> FaceBook Developer

这会告诉你一切。

您可以找到一些其他链接来构建Facebook集成应用程序。

祝你好运。

答案 2 :(得分:0)

首先从以下链接下载belove项目... https://github.com/facebook/facebook-android-sdk 并在您的项目中使用facebook sdk 按照这个(http://www.integratingstuff.com/2010/10/14/integrating-facebook-into-an) 链接更多facebook intergaration [。] [2]

答案 3 :(得分:0)