Android騰訊微博客戶端開發三:多賬號管理的實現

先看看效果。。 
列表中的賬號是保存在sqlite數據庫中的,下方的大圖片是顯示你選擇的默認賬號,雙擊圖片就會顯示此賬號的主頁 

 

 

 


點擊添加賬號,將會跳向騰訊的授權頁面,這個頁面我是放在WebView中的,當授權成功後,騰訊的API將會返回給我們一個驗證碼,然後返回到我們的賬號管理界面。 


 
Java代碼  收藏代碼
  1. public class AccountActivity extends ListActivity implements OnItemClickListener,OnItemLongClickListener,OnClickListener{  
  2.       
  3.     private final static String TAG="AccountActivity";  
  4.     private DataHelper dataHelper;  
  5.     private MyWeiboSync weibo;  
  6.     private List<UserInfo> userList;  
  7.     private ListView listView;  
  8.     private ImageView user_default_headicon;  
  9.     private LinearLayout account_add_btn_bar;  
  10.     private UserInfo currentUser;  
  11.     private UserAdapater adapater;  
  12.   
  13.       
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.account);  
  18.           
  19.         setUpViews();//設置view  
  20.         setUpListeners();//設置listenter  
  21.           
  22.         registerReceiver(broadcastReceiver, new IntentFilter("com.weibo.caigang.getverifier"));//註冊拿到驗證碼廣播接收器.  
  23.           
  24.         dataHelper = DataBaseContext.getInstance(getApplicationContext());//獲取數據庫連接類,用了單例,保證全局只有一個此對象。  
  25.         userList = dataHelper.GetUserList(false);  
  26.           
  27.         SharedPreferences preferences = getSharedPreferences("default_user",Activity.MODE_PRIVATE);  
  28.         String nick = preferences.getString("user_default_nick""");//取得微博默認登錄賬號信息  
  29.           
  30.         UserInfo user = null;  
  31.           
  32.         if(userList!=null&&userList.size()>0){  
  33.             if (nick != "") {  
  34.                 user = dataHelper.getUserByName(nick,userList);//取得微博默認登錄賬號信息  
  35.             }  
  36.             if(user == null) {  
  37.                 user = userList.get(0);  
  38.             }  
  39.         }  
  40.         if(user!=null){  
  41.             user_default_headicon.setImageDrawable(user.getUserIcon());  
  42.         }  
  43.           
  44.         if(userList!=null&&userList.size()>0){  
  45.             adapater = new UserAdapater();  
  46.             listView.setAdapter(adapater);  
  47.             listView.setOnItemClickListener(this);  
  48.         }  
  49.     }  
  50.       
  51.     private void setUpViews(){  
  52.         listView = getListView();  
  53.         user_default_headicon = (ImageView)findViewById(R.id.user_default_headicon);  
  54.         account_add_btn_bar = (LinearLayout)findViewById(R.id.account_add_btn_bar);  
  55.     }  
  56.       
  57.     private void setUpListeners(){  
  58.         user_default_headicon.setOnClickListener(this);  
  59.         account_add_btn_bar.setOnClickListener(this);  
  60.         listView.setOnItemLongClickListener(this);  
  61.     }  
  62.       
  63.       
  64.       
  65.     public class UserAdapater extends BaseAdapter{  
  66.         @Override  
  67.         public int getCount() {  
  68.             return userList.size();  
  69.         }  
  70.   
  71.         @Override  
  72.         public Object getItem(int position) {  
  73.             return userList.get(position);  
  74.         }  
  75.   
  76.         @Override  
  77.         public long getItemId(int position) {  
  78.             return position;  
  79.         }  
  80.   
  81.         @Override  
  82.         public View getView(int position, View convertView, ViewGroup parent) {  
  83.             convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.account_list_item, null);  
  84.             ImageView user_headicon = (ImageView) convertView.findViewById(R.id.user_headicon);  
  85.             TextView user_nick = (TextView) convertView.findViewById(R.id.user_nick);  
  86.             TextView user_name = (TextView) convertView.findViewById(R.id.user_name);  
  87.             UserInfo user = userList.get(position);  
  88.             try {  
  89.                 user_headicon.setImageDrawable(user.getUserIcon());  
  90.                 user_nick.setText(user.getUserName());  
  91.                 user_name.setText("@"+user.getUserId());  
  92.             } catch (Exception e) {  
  93.                 e.printStackTrace();  
  94.             }  
  95.             return convertView;  
  96.         }  
  97.     }  
  98.   
  99.     @Override  
  100.     public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {  
  101.         currentUser = userList.get(position);  
  102.         user_default_headicon.setImageDrawable(currentUser.getUserIcon());  
  103.     }  
  104.   
  105.     @Override  
  106.     public void onClick(View v) {  
  107.         switch (v.getId()) {  
  108.         case R.id.account_add_btn_bar: {  
  109.             weibo = WeiboContext.getInstance();//單例,保證整個應用只有一個weibo對象  
  110.             weibo.getRequestToken();  
  111.             Intent intent = new Intent(AccountActivity.this,AuthorizeActivity.class);  
  112.             Bundle bundle = new Bundle();  
  113.             bundle.putString("url", weibo.getAuthorizeUrl());  
  114.             intent.putExtras(bundle);  
  115.             startActivity(intent);//跳轉到騰訊的微博授權頁面,使用webview來顯示  
  116.         }  
  117.             break;  
  118.         case R.id.user_default_headicon: {  
  119.             SharedPreferences preferences = getSharedPreferences("default_user", Activity.MODE_PRIVATE);  
  120.             SharedPreferences.Editor editor = preferences.edit();  
  121.             editor.putString("user_default_nick", currentUser.getUserName());  
  122.             editor.putString("user_default_name", currentUser.getUserId());  
  123.             editor.commit();  
  124.             Intent intent = new Intent(AccountActivity.this, MainActivity.class);  
  125.             startActivity(intent);  
  126.         }  
  127.             break;  
  128.   
  129.         default:  
  130.             break;  
  131.         }  
  132.     }  
  133.       
  134.     BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {  
  135.         @Override  
  136.         public void onReceive(Context context, Intent intent) {  
  137.             if(intent.getAction().equals("com.weibo.caigang.getverifier")){  
  138.                 weibo = WeiboContext.getInstance();  
  139.                 Bundle bundle = intent.getExtras();  
  140.                 String veryfier = bundle.getString("veryfier");//獲取從授權頁面返回的veryfier  
  141.                 if(veryfier!=null){  
  142.                     //unregisterReceiver(broadcastReceiver);  
  143.                     weibo.getAccessToken(weibo.getTokenKey(), weibo.getTokenSecrect(), veryfier);//取得key和secret,這個key和secret非常重要,調騰訊的API全靠它了,神馬新浪的,人人網的都一樣的,不過還是有點區別,騰訊的OAuth認證是基於1.0的  
  144.                     String userInfo = weibo.getUserInfo(weibo.getAccessTokenKey(), weibo.getAccessTokenSecrect());  
  145.                     try {  
  146.                         JSONObject data = new JSONObject(userInfo).getJSONObject("data");  
  147.                         String headUrl = null;  
  148.                         if(data.getString("head")!=null&&!"".equals(data.getString("head"))){  
  149.                             headUrl = data.getString("head")+"/100";  
  150.                         }  
  151.                         String userId = data.getString("name");  
  152.                         String userName = data.getString("nick");  
  153.                           
  154.                         UserInfo user = new UserInfo();//生成一個user對象保存到數據庫  
  155.                         user.setUserId(userId);  
  156.                         user.setUserName(userName);  
  157.                         user.setToken(weibo.getAccessTokenKey());  
  158.                         user.setTokenSecret(weibo.getAccessTokenSecrect());  
  159.                           
  160.                         Long insertId = 0L;  
  161.                           
  162.                         if (dataHelper.HaveUserInfo(userId)){//數據庫已經存在了次用戶  
  163.                             dataHelper.UpdateUserInfo(userName, ImageUtil.getRoundBitmapFromUrl(headUrl, 15), userId);  
  164.                             //Toast.makeText(AccountActivity.this, "此用戶已存在,如果你用戶名或者頭像已經改變,那麼此操作將同步更新!", Toast.LENGTH_LONG).show();  
  165.                         }else{  
  166.                             if(headUrl!=null){  
  167.                                 insertId = dataHelper.SaveUserInfo(user,ImageUtil.getBytesFromUrl(headUrl));  
  168.                             }else{  
  169.                                 insertId = dataHelper.SaveUserInfo(user,null);  
  170.                             }  
  171.                         }  
  172.                         if(insertId>0L){  
  173.                             //Toast.makeText(AccountActivity.this, "已經授權成功,將跳轉到選擇默認的登錄用戶界面!", Toast.LENGTH_LONG).show();  
  174.                         }  
  175.                         Log.d(TAG+"插入數據庫的id是", insertId.toString());  
  176.                           
  177.                         userList = dataHelper.GetUserList(false);  
  178.                         adapater = new UserAdapater();  
  179.                         adapater.notifyDataSetChanged();//刷新listview  
  180.                         listView.setAdapter(adapater);  
  181.                           
  182.                     } catch (JSONException e) {  
  183.                         e.printStackTrace();  
  184.                     } catch (Exception e) {  
  185.                         e.printStackTrace();  
  186.                     }  
  187.                     Log.e(TAG, userInfo);  
  188.                 }  
  189.                 Log.e(TAG, veryfier);  
  190.             }  
  191.         }  
  192.     };  
  193.   
  194.   
  195.     @Override  
  196.     public boolean onItemLongClick(AdapterView<?> arg0, View arg1, final int position,  
  197.             long arg3) {  
  198.         CharSequence [] items = null;  
  199.         items= new CharSequence[]{"刪除賬號","取消"};  
  200.         new AlertDialog.Builder(AccountActivity.this).setTitle("選項").setItems(items,new DialogInterface.OnClickListener() {  
  201.             @Override  
  202.             public void onClick(DialogInterface dialog, int which) {  
  203.                         switch (which) {  
  204.                         case 0: {  
  205.                             String userId = userList.get(position).getUserId();  
  206.                             dataHelper.DelUserInfo(userId);//刪除數據庫記錄  
  207.                             SharedPreferences preferences = getSharedPreferences("default_user", Activity.MODE_PRIVATE);  
  208.                             SharedPreferences.Editor editor = preferences.edit();  
  209.                             if(preferences.getString("user_default_name""").equals(userId)){  
  210.                                 editor.putString("user_default_nick""");  
  211.                                 editor.putString("user_default_name""");  
  212.                                 editor.commit();//清除裏面保存的記錄SharedPreferences  
  213.                             }  
  214.                             userList = dataHelper.GetUserList(false);  
  215.                             adapater = new UserAdapater();  
  216.                             adapater.notifyDataSetChanged();//刷新listview  
  217.                             listView.setAdapter(adapater);  
  218.                         }  
  219.                             break;  
  220.                         case 1: {  
  221.                         }  
  222.                             break;  
  223.                         default:  
  224.                             break;  
  225.                         }  
  226.             }  
  227.         }).show();  
  228.         return false;  
  229.     }  
  230. }  

