android QQ账号登陆第三方应用

来源:互联网 发布:淘宝电商培训 编辑:程序博客网 时间:2024/06/10 09:48
官网:http://wiki.open.qq.com/wiki/mobile/Android_SDK使用说明
 
这里的第三方应用指的当然是我们自己开发的应用。

腾讯开发平台是一个比较大的开放平台,它包括了腾讯微博开发平台,微信平台等诸多平台,而我们所需要用到的东西,都在QQ互联开放平台。之所以在一开始就说明这一点是因为,之前因为没搞清楚他们之间的关系,走了不少弯路,所以希望大家能够注意到。

1. 在开发之前,首先要使用QQ号登录平台,然后完善开发者信息成为开发者,之后,要在管理中心,创建你的应用,其中包括完善很多关于你的应用的息。(关于这部分的详细信息,请查看官方在线文档开发者注册和android应用注册)。

 2.当我们成功创建 了应用之后,会获得APP ID,这个ID在开发中是必须的。

3.QQ互联SDK,前往该网页,可以下载官方的sdk资料,其中 包括了jar文件,demo,以及详细的开发说明文档。

  4.(警醒)开发的第一步,首先要把所需要的jar文件导入到我们的工程中去,在上一步下载的SDK文档中,我们会发现有一个open_sdk.jar文件,按照说明文档导入进去,但是我们会发现在说明文档中分明提到了2个jar文件,腾讯开发平台SDK在这个链接中下载到相应的文件之后,我们会发现它和之前下载的QQ互联文件几乎一样,区别在于开发说明文档在细节上的区别,以及JAR文件的不同。我们刚才提到的所缺少的一个jar文件,在这就可以找到了。但是需要特别注意的是,有些人一看,好像2个jar文件都在这里,那就直接把这2个导入进去开发吧!恭喜你成功掉入陷阱了!我们仔细对比会发现,在腾讯SDK和QQ互联sdk中的open_sdk.jar,他们的大小是不一样的,这2个文件是有区别的。撸主也是在这里纠结了很久,代码完成后没有提示错误,但是运行老是崩掉,后来发现这个问题之后,一阵无语。
    简言之,将QQ互联sdk中的open_sdk.jar以及腾讯sdk中的mta_sdk_XXX.jar导入到工程之后,可以按照开发文档,继续往下进行。

  5.关于AndroidManifest的配置,开发文档比较详尽,不赘述。



开工前期的准备:

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

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

给个链接吧:

        下载


配置清单:

       1.添加权限:

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <uses-permission android:name="android.permission.INTERNET" />  
  2.    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  

      2.添加活动:

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


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


布局:

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns: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.   
  7.     <TextView  
  8.         android:id="@+id/user_nickname"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="昵称" />  
  12.   
  13.     <ImageView  
  14.         android:id="@+id/user_logo"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content" />  
  17.   
  18.     <Button  
  19.         android:id="@+id/new_login_btn"  
  20.         android:layout_width="match_parent"  
  21.         android:layout_height="wrap_content"  
  22.         android:text="登录" />  
  23.   
  24.     <TextView  
  25.         android:id="@+id/user_callback"  
  26.         android:layout_width="wrap_content"  
  27.         android:layout_height="wrap_content"  
  28.         android:text="返回消息" />  

