Android Call QQ Login

Source: Internet
Author: User

Call QQ Login
In the current project development, call third-party login, is almost necessary, and call QQ login is also essential, here the relevant code to share out, hope to take it can be used, reduce the project development time, hope to everyone useful.

1, go to QQ open Platform Registration account (http://open.qq.com/), apply for the application of QQ app_id, and download the relevant jar package, put in the project Lib directory.

2, in the manifest.xml registered QQ related activity, the code is as follows
        <activity        android:name= "com.tencent.connect.common.AssistActivity"        android:screenorientation= " Portrait "        android:theme=" @android: Style/theme.translucent.notitlebar "/>                <activity        android: Name= "com.tencent.tauth.AuthActivity"        android:launchmode= "Singletask"        android:nohistory= "true" >            <intent-filter>                <action android:name= "Android.intent.action.VIEW"/>                <category Android:name= "Android.intent.category.DEFAULT"/>                <category android:name= " Android.intent.category.BROWSABLE "/>                <data android:scheme=" tencent100491239 "/>            </ Intent-filter>        

3, using the relevant APIs provided by QQ to obtain relevant OpenID, nicknames, images and other information. One thing to note here is that because the QQ server returns OpenID and nicknames and other information is carried out in two steps, and the first return of the OpenID fast, return nickname and other information may be relatively slow, so when returning to OpenID, you can think that has been called QQ login success, and then use Asynchronously handles other information such as nicknames. The following is the Code section, which has been tested by the project and can be used directly (replace the corresponding app_id)
Import Java.io.ioexception;import Java.net.malformedurlexception;import Java.net.sockettimeoutexception;import Org.apache.http.conn.connecttimeoutexception;import Org.json.jsonexception;import Org.json.JSONObject;import Android.app.activity;import Com.smart.util.broadcastaction;import Com.smart.util.broadcastutil;import Com.smart.util.constant;import Com.smart.util.ilog;import Com.smart.util.prefutil;import Com.smart.util.Prefkey; Import Com.tencent.open.httpstatusexception;import Com.tencent.open.networkunavailableexception;import Com.tencent.tauth.constants;import Com.tencent.tauth.irequestlistener;import Com.tencent.tauth.IUiListener; Import Com.tencent.tauth.tencent;import com.tencent.tauth.uierror;import com.utils.lib.ss.common.toasthelper;/** * QQ Landing. * @author E */public class Qqlogin {private static Qqlogin Qqlogin = Null;public static Qqlogin getinstance () {if (null = = Qqlogin) {qqlogin = new Qqlogin ();} return qqlogin;} public void Login (Activity context, Qqloginlistener loginlistEner) {//constant.qq_app_id here to replace their own application of app_idtencent Tencent = Tencent.createinstance (constant.qq_app_id, context); if (null = = Tencent) {Toasthelper.maketext (context, "Error cased!"); return;} Tencent.login (Context, "Get_user_info", New Baseuilistener (Tencent,loginlistener)); Class Baseuilistener implements iuilistener{tencent Tencent = null; Qqloginlistener Qqloginlistener = Null;public Baseuilistener (Tencent Tencent, Qqloginlistener QqLoginListener) {super ( ); this.tencent = Tencent;this.qqloginlistener = Qqloginlistener;} @Overridepublic void OnComplete (Jsonobject values) {try {String OpenID = values.getstring ("OpenID"); Qqloginlistener.onloginsuccess (OpenID); Tencent.requestasync (Constants.graph_simple_user_info, NULL, Constants.http_get, New Baseapilistener (Qqloginlistener), null);} catch (Exception e) {e.printstacktrace ();}} @Overridepublic void OnError (Uierror arg0) {qqloginlistener.onloginfail ();} @Overridepublic void OnCancel () {qqloginlistener.oncancel ();}} Class Baseapilistener ImpLements irequestlistener{Qqloginlistener qqloginlistener = null;public Baseapilistener (QqLoginListener Qqloginlistener) {super (); this.qqloginlistener = Qqloginlistener;} @Overridepublic void OnComplete (Jsonobject jsonobject, Object object) {try {ilog.e (jsonobject.tostring ()); String nickname = jsonobject.getstring ("nickname"); String figureurl_qq_1 = jsonobject.getstring ("figureurl_qq_2"); Prefutil.instance (). Setpref (Prefkey.third_nickname, nickname); Prefutil.instance (). Setpref (Prefkey.third_image, figureurl_qq_1);//Send Broadcast to home page to update broadcastutil.sendbroadcast ( Broadcastaction.upload_third_info);} catch (Exception e) {e.printstacktrace ();}} @Overridepublic void Onconnecttimeoutexception (connecttimeoutexception arg0,object arg1) {} @Overridepublic void Onhttpstatusexception (httpstatusexception arg0, Object arg1) {} @Overridepublic void Onioexception (IOException arg0, Object arg1) {} @Overridepublic void Onjsonexception (jsonexception arg0, Object arg1) {} @Overridepublic void Onmalformedurlexception (MalFormedurlexception arg0,object arg1) {} @Overridepublic void Onnetworkunavailableexception ( Networkunavailableexception arg0, Object arg1) {} @Overridepublic void Onsockettimeoutexception ( Sockettimeoutexception arg0,object arg1) {} @Overridepublic void Onunknowexception (Exception arg0, Object arg1) {}}/** * granted Right after completion, perform a step callback */public static abstract class Qqloginlistener{public void Onloginsuccess (String OpenID) {};p ublic void Onloginsuccess (String OpenID, string nickname) {};p ublic void Onloginfail () {};p ublic void OnCancel () {};}

OK, in the Code section has been written, only need to call the login (Activity context, Qqloginlistener Loginlistener) method is OK.

Android Call QQ Login

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.