博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android腾讯微博客户端开发三:多账号管理的实现
阅读量:5034 次
发布时间:2019-06-12

本文共 13129 字,大约阅读时间需要 43 分钟。

先看看效果。。
 
列表中的账号是保存在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>  

转载于:https://www.cnblogs.com/afly/archive/2012/02/20/2360233.html

你可能感兴趣的文章
hdu 1010 dfs搜索
查看>>
搭建wamp环境,数据库基础知识
查看>>
android中DatePicker和TimePicker的使用
查看>>
SpringMVC源码剖析(四)- DispatcherServlet请求转发的实现
查看>>
Codeforces Gym 100513M M. Variable Shadowing 暴力
查看>>
浅谈 Mybatis中的 ${ } 和 #{ }的区别
查看>>
CNN 笔记
查看>>
版本更新
查看>>
SQL 单引号转义
查看>>
实现手机扫描二维码页面登录,类似web微信-第三篇,手机客户端
查看>>
【凸优化】保留凸性的几个方式(交集、仿射变换、投影、线性分式变换)
查看>>
NYOJ-613//HDU-1176-免费馅饼,数字三角形的兄弟~~
查看>>
TFS --- GrantBackup Plan Permissions Error
查看>>
Css:背景色透明,内容不透明之终极方法!兼容所有浏览器
查看>>
mysql存储过程
查看>>
letecode [136] - Single Number
查看>>
linux下设置固定IP的方法
查看>>
高效的jQuery
查看>>
ubuntu 16.04 (软件应用)-输入法
查看>>
windos7修复引导扇区
查看>>