QQ的第三方登录

因为,开发SDK的版本更新比较快,阅读的童鞋注意点吧。


开工前期的准备:

1.建议你首先去下载最新的SDK,那里面除了有案例外,还有必须的jar包。

2.最好在qq的开发平台自己注册个账号,那样移植起来更容易点。

给个链接吧:

下载


配置清单:

1.添加权限:

[html]view plaincopyprint?
  1. <uses-permissionandroid:name="android.permission.INTERNET"/>
  2. <uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE"/>

2.添加活动:

[html]view plaincopyprint?
  1. <activity
  2. android:name="com.tencent.tauth.AuthActivity"
  3. android:launchMode="singleTask"
  4. android:noHistory="true">
  5. <intent-filter>
  6. <actionandroid:name="android.intent.action.VIEW"/>
  7. <categoryandroid:name="android.intent.category.DEFAULT"/>
  8. <categoryandroid:name="android.intent.category.BROWSABLE"/>
  9. <dataandroid:scheme="tencent222222"/><!--100380359100381104222222-->
  10. </intent-filter>
  11. </activity>
  12. <activity
  13. android:name="com.tencent.connect.common.AssistActivity"
  14. android:screenOrientation="portrait"
  15. android:theme="@android:style/Theme.Translucent.NoTitleBar"/>
在tencent后面添加自己的应用id,222222是腾讯给的专用测试id。


顺便提醒一句,在这个版本中要导入两个jar包。


布局:

[html]view plaincopyprint?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6. <TextView
  7. android:id="@+id/user_nickname"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:text="昵称"/>
  11. <ImageView
  12. android:id="@+id/user_logo"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"/>
  15. <Button
  16. android:id="@+id/new_login_btn"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:text="登录"/>
  20. <TextView
  21. android:id="@+id/user_callback"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:text="返回消息"/>

活动的详细代码:

