private void authorize(Platform plat) { showDialog("正在调起授权"); if (plat == null) { // popupOthers(); return; } /** 清除之前的账户 */ if (plat.isValid()) { plat.removeAccount(); } // 判断指定平台是否已经完成授权 // if (plat.isAuthValid()) { // String userId = plat.getDb().getUserId(); // if (userId != null) { // handler.sendEmptyMessage(MSG_USERID_FOUND); //// login(plat.getName(), userId, null); // return; // } // } plat.setPlatformActionListener(this); // true不使用SSO授权,false使用SSO授权 plat.SSOSetting(false); // 获取用户资料 plat.showUser(null); }
@Override public void onClick(View v) { ShareSDK.initSDK(LoginActivity.this); Platform weibo = null; switch (v.getId()) { case R.id.iv_weixin: weibo = ShareSDK.getPlatform(Wechat.NAME); break; case R.id.iv_qq: weibo = ShareSDK.getPlatform(LoginActivity.this, QQ.NAME); break; case R.id.iv_weibo: weibo = ShareSDK.getPlatform(SinaWeibo.NAME); break; case R.id.iv_kongjian: weibo = ShareSDK.getPlatform(QZone.NAME); break; } weibo.setPlatformActionListener( new PlatformActionListener() { @Override public void onComplete(Platform platform, int action, HashMap<String, Object> res) { LogUtil.e("授权成功" + res.toString()); String id, name, description, profile_image_url; // id=res.get("id").toString();//ID name = res.get("nickname").toString(); // 用户名 // description=res.get("description").toString();//描述 // profile_image_url=res.get("profile_image_url").toString();//头像链接 // String str="ID: "+id+";\n"+ // "用户名: "+name+";\n"+ // "描述:"+description+";\n"+ // "用户头像地址:"+profile_image_url; LogUtil.e("用户资料: " + name); // 通过打印res数据看看有哪些数据是你想要的 if (action == Platform.ACTION_USER_INFOR) { PlatformDb platDB = platform.getDb(); // 获取数平台数据DB // 通过DB获取各种数据 String token = platDB.getToken(); platDB.getUserGender(); platDB.getUserIcon(); String userId = platDB.getUserId(); platDB.getUserName(); LogUtil.e("userId=" + userId + "##token=" + token); } Intent intent = getIntent(); intent.putExtra("name", name); setResult(3, intent); finish(); } @Override public void onError(Platform platform, int i, Throwable throwable) { LogUtil.e("授权失败" + throwable); } @Override public void onCancel(Platform platform, int i) { LogUtil.e("授权取消"); } }); weibo.authorize(); // 移除授权 // weibo.removeAccount(true); weibo.showUser(null); // 执行登录,登录后在回调里面获取用户资料 // weibo.showUser(“3189087725”);//获取账号为“3189087725”的资料 }