我一直在寻找一种方法来做到这一点。但似乎没有什么对我有用。有人可以帮忙这样做吗? 这是我的代码
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;
import com.facebook.android.Util;
public class MainActivity extends Activity {
Facebook fb;
SharedPreferences sp;
ImageView pic,button;
TextView welcome;
Button post;
private static final String[] PERMISSIONS = new String[] {
"user_about_me",
"user_activities",
"user_birthday",
"user_checkins",
"user_education_history",
"user_events",
"user_groups",
"user_hometown",
"user_interests",
"user_likes",
"user_location",
"user_notes",
"user_online_presence",
"user_photo_video_tags",
"user_photos",
"user_relationships",
"user_relationship_details",
"user_religion_politics",
"user_status",
"user_videos",
"user_website",
"user_work_history",
"email",
"read_friendlists",
"read_insights",
"read_mailbox",
"read_requests",
"read_stream",
"xmpp_login",
"ads_management",
"create_event",
"manage_friendlists",
"manage_notifications",
"offline_access",
"publish_checkins",
"publish_stream",
"rsvp_event",
"sms",
//"publish_actions",
"manage_pages"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
post=(Button)findViewById(R.id.post);
String APP_Id=getString(R.string.APP_ID);
fb=new Facebook(APP_Id);
sp=getPreferences(MODE_PRIVATE);
String acces_token=sp.getString("access_token", null);
long expires=sp.getLong("access_expires",0);
if(acces_token != null){
fb.setAccessToken(acces_token);
}
if(expires != 0){
fb.setAccessExpires(expires);
}
button=(ImageView) findViewById(R.id.login);
pic=(ImageView)findViewById(R.id.picture_pic);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(fb.isSessionValid()){
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{
fb.authorize(MainActivity.this,PERMISSIONS,fb.FORCE_DIALOG_AUTH ,new DialogListener() {
@Override
public void onFacebookError(FacebookError e) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "fbError", 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 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 onCancel() {
Toast.makeText(MainActivity.this, "onCancel",Toast.LENGTH_SHORT).show();
}
});
}
}
});
updateButtonImage();
}
private void updateButtonImage() {
// TODO Auto-generated method stub
if(fb.isSessionValid()){
post.setVisibility(Button.VISIBLE);
button.setImageResource(R.drawable.logout);
pic.setVisibility(ImageView.VISIBLE);
JSONObject obj=null;
URL img_url=null;
try {
String jsonUser = fb.request("me");
obj=Util.parseJson(jsonUser);
String id=obj.optString("id");
String name=obj.optString("name");
welcome=(TextView)findViewById(R.id.welcome);
welcome.setText("Welcome " + name);
img_url=new URL("http://graph.facebook.com/"+id+"/picture?type=normal");
Bitmap bmp=BitmapFactory.decodeStream(img_url.openConnection().getInputStream());
pic.setImageBitmap(bmp);
} catch (FacebookError e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
post.setVisibility(Button.INVISIBLE);
button.setImageResource(R.drawable.login_button);
pic.setVisibility(ImageView.INVISIBLE);
}
}
public void buttonClicks(View v){
switch(v.getId()){
case R.id.post:
// Post
fb.dialog(MainActivity.this, "feed", new DialogListener() {
@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 onComplete(Bundle values) {
// TODO Auto-generated method stub
}
@Override
public void onCancel() {
// TODO Auto-generated method stub
}
});
}
}
@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);
}
}
有人能指出正确的方向吗? :) 谢谢所有