Java代碼  收藏代碼
  1. public class AuthorizeActivity extends Activity {  
  2.     private static final String TAG = "AuthorizeActivity";  
  3.   
  4.     @Override  
  5.     public void onCreate(Bundle savedInstanceState) {  
  6.         super.onCreate(savedInstanceState);  
  7.         setContentView(R.layout.webview);  
  8.         WebView webView = (WebView) findViewById(R.id.web);  
  9.         webView.setWebViewClient(new MyWebViewClient());  
  10.         Intent intent = this.getIntent();  
  11.         if (!intent.equals(null)) {  
  12.             Bundle bundle = intent.getExtras();  
  13.             if (bundle != null) {  
  14.                 if (bundle.containsKey("url")) {  
  15.                     String url = bundle.getString("url");  
  16.                     WebSettings webSettings = webView.getSettings();  
  17.                     webSettings.setJavaScriptEnabled(true);  
  18.                     webSettings.setSupportZoom(true);  
  19.                     webView.requestFocus();  
  20.                     webView.loadUrl(url);  
  21.                 }  
  22.             }  
  23.         }  
  24.     }  
  25.   
  26.     @Override  
  27.     public boolean onKeyDown(int keyCode, KeyEvent event) {  
  28.         if (keyCode == KeyEvent.KEYCODE_BACK) {  
  29.             WebView webView = (WebView) findViewById(R.id.web);  
  30.             if (webView.canGoBack()) {  
  31.                 webView.goBack();  
  32.                 return true;  
  33.             }  
  34.         }  
  35.         return super.onKeyDown(keyCode, event);  
  36.     }  
  37.   
  38.     public class MyWebViewClient extends WebViewClient {  
  39.         @Override  
  40.         public boolean shouldOverrideUrlLoading(WebView view, String url) {  
  41.             view.loadUrl(url);  
  42.             return true;  
  43.         }  
  44.   
  45.         @Override  
  46.         public void onPageStarted(WebView view, String url, Bitmap favicon) {  
  47.             Pattern p = Pattern.compile("^" + MyWeiboSync.CALLBACK_URL  
  48.                     + ".*oauth_verifier=(\\d+)");  
  49.             Matcher m = p.matcher(url);  
  50.             if (m.find()) {  
  51.                 Intent intent = new Intent();  
  52.                 intent.setAction("com.weibo.caigang.getverifier");  
  53.                 String veryfier = m.group(1);  
  54.                 intent.putExtra("veryfier", veryfier);  
  55.                 sendBroadcast(intent);  
  56.                 finish();  
  57.             }  
  58.         }  
  59.     }  
  60. }  