[html]view plaincopyprint?
  1. /**
  2. *测试qq第三方登录功能
  3. *
  4. */
  5. publicclassTestQQextendsActivityimplementsOnClickListener{
  6. privateTextViewmUserInfo;
  7. privateImageViewmUserLogo;
  8. privateButtonmNewLoginButton;
  9. privateTextViewbackInfo;
  10. privateUserInfomInfo;
  11. privateTencentmTencent;
  12. publicQQAuthmQQAuth;
  13. //申请的id
  14. publicStringmAppid="222222";
  15. @Override
  16. protectedvoidonCreate(BundlesavedInstanceState){
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.acy_testqq);
  19. initView();
  20. }
  21. publicvoidinitView(){
  22. mUserInfo=(TextView)findViewById(R.id.user_nickname);
  23. mUserLogo=(ImageView)findViewById(R.id.user_logo);
  24. mNewLoginButton=(Button)findViewById(R.id.new_login_btn);
  25. mNewLoginButton.setOnClickListener(this);
  26. backInfo=(TextView)findViewById(R.id.user_callback);
  27. //Tencent类是SDK的主要实现类,通过此访问腾讯开放的OpenAPI。
  28. mQQAuth=QQAuth.createInstance(mAppid,this.getApplicationContext());
  29. //实例化
  30. mTencent=Tencent.createInstance(mAppid,this);
  31. }
  32. HandlermHandler=newHandler(){
  33. @Override
  34. publicvoidhandleMessage(Messagemsg){
  35. if(msg.what==0){
  36. mUserInfo.setVisibility(android.view.View.VISIBLE);
  37. mUserInfo.setText(msg.getData().getString("nickname"));
  38. }elseif(msg.what==1){
  39. Bitmapbitmap=(Bitmap)msg.obj;
  40. mUserLogo.setImageBitmap(bitmap);
  41. mUserLogo.setVisibility(android.view.View.VISIBLE);
  42. }
  43. }
  44. };
  45. privatevoidupdateUserInfo(){
  46. if(mQQAuth!=null&&mQQAuth.isSessionValid()){
  47. IUiListenerlistener=newIUiListener(){
  48. @Override
  49. publicvoidonError(UiErrore){
  50. //TODOAuto-generatedmethodstub
  51. }
  52. @Override
  53. publicvoidonComplete(finalObjectresponse){
  54. JSONObjectjson=(JSONObject)response;
  55. //昵称
  56. Messagemsg=newMessage();
  57. Stringnickname=null;
  58. try{
  59. nickname=((JSONObject)response)
  60. .getString("nickname");
  61. }catch(JSONExceptione){
  62. //TODOAuto-generatedcatchblock
  63. e.printStackTrace();
  64. }
  65. msg.getData().putString("nickname",nickname);
  66. msg.what=0;
  67. mHandler.sendMessage(msg);
  68. //头像
  69. Stringpath;
  70. try{
  71. path=json.getString("figureurl_qq_2");
  72. MyImgThreadimgThread=newMyImgThread(path);
  73. Threadthread=newThread(imgThread);
  74. thread.start();
  75. }catch(JSONExceptione1){
  76. //TODOAuto-generatedcatchblock
  77. e1.printStackTrace();
  78. }
  79. }
  80. @Override
  81. publicvoidonCancel(){
  82. //TODOAuto-generatedmethodstub
  83. }
  84. };
  85. //MainActivity.mTencent.requestAsync(Constants.GRAPH_SIMPLE_USER_INFO,
  86. //null,
  87. //Constants.HTTP_GET,requestListener,null);
  88. mInfo=newUserInfo(this,mQQAuth.getQQToken());
  89. mInfo.getUserInfo(listener);
  90. }else{
  91. //mUserInfo.setText("");
  92. //mUserInfo.setVisibility(android.view.View.GONE);
  93. //mUserLogo.setVisibility(android.view.View.GONE);
  94. }
  95. }
  96. /**
  97. *开启线程获取头像
  98. */
  99. classMyImgThreadimplementsRunnable{
  100. privateStringimgPath;
  101. privateBitmapbitmap;
  102. publicMyImgThread(Stringimgpath){
  103. this.imgPath=imgpath;
  104. }
  105. @Override
  106. publicvoidrun(){
  107. //TODOAuto-generatedmethodstub
  108. bitmap=getImgBitmap(imgPath);
  109. Messagemsg=newMessage();
  110. msg.obj=bitmap;
  111. msg.what=1;
  112. mHandler.sendMessage(msg);
  113. }
  114. }
  115. /**
  116. *根据头像的url获取bitmap
  117. */
  118. publicBitmapgetImgBitmap(StringimageUri){
  119. //显示网络上的图片
  120. Bitmapbitmap=null;
  121. HttpURLConnectionconn=null;
  122. InputStreamis=null;
  123. try{
  124. URLmyFileUrl=newURL(imageUri);
  125. conn=(HttpURLConnection)myFileUrl.openConnection();
  126. conn.setDoInput(true);
  127. conn.connect();
  128. is=conn.getInputStream();
  129. bitmap=BitmapFactory.decodeStream(is);
  130. is.close();
  131. }catch(IOExceptione){
  132. e.printStackTrace();
  133. returnnull;
  134. }finally{
  135. try{
  136. conn.disconnect();
  137. is.close();
  138. is.reset();
  139. }catch(IOExceptione){
  140. //TODOAuto-generatedcatchblock
  141. e.printStackTrace();
  142. }
  143. }
  144. returnbitmap;
  145. }
  146. publicvoidonClickLogin(){
  147. //登录
  148. if(!mQQAuth.isSessionValid()){
  149. //实例化回调接口
  150. IUiListenerlistener=newBaseUiListener(){
  151. @Override
  152. protectedvoiddoComplete(JSONObjectvalues){
  153. updateUserInfo();
  154. //updateLoginButton();
  155. if(mQQAuth!=null){
  156. mNewLoginButton.setTextColor(Color.BLUE);
  157. mNewLoginButton.setText("登录");
  158. }
  159. }
  160. };
  161. //"all":所有权限,listener:回调的实例
  162. //mQQAuth.login(this,"all",listener);
  163. //这版本登录是使用的这种方式,后面的几个参数是啥意思我也没查到
  164. mTencent.loginWithOEM(this,"all",listener,"10000144",
  165. "10000144","xxxx");
  166. }else{
  167. //注销登录
  168. mQQAuth.logout(this);
  169. updateUserInfo();
  170. //updateLoginButton();
  171. mNewLoginButton.setTextColor(Color.RED);
  172. mNewLoginButton.setText("退出帐号");
  173. }
  174. }
  175. /**
  176. *调用SDK封装好的借口,需要传入回调的实例会返回服务器的消息
  177. */
  178. privateclassBaseUiListenerimplementsIUiListener{
  179. /**
  180. *成功
  181. */
  182. @Override
  183. publicvoidonComplete(Objectresponse){
  184. backInfo.setText(response.toString());
  185. doComplete((JSONObject)response);
  186. }
  187. /**
  188. *处理返回的消息比如把json转换为对象什么的
  189. *
  190. *@paramvalues
  191. */
  192. protectedvoiddoComplete(JSONObjectvalues){
  193. }
  194. @Override
  195. publicvoidonError(UiErrore){
  196. Toast.makeText(TestQQ.this,e.toString(),1000).show();
  197. }
  198. @Override
  199. publicvoidonCancel(){
  200. Toast.makeText(TestQQ.this,"cancel",1000).show();
  201. }
  202. }
  203. @Override
  204. publicvoidonClick(Viewv){
  205. //TODOAuto-generatedmethodstub
  206. //当点击登录按钮
  207. if(v==mNewLoginButton){
  208. onClickLogin();
  209. }
  210. }
  211. }

测试:

1.运行的开始界面:

2.当你的手机没用安装qq的时候,会跳转到网页qq注册界面:

3.如果手机上有qq客户端:

4.获取成功:


注意:

1.因为我使用的是腾讯给的测试接口id,如果你也是使用的测试接口的话,那么记得把应用的名字改为: “open_sample”。

2.在进行登录的时候,可以进行判断是否适合sso登录。

[html]view plaincopyprint?
  1. //是否支持sso登录
  2. if(mTencent.isSupportSSOLogin(this)){
  3. onClickLogin();
  4. }
当支持的时候,就返回真,否则返回假。

更多相关文章

  1. Android Studio 2.2 Espresso Test Recorder-----解放双手,通过录
  2. "AndroidStudio 单元测试"-最简单最快的方法-ApplicationTest
  3. 编译和测试android的驱动程序学习笔记
  4. android-----关于事件分发机制中几点困惑测试
  5. Java的单元测试(Junit)
  6. 根据给出的语料库,训练n-gram模型。根据训练出的模型,判断测试集中
  7. 探秘腾讯Android手机游戏平台之不安装游戏APK直接启动法(转)
  8. 集成腾讯Bugly异常监测平台
  9. osmdroid 6.0.1基础测试

随机推荐

  1. 一起学android之EditText的各种使用(15)
  2. Android -- 设置textview文字居中或者控
  3. 在Android模拟器安装应用
  4. Android开发常用代码片段(三)
  5. Kotlin Anko Layout+MVP(Glide,Retrofit,
  6. APIDEMO GRIDVIEW
  7. Android(安卓)读取内存文件返回byte数组
  8. #AndroidDevSummit,就在此刻!
  9. Android TextView多行文本滚动实现
  10. Android shape 绘制图形的实例详解