QQ的第三方登錄

 

QQ的第三方登錄

因爲,開發SDK的版本更新比較快,閱讀的童鞋注意點吧。


開工前期的準備:

        1.建議你首先去下載最新的SDK,那裏面除了有案例外,還有必須的jar包。

         2.最好在qq的開發平臺自己註冊個賬號,那樣移植起來更容易點。

給個鏈接吧:

        下載


配置清單:

       1.添加權限:

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

      2.添加活動:

[html] view plain copy
 print?
  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 plain copy
 print?
  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 plain copy
 print?
  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 plain copy
 print?
  1. // 是否支持sso登錄  
  2.             if (mTencent.isSupportSSOLogin(this)) {  
  3.                 onClickLogin();  
  4.             }  
當支持的時候,就返回真,否則返回假。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章