活动的详细代码:

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. /**  
  2.  * 测试qq第三方登录功能  
  3.  *   
  4.  */  
  5. public class TestQQ extends Activity implements OnClickListener {  
  6.     private TextView mUserInfo;  
  7.     private ImageView mUserLogo;  
  8.     private Button mNewLoginButton;  
  9.     private TextView backInfo;  
  10.   
  11.     private UserInfo mInfo;  
  12.     private Tencent mTencent;  
  13.     public QQAuth mQQAuth;  
  14.     // 申请的id  
  15.     public String mAppid = "222222";  
  16.   
  17.     @Override  
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.acy_testqq);  
  21.         initView();  
  22.     }  
  23.   
  24.     public void initView() {  
  25.         mUserInfo = (TextView) findViewById(R.id.user_nickname);  
  26.         mUserLogo = (ImageView) findViewById(R.id.user_logo);  
  27.         mNewLoginButton = (Button) findViewById(R.id.new_login_btn);  
  28.         mNewLoginButton.setOnClickListener(this);  
  29.         backInfo = (TextView) findViewById(R.id.user_callback);  
  30.         // Tencent类是SDK的主要实现类,通过此访问腾讯开放的OpenAPI。  
  31.         mQQAuth = QQAuth.createInstance(mAppid, this.getApplicationContext());  
  32.         // 实例化  
  33.         mTencent = Tencent.createInstance(mAppid, this);  
  34.     }  
  35.   
  36.     Handler mHandler = new Handler() {  
  37.   
  38.         @Override  
  39.         public void handleMessage(Message msg) {  
  40.             if (msg.what == 0) {  
  41.                 mUserInfo.setVisibility(android.view.View.VISIBLE);  
  42.                 mUserInfo.setText(msg.getData().getString("nickname"));  
  43.             } else if (msg.what == 1) {  
  44.                 Bitmap bitmap = (Bitmap) msg.obj;  
  45.                 mUserLogo.setImageBitmap(bitmap);  
  46.                 mUserLogo.setVisibility(android.view.View.VISIBLE);  
  47.             }  
  48.         }  
  49.     };  
  50.   
  51.     private void updateUserInfo() {  
  52.         if (mQQAuth != null && mQQAuth.isSessionValid()) {  
  53.             IUiListener listener = new IUiListener() {  
  54.                 @Override  
  55.                 public void onError(UiError e) {  
  56.                     // TODO Auto-generated method stub  
  57.                 }  
  58.   
  59.                 @Override  
  60.                 public void onComplete(final Object response) {  
  61.                     JSONObject json = (JSONObject) response;  
  62.                     // 昵称  
  63.                     Message msg = new Message();  
  64.                     String nickname = null;  
  65.                     try {  
  66.                         nickname = ((JSONObject) response)  
  67.                                 .getString("nickname");  
  68.                     } catch (JSONException e) {  
  69.                         // TODO Auto-generated catch block  
  70.                         e.printStackTrace();  
  71.                     }  
  72.                     msg.getData().putString("nickname", nickname);  
  73.                     msg.what = 0;  
  74.                     mHandler.sendMessage(msg);  
  75.                     // 头像  
  76.                     String path;  
  77.                     try {  
  78.                         path = json.getString("figureurl_qq_2");  
  79.                         MyImgThread imgThread = new MyImgThread(path);  
  80.                         Thread thread = new Thread(imgThread);  
  81.                         thread.start();  
  82.                     } catch (JSONException e1) {  
  83.                         // TODO Auto-generated catch block  
  84.                         e1.printStackTrace();  
  85.                     }  
  86.                 }  
  87.   
  88.                 @Override  
  89.                 public void onCancel() {  
  90.                     // TODO Auto-generated method stub  
  91.   
  92.                 }  
  93.             };  
  94.             // MainActivity.mTencent.requestAsync(Constants.GRAPH_SIMPLE_USER_INFO,  
  95.             // null,  
  96.             // Constants.HTTP_GET, requestListener, null);  
  97.             mInfo = new UserInfo(this, mQQAuth.getQQToken());  
  98.             mInfo.getUserInfo(listener);  
  99.   
  100.         } else {  
  101.             // mUserInfo.setText("");  
  102.             // mUserInfo.setVisibility(android.view.View.GONE);  
  103.             // mUserLogo.setVisibility(android.view.View.GONE);  
  104.         }  
  105.     }  
  106.   
  107.     /**  
  108.      * 开启线程 获取头像  
  109.      */  
  110.     class MyImgThread implements Runnable {  
  111.         private String imgPath;  
  112.         private Bitmap bitmap;  
  113.   
  114.         public MyImgThread(String imgpath) {  
  115.             this.imgPath = imgpath;  
  116.         }  
  117.   
  118.         @Override  
  119.         public void run() {  
  120.             // TODO Auto-generated method stub  
  121.             bitmap = getImgBitmap(imgPath);  
  122.             Message msg = new Message();  
  123.             msg.obj = bitmap;  
  124.             msg.what = 1;  
  125.             mHandler.sendMessage(msg);  
  126.         }  
  127.     }  
  128.   
  129.     /**  
  130.      * 根据头像的url 获取bitmap  
  131.      */  
  132.     public Bitmap getImgBitmap(String imageUri) {  
  133.         // 显示网络上的图片  
  134.         Bitmap bitmap = null;  
  135.         HttpURLConnection conn = null;  
  136.         InputStream is = null;  
  137.         try {  
  138.             URL myFileUrl = new URL(imageUri);  
  139.             conn = (HttpURLConnection) myFileUrl.openConnection();  
  140.             conn.setDoInput(true);  
  141.             conn.connect();  
  142.   
  143.             is = conn.getInputStream();  
  144.             bitmap = BitmapFactory.decodeStream(is);  
  145.             is.close();  
  146.         } catch (IOException e) {  
  147.             e.printStackTrace();  
  148.             return null;  
  149.         } finally {  
  150.             try {  
  151.                 conn.disconnect();  
  152.                 is.close();  
  153.                 is.reset();  
  154.             } catch (IOException e) {  
  155.                 // TODO Auto-generated catch block  
  156.                 e.printStackTrace();  
  157.             }  
  158.         }  
  159.         return bitmap;  
  160.     }  
  161.   
  162.     public void onClickLogin() {  
  163.         // 登录  
  164.         if (!mQQAuth.isSessionValid()) {  
  165.             // 实例化回调接口  
  166.             IUiListener listener = new BaseUiListener() {  
  167.                 @Override  
  168.                 protected void doComplete(JSONObject values) {  
  169.                     updateUserInfo();  
  170.                     // updateLoginButton();  
  171.                     if (mQQAuth != null) {  
  172.                         mNewLoginButton.setTextColor(Color.BLUE);  
  173.                         mNewLoginButton.setText("登录");  
  174.                     }  
  175.                 }  
  176.             };  
  177.             // "all": 所有权限,listener: 回调的实例  
  178.             // mQQAuth.login(this, "all", listener);  
  179.   
  180.             // 这版本登录是使用的这种方式,后面的几个参数是啥意思 我也没查到  
  181.             mTencent.loginWithOEM(this, "all", listener, "10000144",  
  182.                     "10000144", "xxxx");  
  183.         } else {  
  184.             // 注销登录  
  185.             mQQAuth.logout(this);  
  186.             updateUserInfo();  
  187.   
  188.             // updateLoginButton();  
  189.             mNewLoginButton.setTextColor(Color.RED);  
  190.             mNewLoginButton.setText("退出帐号");  
  191.         }  
  192.     }  
  193.   
  194.     /**  
  195.      * 调用SDK封装好的借口,需要传入回调的实例 会返回服务器的消息  
  196.      */  
  197.     private class BaseUiListener implements IUiListener {  
  198.         /**  
  199.          * 成功  
  200.          */  
  201.         @Override  
  202.         public void onComplete(Object response) {  
  203.             backInfo.setText(response.toString());  
  204.             doComplete((JSONObject) response);  
  205.         }  
  206.   
  207.         /**  
  208.          * 处理返回的消息 比如把json转换为对象什么的  
  209.          *   
  210.          * @param values  
  211.          */  
  212.         protected void doComplete(JSONObject values) {  
  213.   
  214.         }  
  215.   
  216.         @Override  
  217.         public void onError(UiError e) {  
  218.             Toast.makeText(TestQQ.this, e.toString(), 1000).show();  
  219.         }  
  220.   
  221.         @Override  
  222.         public void onCancel() {  
  223.             Toast.makeText(TestQQ.this, "cancel", 1000).show();  
  224.         }  
  225.     }  
  226.   
  227.     @Override  
  228.     public void onClick(View v) {  
  229.         // TODO Auto-generated method stub  
  230.         // 当点击登录按钮  
  231.         if (v == mNewLoginButton) {  
  232.             onClickLogin();  
  233.         }  
  234.     }  
  235.   
  236. }  

测试:

        1.运行的开始界面:

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

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

       4.获取成功:


注意:

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

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

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. // 是否支持sso登录  
  2.             if (mTencent.isSupportSSOLogin(this)) {  
  3.                 onClickLogin();  
  4.             }  
当支持的时候,就返回真,否则返回假。
0 0
原创粉丝点击