I successfully managed to integrate the Facebook sdk in my my with the help of following code:
public class MainActivity extends Activity implements OnClickListener {
Facebook fb;
Button button;
SharedPreferences sp;
//TextView welcome;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String APP_ID = getString(R.string.APP_ID);
fb=new Facebook(APP_ID);
sp=getPreferences(MODE_PRIVATE);
String access_token=sp.getString("access token",null);
long expires=sp.getLong("access expires",0);
if(access_token!=null){
fb.setAccessToken(access_token);
}
if(expires!=0){
fb.setAccessExpires(expires);
}
button=(Button)findViewById(R.id.login);
//pic=(ImageView)findViewById(R.id.picture_pic);
button.setOnClickListener(this);
updateButtonImage();
}
private void updateButtonImage() {
// TODO Auto-generated method stub
if(fb.isSessionValid()){
try{
//post.setVisibility(Button.VISIBLE);
button.setBackgroundResource(R.drawable.logout_button);
/*pic.setVisibility(ImageView.VISIBLE);
JSONObject obj=null;
URL img_url = null;
String jsonuser=fb.request("me");
obj=Util.parseJson(jsonuser);
String id = obj.optString("id");
String name = obj.optString("name");
welcome.setText("Welcome "+name);
img_url=new URL("http://graph.facebook.com/"+id+"/picture?type=large");
Bitmap bmp = BitmapFactory.decodeStream(img_url.openConnection().getInputStream());
pic.setImageBitmap(bmp);*/
buttonClicks();
}catch(Exception e){
e.printStackTrace();
}
}else{
//post.setVisibility(Button.INVISIBLE);
button.setBackgroundResource(R.drawable.login_button);
//pic.setVisibility(ImageView.INVISIBLE);
}
}
public void buttonClicks()
{
Bundle params = new Bundle();
params.putString("image", "your string");
fb.dialog(MainActivity.this, "feed", params, new DialogListener(){
@Override
public void onComplete(Bundle values) {
// TODO Auto-generated method stub
}
@Override
public void onFacebookError(FacebookError e) {
// TODO Auto-generated method stub
}
@Override
public void onError(DialogError e) {
// TODO Auto-generated method stub
}
@Override
public void onCancel() {
// TODO Auto-generated method stub
}
});
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(fb.isSessionValid()){
//button close our session - log out facebook
try {
fb.logout(getApplicationContext());
updateButtonImage();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
//login to fb
fb.authorize(MainActivity.this, new String[] {"email"}, new DialogListener(){
@Override
public void onComplete(Bundle values) {
// TODO Auto-generated method stub
Editor editor=sp.edit();
editor.putString("access_token", fb.getAccessToken());
editor.putLong("access expires", fb.getAccessExpires());
editor.commit();
updateButtonImage();
}
@Override
public void onFacebookError(FacebookError e) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "onFacebookError", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(DialogError e) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "onError", Toast.LENGTH_SHORT).show();
}
@Override
public void onCancel() {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "onCancel", Toast.LENGTH_SHORT).show();
}
});
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
fb.authorizeCallback(requestCode, resultCode, data);
}
}
I hope, you will get some help with this.