QQ的第三方登录
16lz
2021-01-22
QQ的第三方登录
因为,开发SDK的版本更新比较快,阅读的童鞋注意点吧。
开工前期的准备:
1.建议你首先去下载最新的SDK,那里面除了有案例外,还有必须的jar包。
2.最好在qq的开发平台自己注册个账号,那样移植起来更容易点。
给个链接吧:
下载
配置清单:
1.添加权限:
[html]view plaincopyprint?- <uses-permissionandroid:name="android.permission.INTERNET"/>
- <uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE"/>
2.添加活动:
[html]view plaincopyprint?
- <activity
- android:name="com.tencent.tauth.AuthActivity"
- android:launchMode="singleTask"
- android:noHistory="true">
- <intent-filter>
- <actionandroid:name="android.intent.action.VIEW"/>
- <categoryandroid:name="android.intent.category.DEFAULT"/>
- <categoryandroid:name="android.intent.category.BROWSABLE"/>
- <dataandroid:scheme="tencent222222"/><!--100380359100381104222222-->
- </intent-filter>
- </activity>
- <activity
- android:name="com.tencent.connect.common.AssistActivity"
- android:screenOrientation="portrait"
- android:theme="@android:style/Theme.Translucent.NoTitleBar"/>
顺便提醒一句,在这个版本中要导入两个jar包。
布局:
[html]view plaincopyprint?- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <TextView
- android:id="@+id/user_nickname"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="昵称"/>
- <ImageView
- android:id="@+id/user_logo"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- <Button
- android:id="@+id/new_login_btn"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="登录"/>
- <TextView
- android:id="@+id/user_callback"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="返回消息"/>
活动的详细代码:
[html]view plaincopyprint?- /**
- *测试qq第三方登录功能
- *
- */
- publicclassTestQQextendsActivityimplementsOnClickListener{
- privateTextViewmUserInfo;
- privateImageViewmUserLogo;
- privateButtonmNewLoginButton;
- privateTextViewbackInfo;
- privateUserInfomInfo;
- privateTencentmTencent;
- publicQQAuthmQQAuth;
- //申请的id
- publicStringmAppid="222222";
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.acy_testqq);
- initView();
- }
- publicvoidinitView(){
- mUserInfo=(TextView)findViewById(R.id.user_nickname);
- mUserLogo=(ImageView)findViewById(R.id.user_logo);
- mNewLoginButton=(Button)findViewById(R.id.new_login_btn);
- mNewLoginButton.setOnClickListener(this);
- backInfo=(TextView)findViewById(R.id.user_callback);
- //Tencent类是SDK的主要实现类,通过此访问腾讯开放的OpenAPI。
- mQQAuth=QQAuth.createInstance(mAppid,this.getApplicationContext());
- //实例化
- mTencent=Tencent.createInstance(mAppid,this);
- }
- HandlermHandler=newHandler(){
- @Override
- publicvoidhandleMessage(Messagemsg){
- if(msg.what==0){
- mUserInfo.setVisibility(android.view.View.VISIBLE);
- mUserInfo.setText(msg.getData().getString("nickname"));
- }elseif(msg.what==1){
- Bitmapbitmap=(Bitmap)msg.obj;
- mUserLogo.setImageBitmap(bitmap);
- mUserLogo.setVisibility(android.view.View.VISIBLE);
- }
- }
- };
- privatevoidupdateUserInfo(){
- if(mQQAuth!=null&&mQQAuth.isSessionValid()){
- IUiListenerlistener=newIUiListener(){
- @Override
- publicvoidonError(UiErrore){
- //TODOAuto-generatedmethodstub
- }
- @Override
- publicvoidonComplete(finalObjectresponse){
- JSONObjectjson=(JSONObject)response;
- //昵称
- Messagemsg=newMessage();
- Stringnickname=null;
- try{
- nickname=((JSONObject)response)
- .getString("nickname");
- }catch(JSONExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }
- msg.getData().putString("nickname",nickname);
- msg.what=0;
- mHandler.sendMessage(msg);
- //头像
- Stringpath;
- try{
- path=json.getString("figureurl_qq_2");
- MyImgThreadimgThread=newMyImgThread(path);
- Threadthread=newThread(imgThread);
- thread.start();
- }catch(JSONExceptione1){
- //TODOAuto-generatedcatchblock
- e1.printStackTrace();
- }
- }
- @Override
- publicvoidonCancel(){
- //TODOAuto-generatedmethodstub
- }
- };
- //MainActivity.mTencent.requestAsync(Constants.GRAPH_SIMPLE_USER_INFO,
- //null,
- //Constants.HTTP_GET,requestListener,null);
- mInfo=newUserInfo(this,mQQAuth.getQQToken());
- mInfo.getUserInfo(listener);
- }else{
- //mUserInfo.setText("");
- //mUserInfo.setVisibility(android.view.View.GONE);
- //mUserLogo.setVisibility(android.view.View.GONE);
- }
- }
- /**
- *开启线程获取头像
- */
- classMyImgThreadimplementsRunnable{
- privateStringimgPath;
- privateBitmapbitmap;
- publicMyImgThread(Stringimgpath){
- this.imgPath=imgpath;
- }
- @Override
- publicvoidrun(){
- //TODOAuto-generatedmethodstub
- bitmap=getImgBitmap(imgPath);
- Messagemsg=newMessage();
- msg.obj=bitmap;
- msg.what=1;
- mHandler.sendMessage(msg);
- }
- }
- /**
- *根据头像的url获取bitmap
- */
- publicBitmapgetImgBitmap(StringimageUri){
- //显示网络上的图片
- Bitmapbitmap=null;
- HttpURLConnectionconn=null;
- InputStreamis=null;
- try{
- URLmyFileUrl=newURL(imageUri);
- conn=(HttpURLConnection)myFileUrl.openConnection();
- conn.setDoInput(true);
- conn.connect();
- is=conn.getInputStream();
- bitmap=BitmapFactory.decodeStream(is);
- is.close();
- }catch(IOExceptione){
- e.printStackTrace();
- returnnull;
- }finally{
- try{
- conn.disconnect();
- is.close();
- is.reset();
- }catch(IOExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }
- }
- returnbitmap;
- }
- publicvoidonClickLogin(){
- //登录
- if(!mQQAuth.isSessionValid()){
- //实例化回调接口
- IUiListenerlistener=newBaseUiListener(){
- @Override
- protectedvoiddoComplete(JSONObjectvalues){
- updateUserInfo();
- //updateLoginButton();
- if(mQQAuth!=null){
- mNewLoginButton.setTextColor(Color.BLUE);
- mNewLoginButton.setText("登录");
- }
- }
- };
- //"all":所有权限,listener:回调的实例
- //mQQAuth.login(this,"all",listener);
- //这版本登录是使用的这种方式,后面的几个参数是啥意思我也没查到
- mTencent.loginWithOEM(this,"all",listener,"10000144",
- "10000144","xxxx");
- }else{
- //注销登录
- mQQAuth.logout(this);
- updateUserInfo();
- //updateLoginButton();
- mNewLoginButton.setTextColor(Color.RED);
- mNewLoginButton.setText("退出帐号");
- }
- }
- /**
- *调用SDK封装好的借口,需要传入回调的实例会返回服务器的消息
- */
- privateclassBaseUiListenerimplementsIUiListener{
- /**
- *成功
- */
- @Override
- publicvoidonComplete(Objectresponse){
- backInfo.setText(response.toString());
- doComplete((JSONObject)response);
- }
- /**
- *处理返回的消息比如把json转换为对象什么的
- *
- *@paramvalues
- */
- protectedvoiddoComplete(JSONObjectvalues){
- }
- @Override
- publicvoidonError(UiErrore){
- Toast.makeText(TestQQ.this,e.toString(),1000).show();
- }
- @Override
- publicvoidonCancel(){
- Toast.makeText(TestQQ.this,"cancel",1000).show();
- }
- }
- @Override
- publicvoidonClick(Viewv){
- //TODOAuto-generatedmethodstub
- //当点击登录按钮
- if(v==mNewLoginButton){
- onClickLogin();
- }
- }
- }
测试:
1.运行的开始界面:
2.当你的手机没用安装qq的时候,会跳转到网页qq注册界面:
3.如果手机上有qq客户端:
4.获取成功:
注意:
1.因为我使用的是腾讯给的测试接口id,如果你也是使用的测试接口的话,那么记得把应用的名字改为: “open_sample”。
2.在进行登录的时候,可以进行判断是否适合sso登录。
[html]view plaincopyprint?- //是否支持sso登录
- if(mTencent.isSupportSSOLogin(this)){
- onClickLogin();
- }
更多相关文章
- Android Studio 2.2 Espresso Test Recorder-----解放双手,通过录
- "AndroidStudio 单元测试"-最简单最快的方法-ApplicationTest
- 编译和测试android的驱动程序学习笔记
- android-----关于事件分发机制中几点困惑测试
- Java的单元测试(Junit)
- 根据给出的语料库,训练n-gram模型。根据训练出的模型,判断测试集中
- 探秘腾讯Android手机游戏平台之不安装游戏APK直接启动法(转)
- 集成腾讯Bugly异常监测平台
- osmdroid 6.0.1基础测试