android 第三方登录 --- QQ

1,jar包

2,配置AndroidManifest.xml

 1 <activity android:name=".login.LoginActivity"
 2           android:configChanges="orientation|screenSize|smallestScreenSize|keyboard|keyboardHidden|navigation">
 3     <intent-filter>
 4         <action android:name="android.intent.action.MAIN"/>
 5         <category android:name="android.intent.category.LAUNCHER"/>
 6     </intent-filter>
 7 </activity>
 8 <activity
 9     android:name="com.tencent.tauth.AuthActivity"
10     android:launchMode="singleTask"
11     android:noHistory="true" >
12     <intent-filter>
13         <action android:name="android.intent.action.VIEW" />
14         <category android:name="android.intent.category.DEFAULT" />
15         <category android:name="android.intent.category.BROWSABLE" />
16         <data android:scheme="tencent222222" />
17         <!-- 将222222换成自己的appid -->
18     </intent-filter>
19 </activity>
20 
21 <activity
22     android:name="com.tencent.connect.common.AssistActivity"
23     android:configChanges="orientation|keyboardHidden"
24     android:screenOrientation="behind"
25     android:theme="@android:style/Theme.Translucent.NoTitleBar" />

3,LoginActivity调用

 1 //222222换成自己的appid
 2 Tencent mTencent = Tencent.createInstance("222222", this.getApplicationContext());
 3 //all获取全部
 4 mTencent.login(this, "all", new IUiListener() {
 5     @Override
 6     public void onComplete(Object o) {
 7         Log.i("返回",o.toString());
 8     }
 9     @Override
10     public void onError(UiError uiError) {
11     }
12     @Override
13     public void onCancel() {
14     }
15 });
16 //注销
17 //mTencent.logout(this);

4,回调

 1 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 2     if (requestCode == Constants.REQUEST_LOGIN){
 3         if(resultCode == Constants.ACTIVITY_OK){
 4             Log.i("onActivityResult",data.getStringExtra("key_action"));
 5             //如果少了这句,监听器的没效果。onComplete不会执行
 6             Tencent.handleResultData(data,loginListener);
 7         }
 8     }
 9     super.onActivityResult(requestCode, resultCode, data);
10 }

5,以上是授权获取openid。下边就是获取授权用户的基本信息了。授权成功后将onComplete(Object o)返回的数据设置到mTencent里

 1 try {
 2     String token = object.getString(Constants.PARAM_ACCESS_TOKEN);
 3     String expires = object.getString(Constants.PARAM_EXPIRES_IN);
 4     String openId = object.getString(Constants.PARAM_OPEN_ID);
 5     //设置token
 6     mTencent.setAccessToken(token, expires);
 7     //设置openid
 8     mTencent.setOpenId(openId);
 9 } catch (JSONException e) {
10     e.printStackTrace();
11 }

6,拿到当前授权QQ号的昵称,头像之类的基本信息

1 UserInfo userInfo = new UserInfo(this,mTencent.getQQToken());
2  //和上边授权的一样
3  userInfo.getUserInfo(new IUiListener(){...});

 

posted @ 2016-01-26 14:45  西瓜皮不甜  阅读(365)  评论(0编辑  收藏  举报