Java代碼  收藏代碼
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="#b8c0c8" android:layout_width="fill_parent" android:layout_height="fill_parent">  
  3.     <RelativeLayout android:id="@+id/account_top" android:background="@drawable/header" android:layout_width="fill_parent" android:layout_height="wrap_content">  
  4.         <TextView android:textSize="22.0sp" android:text="賬號管理" android:textColor="#ffffffff" android:ellipsize="middle" android:gravity="center_horizontal" android:id="@+id/account_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true"  android:layout_centerInParent="true" android:layout_alignWithParentIfMissing="true" />  
  5.     </RelativeLayout>  
  6.       
  7.     <ListView android:id="@id/android:list"  android:background="@drawable/panel_bg" android:layout_marginTop="20.0dip" android:layout_marginLeft="10.0dip" android:layout_marginRight="10.0dip" android:padding="5.0dip" android:layout_below="@id/account_top" android:layout_width="fill_parent" android:cacheColorHint="#00000000"  
  8.         android:layout_height="wrap_content" android:layout_weight="1" android:divider="@drawable/list_divider"/>  
  9.           
  10.     <RelativeLayout android:id="@+id/account_bottom" android:layout_width="fill_parent" android:layout_height="40.0dip" android:gravity="center" android:layout_alignParentBottom="true">  
  11.         <Button android:id="@+id/account_back_btn" android:layout_width="40.0dip" android:drawableTop="@drawable/btn_back_selector" android:background="@drawable/bottom_back_bg"  
  12.             android:layout_height="40.0dip"  android:layout_alignParentLeft="true"/>  
  13.         <Button android:id="@+id/account_tohome_btn" android:layout_width="40.0dip"  
  14.             android:layout_height="40.0dip" android:drawableTop="@drawable/btn_home_selector" android:background="@drawable/bottom_home_bg" android:layout_alignParentRight="true"/>  
  15.         <LinearLayout android:layout_marginLeft="35.0dip" android:layout_toRightOf="@id/account_back_btn" android:layout_toLeftOf="@id/account_tohome_btn" android:layout_centerInParent="true" android:orientation="horizontal" android:id="@+id/account_add_btn_bar" android:layout_width="fill_parent" android:layout_height="fill_parent">      
  16.             <TextView android:textSize="16.0dip" android:text="添加賬號" android:textColor="@color/bottom_button_text_selector" android:gravity="center" android:background="@drawable/account_add_btn_selector" android:focusable="true" android:layout_width="wrap_content" android:layout_height="wrap_content" />  
  17.         </LinearLayout>     
  18.     </RelativeLayout>  
  19.       
  20.     <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:layout_above="@id/account_bottom" android:layout_marginBottom="40.0dip">  
  21.         <ImageView   android:id="@+id/user_default_headicon" android:layout_width="120.0dip" android:layout_height="78.0dip"/>  
  22.     </RelativeLayout>  
  23.       
  24. </RelativeLayout>  

