public class MainActivity extends ParentActivity { /** QQ初始化类*/ private Tencent mQQTencent; /** QQ授权登录回调监听*/ private BaseUiListener mBaseUiListener; /** QQ授权登录之后获取用户信息*/ privateUserInfo mUserInfo;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.custom_layout);
//QQ SDK初始化 mQQTencent= Tencent.createInstance("1106341755",this); //初始化监听 mBaseUiListener= new BaseUiListener(); /** * QQ SDK实现授权登录 * 第一个参数:上下文 * 第二个参数:获取jar中的权限 all:jar包所有权限 * 第三个参数:数据回调监听 */ mQQTencent.login(this,"all",mBaseUiListener);
}
/** * 必须在这个方法里面添加回调,否则获取不到数据 * 但是测试过这里的监听没有返回数据,真正的回调数据在login()方法里面的监听数据里 * 联系QQ客服,居然说没有技术支持 */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { //添加回调接收:测试过这里的监听没有返回数据,貌似就是个跳转回跳,真正的回调数据在login()方法里面的监听数据里 Tencent.onActivityResultData(requestCode,resultCode,data,new BaseUiListener()); super.onActivityResult(requestCode, resultCode, data); }
/** * 自定义QQ授权登录监听 */ private class BaseUiListener implements IUiListener{
@Override public void onComplete(Object obj) { Toast.makeText(MainActivity.this,"授权成功",Toast.LENGTH_SHORT).show(); //解析QQ授权登录基础数据 JSONObject object = (JSONObject)obj; Log.e("LKing","json = "+object.toString()); //结果: // {"ret":0,"openid":"690318232C185C0CFBB34C***", // "access_token":"64EA773D7521218E8AFC3134***", // "pay_token":"A6186F3E6CC29AF696***", // "expires_in":777***,"pf":"desktop_m_qq-10000144-***", // "pfkey":"c6e73ba69433e0a0d63f5***", // "msg":"","login_cost":119,"query_authority_cost":204, // "authority_cost":14217} try{ String openID = object.getString("openid"); String accessToken = object.getString("access_token"); String expires = object.getString("expires_in");
//获取用户信息 mQQTencent.setOpenId(openID); mQQTencent.setAccessToken(accessToken,expires); QQToken qqToken = mQQTencent.getQQToken(); mUserInfo = new UserInfo(getApplicationContext(),qqToken); mUserInfo.getUserInfo(new IUiListener() { @Override public void onComplete(Object o) { Log.e("LKing","用户消息= "+o.toString()); //结果: // {"ret":0,"msg":"","is_lost":0,"nickname":"LKing", // "gender":"男","province":"陕西","city":"西安", // "figureurl":"http:\/\/qzapp.qlogo.cn\/qzapp\/1106341755\/690318232C185C0CFBB34C85A08F3775\/30", // "figureurl_1":"http:\/\/qzapp.qlogo.cn\/qzapp\/1106341755\/690318232C185C0CFBB34C85A08F3775\/50", // "figureurl_2":"http:\/\/qzapp.qlogo.cn\/qzapp\/1106341755\/690318232C185C0CFBB34C85A08F3775\/100", // "figureurl_qq_1":"http:\/\/q.qlogo.cn\/qqapp\/1106341755\/690318232C185C0CFBB34C85A08F3775\/40", // "figureurl_qq_2":"http:\/\/q.qlogo.cn\/qqapp\/1106341755\/690318232C185C0CFBB34C85A08F3775\/100", // "is_yellow_vip":"0","vip":"0","yellow_vip_level":"0","level":"0","is_yellow_year_vip":"0"} }
@Override public void onError(UiError uiError) { Log.e("LKing","用户消息获取失败"); }
@Override public void onCancel() { Log.e("LKing","用户消息获取取消"); } }); }catch(Exception e){ Toast.makeText(MainActivity.this,"授权失败,Json解析出错",Toast.LENGTH_SHORT).show(); } }
@Override public void onError(UiError uiError) { Toast.makeText(MainActivity.this,"授权失败",Toast.LENGTH_SHORT).show(); }
@Override public void onCancel() { Toast.makeText(MainActivity.this,"取消授权",Toast.LENGTH_SHORT).show();
} } @Override protected void onDestroy() { super.onDestroy(); mQQTencent.logout(this); } } |