功能分类:其他
支持平台:Android
运行环境:Eclipse
开发语言:Java
开发工具:Eclipse
源码大小:1.31MB
源码下载地址:http://down.51cto.com/data/1976923
源码简介
最近使用第三方登录,QQ登录官方给的demo中类太多了,这里上传一个小demo,只有2个类,可以授权QQ进行第三方登录。
源码运行截图
授权登录界面
授权成功返回值
主页面
源码片段
- Handler mHandler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- if (msg.what == 0) {
- JSONObject response = (JSONObject) msg.obj;
- if (response.has("nickname")) {
- try {
- mUserInfo.setVisibility(android.view.View.VISIBLE);
- mUserInfo.setText(response.getString("nickname"));
- } catch (JSONException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- } else if (msg.what == 1) {
- Bitmap bitmap = (Bitmap) msg.obj;
- mUserLogo.setImageBitmap(bitmap);
- mUserLogo.setVisibility(android.view.View.VISIBLE);
- }
- }
- };
- private void onClickLogin() {
- if (!mQQAuth.isSessionValid()) {
- IUiListener listener = new BaseUiListener() {
- @Override
- protected void doComplete(JSONObject values) {
- updateUserInfo();
- updateLoginButton();
- }
- };
- mQQAuth.login(this, "all", listener);
- // mTencent.loginWithOEM(this, "all",
- // listener,"10000144","10000144","xxxx");
- mTencent.login(this, "all", listener);
- } else {
- mQQAuth.logout(this);
- updateUserInfo();
- updateLoginButton();
- }
- }
- public static boolean ready(Context context) {
- if (mQQAuth == null) {
- return false;
- }
- boolean ready = mQQAuth.isSessionValid()
- && mQQAuth.getQQToken().getOpenId() != null;
- if (!ready)
- Toast.makeText(context, "login and get openId first, please!",
- Toast.LENGTH_SHORT).show();
- return ready;
- }
- private class BaseUiListener implements IUiListener {
- @Override
- public void onComplete(Object response) {
- Util.showResultDialog(MainActivity.this, response.toString(),
- "登录成功");
- doComplete((JSONObject) response);
- }
- protected void doComplete(JSONObject values) {
- }
- @Override
- public void onError(UiError e) {
- Util.toastMessage(MainActivity.this, "onError: " + e.errorDetail);
- Util.dismissDialog();
- }
- @Override
- public void onCancel() {
- Util.toastMessage(MainActivity.this, "onCancel: ");
- Util.dismissDialog();
- }
- }