Java代碼  收藏代碼
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:paddingTop="3.0dip" android:orientation="horizontal" android:background="@drawable/listitem_selector"  android:layout_width="fill_parent" android:layout_height="wrap_content">  
  3.     <RelativeLayout android:layout_width="50.0dip" android:layout_height="50.0dip" android:layout_weight="0.0">  
  4.         <ImageView android:id="@+id/user_headicon" android:layout_width="45.0dip" android:layout_height="45.0dip" android:scaleType="fitCenter" android:layout_centerInParent="true" />  
  5.     </RelativeLayout>  
  6.     <RelativeLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="4.0dip" android:layout_weight="1.0">  
  7.         <TextView android:id="@+id/user_nick" android:textColor="#000000" android:layout_width="wrap_content" android:layout_height="32.0dip" android:textSize="14.0sp" android:layout_alignParentLeft="true"/>  
  8.         <TextView android:id="@+id/user_name" android:layout_marginLeft="6.0dip" android:layout_below="@id/user_nick" android:textColor="#ff000000" android:layout_width="wrap_content" android:layout_height="32.0dip" android:textSize="8.0sp" android:layout_alignParentLeft="true"/>  
  9.     </RelativeLayout>  
  10. </LinearLayout>  


Java代碼  收藏代碼
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.     <WebView     
  8.         android:id="@+id/web"  
  9.         android:layout_height="wrap_content"              
  10.         android:layout_width="wrap_content"    
  11.         />     
  12. </LinearLayout>  



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章