Facebook SDK 3.0 Logout Not Working in android Application from any other Intent -
i using facebook login in android app,its login .
after successful login intent goes next page.
from second page trying logout facebook ,its not working
help me resolve it.
else suggest me working example facebook login,logout,getting profile information latest sdk4.2.
my logout method
public void logoutfromfacebook() { masyncrunner.logout(this, new requestlistener() { @override public void oncomplete(string response, object state) { log.d("logout facebook", response); if (boolean.parseboolean(response) == true) { //logout successss } } }); }
this facebook login code
androidfacebookconnectactivity.java
public class androidfacebookconnectactivity extends activity { // facebook app id private static string app_id = "*************" ; // replace app id // instance of facebook class private facebook facebook = new facebook(app_id); private asyncfacebookrunner masyncrunner; string filename = "androidsso_data"; private sharedpreferences mprefs; // buttons button btnfblogin; button btnfbgetprofile; button btnposttowall; button btnshowaccesstokens; button logout; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); btnfblogin = (button) findviewbyid(r.id.btn_fblogin); btnfbgetprofile = (button) findviewbyid(r.id.btn_get_profile); btnposttowall = (button) findviewbyid(r.id.btn_fb_post_to_wall); btnshowaccesstokens = (button) findviewbyid(r.id.btn_show_access_tokens); logout=(button)findviewbyid(r.id.btn_show_logout); masyncrunner = new asyncfacebookrunner(facebook); /** * login button click event * */ btnfblogin.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { log.d("image button", "button clicked"); logintofacebook(); } }); /** * getting facebook profile info * */ btnfbgetprofile.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { getprofileinformation(); } }); /** * posting facebook wall * */ btnposttowall.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { posttowall(); } }); /** * showing access tokens * */ btnshowaccesstokens.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { showaccesstokens(); } }); logout.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { log.d("logout button clicked", "button clicked"); logoutfromfacebook(); } }); } /** * function login facebook * */ public 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); btnfblogin.setvisibility(view.invisible); // making profile button visible btnfbgetprofile.setvisibility(view.visible); // making post wall visible btnposttowall.setvisibility(view.visible); // making show access tokens button visible btnshowaccesstokens.setvisibility(view.visible); logout.setvisibility(view.visible); log.d("fb sessions", "" + facebook.issessionvalid()); } if (expires != 0) { facebook.setaccessexpires(expires); } if (!facebook.issessionvalid()) { facebook.authorize(this, new string[] { "email", "publish_actions" }, new dialoglistener() { @override public void oncancel() { // function handle cancel event } @override public void oncomplete(bundle values) { // function handle complete event // edit preferences , update facebook acess_token sharedpreferences.editor editor = mprefs.edit(); editor.putstring("access_token", facebook.getaccesstoken()); editor.putlong("access_expires", facebook.getaccessexpires()); editor.commit(); // making login button invisible btnfblogin.setvisibility(view.invisible); // making logout button visible btnfbgetprofile.setvisibility(view.visible); // making post wall visible btnposttowall.setvisibility(view.visible); // making show access tokens button visible btnshowaccesstokens.setvisibility(view.visible); logout.setvisibility(view.visible); } @override public void onerror(dialogerror error) { // function handle error } @override public void onfacebookerror(facebookerror fberror) { // function handle facebook errors } }); } } @override public void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); facebook.authorizecallback(requestcode, resultcode, data); } /** * profile information making request facebook graph api * */ public void getprofileinformation() { masyncrunner.request("me", new requestlistener() { @override public void oncomplete(string response, object state) { log.d("profile", response); string json = response; try { // facebook profile json data jsonobject profile = new jsonobject(json); // getting name of user final string name = profile.getstring("name"); // getting email of user final string email = profile.getstring("email"); runonuithread(new runnable() { @override public void run() { toast.maketext(getapplicationcontext(), "name: " + name + "\nemail: " + email, toast.length_long).show(); } }); } catch (jsonexception e) { e.printstacktrace(); } } @override public void onioexception(ioexception e, object state) { } @override public void onfilenotfoundexception(filenotfoundexception e, object state) { } @override public void onmalformedurlexception(malformedurlexception e, object state) { } @override public void onfacebookerror(facebookerror e, object state) { } }); } /** * function post facebook wall * */ public void posttowall() { // post on user's wall. facebook.dialog(this, "feed", new dialoglistener() { @override public void onfacebookerror(facebookerror e) { } @override public void onerror(dialogerror e) { } @override public void oncomplete(bundle values) { } @override public void oncancel() { } }); } /** * function show access tokens * */ public void showaccesstokens() { string access_token = facebook.getaccesstoken(); toast.maketext(getapplicationcontext(), "access token: " + access_token, toast.length_long).show(); } /** * function logout user facebook * */ public void logoutfromfacebook() { masyncrunner.logout(this, new requestlistener() { @override public void oncomplete(string response, object state) { log.d("logout facebook", response); if (boolean.parseboolean(response) == true) { runonuithread(new runnable() { @override public void run() { // make login button visible btnfblogin.setvisibility(view.visible); // making remaining buttons invisible btnfbgetprofile.setvisibility(view.invisible); btnposttowall.setvisibility(view.invisible); btnshowaccesstokens.setvisibility(view.invisible); logout.setvisibility(view.invisible); } }); } } @override public void onioexception(ioexception e, object state) { } @override public void onfilenotfoundexception(filenotfoundexception e, object state) { } @override public void onmalformedurlexception(malformedurlexception e, object state) { } @override public void onfacebookerror(facebookerror e, object state) { } }); }
i found solution question
i replaced facebook sdk 3.0 3.2 .
also changed code facebook login.
now logout , login working fine.
mainactivity.java
public class mainactivity extends fragmentactivity { string tag="mainactivity"; private static final string permission = "publish_actions"; //private static final string permission = "email"; private final string pending_action_bundle_key = "pending_action"; private button poststatusupdatebutton; private loginbutton loginbutton; private profilepictureview profilepictureview; private textview greeting; private pendingaction pendingaction = pendingaction.none; private graphuser user; private graphplace place; private list<graphuser> tags; private boolean canpresentsharedialog; button logoutbutton,pro; /* private static final list<string> permission = arrays.aslist( "email","publish_actions");*/ private enum pendingaction { none, post_status_update } private uilifecyclehelper uihelper; private session.statuscallback callback = new session.statuscallback() { @override public void call(session session, sessionstate state, exception exception) { onsessionstatechange(session, state, exception); } }; private facebookdialog.callback dialogcallback = new facebookdialog.callback() { @override public void onerror(facebookdialog.pendingcall pendingcall, exception error, bundle data) { log.d(tag, string.format("error: %s", error.tostring())); } @override public void oncomplete(facebookdialog.pendingcall pendingcall, bundle data) { log.d(tag, "success!"); } }; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); uihelper = new uilifecyclehelper(this, callback); uihelper.oncreate(savedinstancestate); // can present share dialog regular links? canpresentsharedialog = facebookdialog.canpresentsharedialog(this,facebookdialog.sharedialogfeature.share_dialog); if (savedinstancestate != null) { string name = savedinstancestate.getstring(pending_action_bundle_key); pendingaction = pendingaction.valueof(name); } setcontentview(r.layout.activity_main); loginbutton = (loginbutton) findviewbyid(r.id.login_button); loginbutton.setuserinfochangedcallback(new loginbutton.userinfochangedcallback() { @override public void onuserinfofetched(graphuser user) { mainactivity.this.user = user; updateui(); // it's possible waiting this.user // populated in order post status update. handlependingaction(); } }); profilepictureview = (profilepictureview) findviewbyid(r.id.profilepicture); greeting = (textview) findviewbyid(r.id.greeting); poststatusupdatebutton = (button) findviewbyid(r.id.poststatusupdatebutton); poststatusupdatebutton.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { performpublish(pendingaction.post_status_update,canpresentsharedialog); } }); logoutbutton=(button)findviewbyid(r.id.logoutbutton); logoutbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { //callfacebooklogout(session); logout(); } }); } //override lifecycle methods uilifecyclehelper know state of activity @override protected void onresume() { super.onresume(); uihelper.onresume(); updateui(); } @override protected void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); uihelper.onsaveinstancestate(outstate); outstate.putstring(pending_action_bundle_key, pendingaction.name()); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); uihelper.onactivityresult(requestcode, resultcode, data, dialogcallback); } @override public void onpause() { super.onpause(); uihelper.onpause(); } @override public void ondestroy() { super.ondestroy(); uihelper.ondestroy(); } private void onsessionstatechange(session session, sessionstate state,exception exception) { if (state.isopened()) { toast.maketext(getapplicationcontext(), "user logged in...", toast.length_short).show(); getuserdata(session,state); } else if (state.isclosed()) { toast.maketext(getapplicationcontext(), "user logged out...", toast.length_short).show(); } if (pendingaction != pendingaction.none && (exception instanceof facebookoperationcanceledexception || exception instanceof facebookauthorizationexception)) { new alertdialog.builder(mainactivity.this)//if permission not granted .settitle(r.string.cancelled) .setmessage(r.string.permission_not_granted) .setpositivebutton(r.string.ok, null).show(); pendingaction = pendingaction.none; } else if (state == sessionstate.opened_token_updated) { handlependingaction(); } updateui(); } private void updateui() { session session = session.getactivesession(); boolean enablebuttons = (session != null && session.isopened()); poststatusupdatebutton.setenabled(enablebuttons || canpresentsharedialog); if (enablebuttons && user != null) { profilepictureview.setprofileid(user.getid()); greeting.settext(getstring(r.string.hello_user, user.getfirstname())); } else { profilepictureview.setprofileid(null); greeting.settext(null); } } @suppresswarnings("incomplete-switch") private void handlependingaction() { pendingaction previouslypendingaction = pendingaction; // these actions may re-set pendingaction if still pending, assume // succeed. pendingaction = pendingaction.none; switch (previouslypendingaction) { case post_status_update: poststatusupdate(); break; } } private interface graphobjectwithid extends graphobject { string getid(); } private void showpublishresult(string message, graphobject result, facebookrequesterror error) { string title = null; string alertmessage = null; if (error == null) { title = getstring(r.string.success); string id = result.cast(graphobjectwithid.class).getid(); alertmessage = getstring(r.string.successfully_posted_post, message, id); } else { title = getstring(r.string.error); alertmessage = error.geterrormessage(); } new alertdialog.builder(this).settitle(title).setmessage(alertmessage) .setpositivebutton(r.string.ok, null).show(); } // create sample post update on facebook private facebookdialog.sharedialogbuilder createsharedialogbuilderforlink() { return new facebookdialog.sharedialogbuilder(this) .setname("hello facebook") .setdescription("this sample post androidsrc.net demonstrate facebook login in android application") .setlink("http://androidsrc.net/"); } private void poststatusupdate() { if (canpresentsharedialog) { facebookdialog sharedialog = createsharedialogbuilderforlink().build(); uihelper.trackpendingdialogcall(sharedialog.present()); } else if (user != null && haspublishpermission()) { final string message = getstring(r.string.status_update, user.getfirstname(), (new date().tostring())); request request = request.newstatusupdaterequest( session.getactivesession(), message, place, tags, new request.callback() { @override public void oncompleted(response response) { showpublishresult(message, response.getgraphobject(), response.geterror()); } }); request.executeasync(); } else { pendingaction = pendingaction.post_status_update; } } //check if app has permission publish on facebook private boolean haspublishpermission() { session session = session.getactivesession(); return session != null && session.getpermissions().contains("publish_actions")&&session.getpermissions().contains("email"); } private void performpublish(pendingaction action, boolean allownosession) { session session = session.getactivesession(); if (session != null) { pendingaction = action; if (haspublishpermission()) { // can action right away. handlependingaction(); return; } else if (session.isopened()) { // need new permissions, complete action when // called back. session.requestnewpublishpermissions(new session.newpermissionsrequest( this, permission)); return; } } if (allownosession) { pendingaction = action; handlependingaction(); } } public void logout() { if (session.getactivesession() != null) { session.getactivesession().closeandcleartokeninformation(); toast.maketext(getapplicationcontext(), "logged out...", toast.length_short).show(); } session.setactivesession(null); } private void getuserdata(session session, sessionstate state) { if (state.isopened()) { request.newmerequest(session, new request.graphusercallback() { @override public void oncompleted(graphuser user, response response) { if (response != null) { try { string name = user.getname(); // if asked email permission string email = (string) user.getproperty("email"); // log.e(log_tag, "name: " + name + " email: " + email); toast.maketext(getapplicationcontext(), "name: " + name + " email: " + email, toast.length_short).show(); } catch (exception e) { e.printstacktrace(); //log.d(log_tag, "exception e"); } } } }).executeasync(); } }
my logout method
public void logout() { if (session.getactivesession() != null) { session.getactivesession().closeandcleartokeninformation(); toast.maketext(getapplicationcontext(), "logged out...", toast.length_short).show(); } session.setactivesession(null); }
link reffered
Comments
Post a Comment