首先做好准备工作http://wiki.open.qq.com/wiki/%E9%A6%96%E9%A1%B5 去官网下载最新sdk.
第一步
<activity android:name="com.tencent.tauth.AuthActivity" android:launchMode="singleTask" android:noHistory="true"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="tencent222222" />//这是腾讯给的测试号,自己申请的直接替换,注意替换的时候不要把tencent替换了,只替换2222 intent-filter> activity> <activity android:name="com.tencent.connect.common.AssistActivity" android:configChanges="orientation|keyboardHidden" android:screenOrientation="behind" android:theme="@android:style/Theme.Translucent.NoTitleBar" /> application>加入到自己的AndroidManifest.xml配置中。
第二步
//在activity中重写onActivityResult
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { Tencent.onActivityResultData(requestCode, resultCode, data, new BaseUiListener()); }
第三步开始初始化并发起登陆请求
//qq登陆 private void qqLogin() { showProgressDialog("请稍后"); qqApi = Tencent.createInstance(Constants.QQ_APP_ID, App.getInstance());//Constants.QQ_APP_ID="222222";官方的测试后期自己替换//Session 是否有效
if (!qqApi.isSessionValid()) {
qqApi.login(this, "all", new BaseUiListener());
} else {//有效就先退出再登陆
qqApi.logout(this);
qqApi.login(this, "all", new BaseUiListener());
}
}//baseUiListener
//qq授权登陆与获取用户信息 private class BaseUiListener implements IUiListener { @Override public void onComplete(Object response) { hideProgress(); try { showToast("登陆成功"); String openidString = ((JSONObject) response).getString("openid"); String access_token = ((JSONObject) response).getString("access_token"); String expires = ((JSONObject) response).getString("expires_in"); Log.e("//", openidString + "onComplete: " + response.toString()); qqApi.setAccessToken(access_token, expires); qqApi.setOpenId(openidString); QQToken token = qqApi.getQQToken(); userInfo = new UserInfo(LoginActivity.this, token);//这个UserInfo类是sdk封装好的 userInfo.getUserInfo(new IUiListener() {//获取用户信息 @Override public void onComplete(Object response) {
//这里获取qq个人信息成功,发送到handler解析 Message message = new Message(); message.what = QQ_INFO; message.obj = response; handler.sendMessage(message); } @Override public void onError(UiError uiError) { } @Override public void onCancel() { } }); } catch (JSONException e) { e.printStackTrace(); } } @Override public void onError(UiError uiError) { hideProgress(); showToast("授权失败"); } @Override public void onCancel() { hideProgress(); showToast("已取消"); } }第四步(最后一步)
private final int QQ_INFO = 1024;//qq个人信息
Handler handler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case QQ_INFO:
JSONObject response = (JSONObject) msg.obj; Gson gson = new Gson(); qqUserInEntity = gson.fromJson(String.valueOf(response), QQUserInfoEntity.class);//QQUserInfoEntity.class 根据获取的QQ信息建的beanbreak; } }};
//这是授权成功与获取个人信息打印的logo
E/haha: 82C65DB9B33D25087AE5A25C563BA7E0onComplete: {"ret":0,"pay_token":"2AC075C33D3052C13B589925FCE12041","pf":"desktop_m_qq-10000144-android-2002-","query_authority_cost":688,"authority_cost":0,"openid":"82C65DB9B33D25087AE5A25C563BA7E0","expires_in":7776000,"pfkey":"e2c799337db9503d294cc5b4bad75e5c","msg":"","access_token":"E70EAF33130549785D8D917ABFB569C8","login_cost":732} E/haha: onComplete: {"is_yellow_year_vip":"0","ret":0,"figureurl_qq_1":"http:\/\/q.qlogo.cn\/qqapp\/222222\/82C65DB9B33D25087AE5A25C563BA7E0\/40","figureurl_qq_2":"http:\/\/q.qlogo.cn\/qqapp\/222222\/82C65DB9B33D25087AE5A25C563BA7E0\/100","nickname":"以前…现在…未来…","yellow_vip_level":"0","is_lost":0,"msg":"","city":"达州","figureurl_1":"http:\/\/qzapp.qlogo.cn\/qzapp\/222222\/82C65DB9B33D25087AE5A25C563BA7E0\/50","vip":"0","level":"0","figureurl_2":"http:\/\/qzapp.qlogo.cn\/qzapp\/222222\/82C65DB9B33D25087AE5A25C563BA7E0\/100","province":"四川","is_yellow_vip":"0","gender":"男","figureurl":"http:\/\/qzapp.qlogo.cn\/qzapp\/222222\/82C65DB9B33D25087AE5A25C563BA7E0\/30"}