Reputation: 10353
I am new to android, I want only to create a Facebook event from the app that I am going to develop. Later I will be adding the feature to invite friends. I would like to display the invitation as a notification or in any good way.
I did so much research on internet but could not find the solution.
I will really appreciate any solutions...
Thank you!
Upvotes: 4
Views: 7713
Reputation: 10353
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)
{
}
}
}
Upvotes: 3
Reputation: 1856
just refer the links given below:
http://ericosgood.com/prog/facebook-android-sdk-tutorial/
and
Upvotes: 0
Reputation: 2119
First download belove project from following link... https://github.com/facebook/facebook-android-sdk and use facebook sdk in your project and follow this(http://www.integratingstuff.com/2010/10/14/integrating-facebook-into-an) link for more facebook intergaration [.][2]
Upvotes: 0
Reputation: 16142
Google > FaceBook Developer
This will tell you everything.
Or
You can find some other links to build a Facebook integration application.
Best of luck.
Upvotes: 0