這是我們為大家提供的一篇介紹多賬號管理是如何實現的文章,接下來就讓我們一起來了解一下吧!
列表中的賬號是保存在sqlite數據庫中的,下方的大圖片是顯示你選擇的默認賬號,雙擊圖片就會顯示此賬號的主頁
點擊添加賬號,將會跳向騰訊的授權頁面,這個頁面我是放在WebView中的,當授權成功後,騰訊的API將會返回給我們一個驗證碼,然後返回到我們的賬號管理界面。
Java代碼
public class AccountActivity extends ListActivity implements OnItemClickListener,OnItemLongClickListener,OnClickListener{
private final static String TAG="AccountActivity";
private DataHelper dataHelper;
private MyWeiboSync weibo;
private List userList;
private ListView listView;
private ImageView user_default_headicon;
private LinearLayout account_add_btn_bar;
private UserInfo currentUser;
private UserAdapater adapater;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.account);
setUpViews();//設置view
setUpListeners();//設置listenter
registerReceiver(broadcastReceiver, new IntentFilter("com.weibo.caigang.getverifier"));//注冊拿到驗證碼廣播接收器.
dataHelper = DataBaseContext.getInstance(getApplicationContext());//獲取數據庫連接類,用了單例,保證全局只有一個此對象。
userList = dataHelper.GetUserList(false);
SharedPreferences preferences = getSharedPreferences("default_user",Activity.MODE_PRIVATE);
String nick = preferences.getString("user_default_nick", "");//取得微博默認登錄賬號信息
UserInfo user = null;
if(userList!=null&&userList.size()>0){
if (nick != "") {
user = dataHelper.getUserByName(nick,userList);//取得微博默認登錄賬號信息
}
if(user == null) {
user = userList.get(0);
}
}
if(user!=null){
user_default_headicon.setImageDrawable(user.getUserIcon());
}
if(userList!=null&&userList.size()>0){
adapater = new UserAdapater();
listView.setAdapter(adapater);
listView.setOnItemClickListener(this);
}
}
private void setUpViews(){
listView = getListView();
user_default_headicon = (ImageView)findViewById(R.id.user_default_headicon);
account_add_btn_bar = (LinearLayout)findViewById(R.id.account_add_btn_bar);
}
private void setUpListeners(){
user_default_headicon.setOnClickListener(this);
account_add_btn_bar.setOnClickListener(this);
listView.setOnItemLongClickListener(this);
}
public class UserAdapater extends BaseAdapter{
@Override
public int getCount() {
return userList.size();
}
@Override
public Object getItem(int position) {
return userList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.account_list_item, null);
ImageView user_headicon = (ImageView) convertView.findViewById(R.id.user_headicon);
TextView user_nick = (TextView) convertView.findViewById(R.id.user_nick);
TextView user_name = (TextView) convertView.findViewById(R.id.user_name);
UserInfo user = userList.get(position);
try {
user_headicon.setImageDrawable(user.getUserIcon());
user_nick.setText(user.getUserName());
user_name.setText("@"+user.getUserId());
} catch (Exception e) {
e.printStackTrace();
}
return convertView;
}
}
@Override
public void onItemClick(AdapterView arg0, View arg1, int position, long arg3) {
currentUser = userList.get(position);
user_default_headicon.setImageDrawable(currentUser.getUserIcon());
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.account_add_btn_bar: {
weibo = WeiboContext.getInstance();//單例,保證整個應用只有一個weibo對象
weibo.getRequestToken();
Intent intent = new Intent(AccountActivity.this,AuthorizeActivity.class);
Bundle bundle = new Bundle();
bundle.putString("url", weibo.getAuthorizeUrl());
intent.putExtras(bundle);
startActivity(intent);//跳轉到騰訊的微博授權頁面,使用webview來顯示
}
break;
case R.id.user_default_headicon: {
SharedPreferences preferences = getSharedPreferences("default_user", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("user_default_nick", currentUser.getUserName());
editor.putString("user_default_name", currentUser.getUserId());
editor.commit();
Intent intent = new Intent(AccountActivity.this, MainActivity.class);
startActivity(intent);
}
break;
default:
break;
}
}
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@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;
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 = dataHelpe