Android调用第三方QQ登录代码分享
本文为大家分享了调用QQ登录的相关代码,希望对大家有帮助,减少项目开发的时间,具体内容如下
1、去QQ开放平台注册帐号(http://open.qq.com/),为应用申请QQ的APP_ID , 并下载相关的jar包,放到工程的lib目录下。
2、在Manifest.xml里注册QQ相关的Activity,代码如下
<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> </activity>
3、利用QQ提供的相关API去获取相关的OPENID,昵称,图像等资料。这里有一点要注意的是,由于QQ服务器返回OPENID和昵称等其他资料是分两步进行的,而首先返回的OPENID速度很快,返回昵称等其他资料可能会比较慢,所以在返回OPENID的时候,就可以认为已经调用QQ登录成功,再用异步的方式处理昵称等其他资料信息。以下是代码部 分,已经过项目测试,可直接用(替换相应的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登陆。 * @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 这里要替换成自己申请的APP_ID Tencent 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; } @Override public 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(); } } @Override public void onError(UiError arg0) { qqLoginListener.onLoginFail(); } @Override public void onCancel() { qqLoginListener.onCancel(); } } class BaseApiListener implements IRequestListener{ QqLoginListener qqLoginListener = null; public BaseApiListener(QqLoginListener qqLoginListener) { super(); this.qqLoginListener = qqLoginListener; } @Override public 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); //发送广播到主页去更新 BroadcastUtil.sendBroadcast(BroadcastAction.UPLOAD_THIRD_INFO); } catch (Exception e) { e.printStackTrace(); } } @Override public void onConnectTimeoutException(ConnectTimeoutException arg0, Object arg1) { } @Override public void onHttpStatusException(HttpStatusException arg0, Object arg1) { } @Override public void onIOException(IOException arg0, Object arg1) { } @Override public void onJSONException(JSONException arg0, Object arg1) { } @Override public void onMalformedURLException(MalformedURLException arg0, Object arg1) { } @Override public void onNetworkUnavailableException( NetworkUnavailableException arg0, Object arg1) { } @Override public void onSocketTimeoutException(SocketTimeoutException arg0, Object arg1) { } @Override public void onUnknowException(Exception arg0, Object arg1) { } } /** * 授权完成后,执行一下步的回调 */ public static abstract class QqLoginListener{ public void onLoginSuccess(String openid){}; public void onLoginSuccess(String openid, String nickname){}; public void onLoginFail(){}; public void onCancel(){}; }
好了,到里代码部分已经写完,只需要调用login(Activity context , QqLoginListener loginListener)方法就可以了。
以上就是Android调用第三方登录的详细步骤和思路,希望对大家的学习有所帮助。
赞 (0)