android facebook sdk 3.0 login and get user info -
i'm developing app using needs few facebook info register own website.
at first using facebook sdk 1.5, working well. until facebook says they'll stop supporting old(v2.0 , below) sdk since june 2013.
so have upgrade 3.0, , re-write code.
here's have done far after read facebook's official tutorial :
manifst.xml
<!-- stuff --> <uses-permission android:name="android.permission.internet" /> <meta-data android:name="com.facebook.sdk.applicationid" android:value="@string/fbappid"/> <activity android:name="com.facebook.loginactivity" android:label="@string/app_name" />
strings.xml
<!-- stuff --> <string name="fbappid">1234567890</string> <!-- know id fake, right ? -->
code
public class loginfragment extends sherlockfragment { private uilifecyclehelper uihelper; private session.statuscallback callback=new session.statuscallback() { @override public void call(final session session, final sessionstate state, final exception exception) { onsessionstatechange(session, state, exception); } }; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view=inflater.inflate(r.layout.login_fragment, container, false); loginbutton authbutton=(loginbutton)view.findviewbyid(r.id.btnlogin); authbutton.setfragment(this); authbutton.setreadpermissions(arrays.aslist("basic_info", "user_status", "email")); return view; } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); uihelper=new uilifecyclehelper(getactivity(), callback); uihelper.oncreate(savedinstancestate); } @override public void onresume() { super.onresume(); // scenarios main activity launched , user // session not null, session state change notification // may not triggered. trigger if it's open/closed. session session=session.getactivesession(); if(session!=null&&(session.isopened()||session.isclosed())) { onsessionstatechange(session, session.getstate(), null); } uihelper.onresume(); } @override public void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); uihelper.onactivityresult(requestcode, resultcode, data); } @override public void onpause() { super.onpause(); uihelper.onpause(); } @override public void ondestroy() { super.ondestroy(); uihelper.ondestroy(); } @override public void onsaveinstancestate(bundle outstate) { try { super.onsaveinstancestate(outstate); setuservisiblehint(true); uihelper.onsaveinstancestate(outstate); } catch(exception e) { e.printstacktrace(); log.w(constants.tag, "error on save instance state, "+e.tostring()); } } private void onsessionstatechange(session session, sessionstate state, exception exception) { if(state.isopened()) //i don't know why state not opened......? { request request=request.newmerequest(session, new request.graphusercallback() { @override public void oncompleted(graphuser user, response response) { if(user!=null) { log.i(constants.tag, "logged in..."); log.d("user fname", user.getfirstname()); log.d("user lname", user.getlastname()); log.d("user username", user.getusername()); log.d("user email", (string) response.getgraphobject().getproperty("email")); } else { log.i(constants.tag, "logged out..."); } } }); request.executebatchasync(request); } } }
when run app, can see facebook login button.
press it, dialog shows ask me allow app access facebook info, email...etc.
and after press yes, there nothing happen, nothing comes out in logcat...
how know user logged in facebook, , access info ?
you should override onactivityresult method , call
authbutton.onactivityresult(params);