Android实现QQ登录功能
程序员文章站
2023-12-18 19:21:40
qq登录是一个非常简单的一个第三方应用,现在,我们就来实现一个qq登录
首先下载两个jar包 这里上传不了jar包,所以可以到我的github中下...
qq登录是一个非常简单的一个第三方应用,现在,我们就来实现一个qq登录
首先下载两个jar包 这里上传不了jar包,所以可以到我的github中下载工程中libs中的两个jar包
网址:
打代码前,先把这些东西写上:
private static final string tag = "mainactivity"; private static final string app_id = "1105602574";//官方获取的appid private tencent mtencent; private baseuilistener miuilistener; private userinfo muserinfo;
在androidmanifest.xml中加入权限
<uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.access_network_state" />
<!-- 注册sdkactivity --> <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="tencent1105602574" /> <!-- 开放平台获取的appid --> </intent-filter> </activity> <activity android:name="com.tencent.connect.common.assistactivity" android:theme="@android:style/theme.translucent.notitlebar" android:screenorientation="portrait"/>
xml布局中的
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.bwei.czx.czx0914qq.mainactivity"> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登录" android:id="@+id/login"/> <textview android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/name"/> <imageview android:layout_width="100dp" android:layout_height="80dp" android:id="@+id/img"/> </linearlayout>
下面开始mainactivity中的代码
package com.bwei.czx.czx0914qq; import android.content.intent; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.util.log; import android.view.view; import android.widget.button; import android.widget.imageview; import android.widget.textview; import android.widget.toast; import com.nostra13.universalimageloader.core.imageloader; import com.nostra13.universalimageloader.core.imageloaderconfiguration; import com.tencent.connect.userinfo; import com.tencent.connect.auth.qqtoken; import com.tencent.connect.common.constants; import com.tencent.tauth.iuilistener; import com.tencent.tauth.tencent; import com.tencent.tauth.uierror; import org.json.jsonexception; import org.json.jsonobject; public class mainactivity extends appcompatactivity { private static final string tag = "mainactivity"; private static final string app_id = "1105602574";//官方获取的appid private tencent mtencent; private baseuilistener miuilistener; private userinfo muserinfo; private button login; private textview name; private imageview img; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //传入参数appid和全局context上下文 mtencent = tencent.createinstance(app_id, mainactivity.this.getapplicationcontext()); initview(); login.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { miuilistener = new baseuilistener(); //all表示获取所有权限 mtencent.login(mainactivity.this,"all", miuilistener); muserinfo = new userinfo(mainactivity.this, mtencent.getqqtoken()); //获取用户信息 muserinfo.getuserinfo(miuilistener); } }); } private void initview() { login = (button) findviewbyid(r.id.login); name = (textview) findviewbyid(r.id.name); img = (imageview) findviewbyid(r.id.img); } /** * 自定义监听器实现iuilistener接口后,需要实现的3个方法 * oncomplete完成 onerror错误 oncancel取消 */ private class baseuilistener implements iuilistener { @override public void oncomplete(object response) { toast.maketext(mainactivity.this, "授权成功", toast.length_short).show(); log.e(tag, "response:" + response); jsonobject obj = (jsonobject) response; try { string openid = obj.getstring("openid"); string accesstoken = obj.getstring("access_token"); string expires = obj.getstring("expires_in"); mtencent.setopenid(openid); mtencent.setaccesstoken(accesstoken,expires); qqtoken qqtoken = mtencent.getqqtoken(); muserinfo = new userinfo(getapplicationcontext(),qqtoken); muserinfo.getuserinfo(new iuilistener() { @override public void oncomplete(object response) { log.e(tag,"登录成功"+response.tostring()); if(response == null){ return; } try { jsonobject jo = (jsonobject) response; toast.maketext(mainactivity.this, "登录成功", toast.length_long).show(); string nickname = jo.getstring("nickname"); string figureurl_1= jo.getstring("figureurl_1"); name.settext(nickname); imageloaderconfiguration configuration = new imageloaderconfiguration.builder(mainactivity.this).build(); imageloader.getinstance().init(configuration); imageloader.getinstance().displayimage(figureurl_1,img); } catch (exception e) { // todo: handle exception } } @override public void onerror(uierror uierror) { log.e(tag,"登录失败"+uierror.tostring()); } @override public void oncancel() { log.e(tag,"登录取消"); } }); } catch (jsonexception e) { e.printstacktrace(); } } @override public void onerror(uierror uierror) { toast.maketext(mainactivity.this, "授权失败", toast.length_short).show(); } @override public void oncancel() { toast.maketext(mainactivity.this, "授权取消", toast.length_short).show(); } } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { if(requestcode == constants.request_login){ tencent.onactivityresultdata(requestcode,resultcode,data,miuilistener); } super.onactivityresult(requestcode, resultcode, data); } }
下面为显示效果
qq登录完成!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。