Android第三方登录之腾讯QQ登录的实例代码
程序员文章站
2022-06-29 12:06:34
布局文件
布局文件
<textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登录成功" android:textsize="25sp" android:layout_margintop="100dp" />
清单文件中的配置
<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="tencent1106163416" /> <!-- 开放平台获取的appid --> </intent-filter> </activity>
代码
public class mainactivity extends appcompatactivity { private static final string tag = "mainactivity"; private static final string app_id = "1106163416";//官方获取的appid private tencent mtencent; private baseuilistener miuilistener; private userinfo muserinfo; private button button; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //传入参数appid和全局context上下文 mtencent = tencent.createinstance(app_id,mainactivity.this.getapplicationcontext()); } public void buttonlogin(view v){ /**通过这句代码,sdk实现了qq的登录,这个方法有三个参数,第一个参数是context上下文,第二个参数scopo 是一个string类型的字符串,表示一些权限 官方文档中的说明:应用需要获得哪些api的权限,由“,”分隔。例如:scope = “get_user_info,add_t”;所有权限用“all” 第三个参数,是一个事件监听器,iuilistener接口的实例,这里用的是该接口的实现类 */ miuilistener = new baseuilistener(); //all表示获取所有权限 mtencent.login(mainactivity.this,"all", miuilistener); } /** * 自定义监听器实现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) { intent inten = new intent(mainactivity.this,nextactivity.class); startactivity(inten); log.e(tag,"登录成功"+response.tostring()); } @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(); } } /** * 在调用login的activity或者fragment中重写onactivityresult方法 * @param requestcode * @param resultcode * @param data */ @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); } }
要添加的权限
<uses-permission android:name="android.permission.internet"></uses-permission> <uses-permission android:name="android.permission.access_network_state"></uses-permission>
以上所述是小编给大家介绍的android第三方登录之腾讯qq登录的实例代码,希望对大家有所帮助