コード例 #1
0
        @Override
        public void onReceive(Context context, Intent intent) {
          if (intent.getAction().equals("com.weibo.caigang.getverifier")) {
            weibo = WeiboContext.getInstance();
            Bundle bundle = intent.getExtras();
            String veryfier = bundle.getString("veryfier"); // 获取从授权页面返回的veryfier
            if (veryfier != null) {
              // unregisterReceiver(broadcastReceiver);
              weibo.getAccessToken(
                  weibo.getTokenKey(),
                  weibo.getTokenSecrect(),
                  veryfier); // 取得key和secret,这个key和secret非常重要,调腾讯的API全靠它了,神马新浪的,人人网的都一样的,不过还是有点区别,腾讯的OAuth认证是基于1.0的
              String userInfo =
                  weibo.getUserInfo(weibo.getAccessTokenKey(), weibo.getAccessTokenSecrect());
              try {
                JSONObject data = new JSONObject(userInfo).getJSONObject("data");
                String headUrl = null;
                if (data.getString("head") != null && !"".equals(data.getString("head"))) {
                  headUrl = data.getString("head") + "/100";
                }
                String userId = data.getString("name");
                String userName = data.getString("nick");

                UserInfo user = new UserInfo(); // 生成一个user对象保存到数据库
                user.setUserId(userId);
                user.setUserName(userName);
                user.setToken(weibo.getAccessTokenKey());
                user.setTokenSecret(weibo.getAccessTokenSecrect());

                Long insertId = 0L;
                dataHelper = DataBaseContext.getInstance(getApplicationContext());
                if (dataHelper.HaveUserInfo(userId)) { // 数据库已经存在了次用户
                  dataHelper.UpdateUserInfo(
                      userName, ImageUtil.getRoundBitmapFromUrl(headUrl, 15), userId);
                  // Toast.makeText(AccountActivity.this, "此用户已存在,如果你用户名或者头像已经改变,那么此操作将同步更新!",
                  // Toast.LENGTH_LONG).show();
                } else {
                  if (headUrl != null) {
                    insertId = dataHelper.SaveUserInfo(user, ImageUtil.getBytesFromUrl(headUrl));
                  } else {
                    insertId = dataHelper.SaveUserInfo(user, null);
                  }
                }
                if (insertId > 0L) {
                  // Toast.makeText(AccountActivity.this, "已经授权成功,将跳转到选择默认的登录用户界面!",
                  // Toast.LENGTH_LONG).show();
                }
                Log.d(TAG + "插入数据库的id是", insertId.toString());

                userList = dataHelper.GetUserList(false);
                adapater = new UserAdapater();
                adapater.notifyDataSetChanged(); // 刷新listview
                listView.setAdapter(adapater);
                listView.setOnItemClickListener(AccountActivity.this);

                SharedPreferences preferences =
                    getSharedPreferences("default_user", Activity.MODE_PRIVATE);
                String nick = preferences.getString("user_default_nick", ""); // 取得微博默认登录账号信息

                UserInfo defauUserInfo = null;

                if (userList != null && userList.size() > 0) {
                  if (nick != "") {
                    defauUserInfo = dataHelper.getUserByName(nick, userList); // 取得微博默认登录账号信息
                  }
                  if (defauUserInfo == null) {
                    defauUserInfo = userList.get(0);
                  }
                }
                if (defauUserInfo != null) {
                  currentUser = defauUserInfo;
                  user_default_headicon.setImageDrawable(defauUserInfo.getUserIcon());
                }

              } catch (JSONException e) {
                e.printStackTrace();
              } catch (Exception e) {
                e.printStackTrace();
              }
              Log.e(TAG, userInfo);
            }
            Log.e(TAG, veryfier);
          }
        }