gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 10:25:09 25 4
gpt4 key购买 nike

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

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

我将非常感谢任何解决方案...

谢谢!

最佳答案

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)
{

}
}
}

关于android - 在 Android 应用程序中创建 facebook 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13639919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com