如何整合Facebook共享Diolouge以附加Url和图像?

时间:2013-12-22 10:02:42

标签: java android facebook

我是android的新手,我正在尝试整合Facebook sdk来分享链接和图像,

但是

我无法实现此功能。

我的代码是

  private UiLifecycleHelper uiHelper;

private static String APP_ID = "8975555558745832";

private Session.StatusCallback callback = new Session.StatusCallback()

{

    @Override

    public void call(Session session, SessionState state, Exception exception) {

      //  onSessionStateChange(session, state, exception);

    }
  };

  @Override
protected void onCreate(Bundle savedInstanceState) {

    // TODO Auto-generated method stub

    super.onCreate(savedInstanceState);

    setContentView(R.layout.share);

    uiHelper = new UiLifecycleHelper(this, callback);

    uiHelper.onCreate(savedInstanceState);

 }
 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    uiHelper.onActivityResult(requestCode, resultCode, data, new FacebookDialog.Callback() {
        @Override
        public void onError(FacebookDialog.PendingCall pendingCall, Exception error, Bundle data) {
            Log.e("Activity", String.format("Error: %s", error.toString()));
        }

        @Override
        public void onComplete(FacebookDialog.PendingCall pendingCall, Bundle data) {
            Log.i("Activity", "Success!");
        }
    });
}


@Override
protected void onResume() {
    super.onResume();
    uiHelper.onResume();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    uiHelper.onSaveInstanceState(outState);
}

@Override
public void onPause() {

    super.onPause();

    uiHelper.onPause();

}

@Override

public void onDestroy() {

    super.onDestroy();

    uiHelper.onDestroy();
}

最后我把这个代码叫做分享diolouge

  FacebookDialog shareDialog = new FacebookDialog.ShareDialogBuilder(this)

        .setLink("https://developers.facebook.com/android")

        .build();

uiHelper.trackPendingDialogCall(shareDialog.present());

谢谢。

1 个答案:

答案 0 :(得分:1)

作为你在Android中的新手,我建议你先学习

如何将状态从您的应用发布到Facebook

你需要一个easyfacebookandroidsdk.jar库并在你的项目中构建它

使用此代码:

public class MainActivity extends Activity implements LoginListener{

EditText et1;
 private FBLoginManager fbLoginManager;
 Button b;

 //replace it with your own Facebook App ID
 public final String FacebookTesting="218874471621782";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    et1=(EditText)findViewById(R.id.editText1);
    b=(Button)findViewById(R.id.button1);
    connectToFacebook();

}


 public void connectToFacebook(){

      //read about Facebook Permissions here:
      //http://developers.facebook.com/docs/reference/api/permissions/
      String permissions[] = {
//          "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"

      };

      fbLoginManager = new FBLoginManager(this,
        R.layout.activity_main, 
        FacebookTesting, 
        permissions);

      if(fbLoginManager.existsSavedFacebook()){
       fbLoginManager.loadFacebook();
      }
      else{
       fbLoginManager.login();
      }
     }





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


 @Override
 protected void onActivityResult(int requestCode, int resultCode, android.content.Intent data){


     fbLoginManager.loginSuccess(data);
 }

 public void loginSuccess(Facebook facebook) {
//  GraphApi graphApi = new GraphApi(facebook);
     new getDataBackgroundThread().execute(facebook);
//    User user = new User();
//
//    try{
//     user = graphApi.getMyAccountInfo();
//
//     //update your status if logged in
//     graphApi.setStatus("Hello, world!");
//    } catch(EasyFacebookError e){
//     Log.d("TAG: ", e.toString());
//    }

//    fbLoginManager.displayToast("Hey, " + user.getFirst_name() + "! Login success!");
 }

@Override
public void logoutSuccess() {
    // TODO Auto-generated method stub

}


@Override
public void loginFail() {
    // TODO Auto-generated method stub

}



 class getDataBackgroundThread extends AsyncTask<Facebook, Void, String> {

        private Exception exception;


        protected String doInBackground(Facebook... urls) {
            try{
                 GraphApi graphApi = new GraphApi(urls[0]);

                  User user = new User();

                  try{
                //   user = graphApi.getMyAccountInfo();

                   //update your status if logged in
                   //graphApi.setStatus("My android App "+"VivekAppTest");
                      graphApi.setStatus("Hello "+"VivekAppTest");
                  } catch(EasyFacebookError e){
                   Log.d("TAG: ", e.toString());
                  }
            return "posted";
            }
            catch (Exception e) {
                // TODO: handle exception
                return null;
            }


}
        protected void onPostExecute(String ipr) {
            // TODO: check this.exception 
            // TODO: do something with the feed
//
//               Intent i=new Intent(getApplicationContext(),MainActivity.class);
//               startActivity(i); 
//               finish();
//              Toast toast=Toast.makeText(getApplicationContext(),"Post Successful !", 1);
//              toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL,0, 130);
//              toast.show();
//          
        }
    }



}

它不会解决所有问题,但可能会有所帮助:)。