人人网官方Android客户端源码分析(3)

3. 开发Android应用访问renren.db中的数据
从上面分析中我们已经知道renren.db中表结构,及访问特定表对应的Uri,如我们可以通过content://com.renren.mobile.provider/account访问renren.db中的account表等等。下面我们写个很简单的例子来访问account表中的account和ticket字段。
main.xml根节点下简单添加2个TextView,如下:



  1. <TextView   
  2.         android:id="@+id/textView1"   
  3.         android:layout_width="fill_parent"   
  4.         android:layout_height="wrap_content"   
  5.         android:text="TextView" />   
  6.    
  7.    
  8. <TextView   
  9.         android:id="@+id/textView2"   
  10.         android:layout_width="fill_parent"   
  11.         android:layout_height="wrap_content"   
  12.         android:text="TextView" />
复制代码

  
将account和ticket信息显示到TextView中,类代码如下

  1. public class RenRenExtActivity extends Activity {   
  2.    
  3.     private static final Uri ACCOUNT_CONTENT_URI = Uri   
  4.             .parse("content://com.renren.mobile.provider/account");   
  5.    
  6.     @Override   
  7.     public void onCreate(Bundle savedInstanceState) {   
  8.         super.onCreate(savedInstanceState);   
  9.         setContentView(R.layout.main);   
  10.    
  11.         TextView textView1 = (TextView) findViewById(R.id.textView1);   
  12.         textView1.setText("Sorry.");   
  13.         TextView textView2 = (TextView) findViewById(R.id.textView2);   
  14.         textView2.setText("Sorry2.");   
  15.    
  16.         Cursor cursor = getContentResolver().query(ACCOUNT_CONTENT_URI, null,   
  17.                 null, null, null);   
  18.    
  19.         List<Map<String, String>> resultList = converCursorToList(cursor);   
  20.         if (!resultList.isEmpty()) {   
  21.             Map<String, String> map = resultList.get(0);   
  22.             textView1.setText(map.get("account"));   
  23.             textView2.setText(map.get("ticket"));   
  24.         }   
  25.     }   
  26.    
  27.     private List<Map<String, String>> converCursorToList(Cursor cursor) {   
  28.         List<Map<String, String>> result = new ArrayList<Map<String, String>>();   
  29.    
  30.         if (cursor == null) {   
  31.             return Collections.emptyList();   
  32.         }   
  33.    
  34.         // 遍历Cursor结果集   
  35.         while (cursor.moveToNext()) {   
  36.             // 将结果集中的数据存入ArrayList中   
  37.             Map<String, String> map = new HashMap<String, String>();   
  38.             map.put("account",   
  39.                     cursor.getString(cursor.getColumnIndex("account")));   
  40.             map.put("ticket", cursor.getString(cursor.getColumnIndex("ticket")));   
  41.             result.add(map);   
  42.         }   
  43.         return result;   
  44.     }   
  45. }
复制代码
需要指出的是,上面的应用程序需要操作人人网android客户端中的数据库,因此要记得在AndroidMantifest.xml文件中为该应用程序授权。也就是在该文件的根元素中添加如下元素:
  • <uses-permission android:name="com.renren.mobile.android.permission.PERMISSION_ADD_ACCOUNT" />

如果你android手机中安装有人人网Android客户端且曾经使用过,那么renren.db中应该有数据存在,把上面应用打包为apk文件安装到你android手机中,运行它,应该能看到屏幕中将显示你的人人网账号及一串ticket,该ticket是人人网Andriod客户端部分功能与人人网服务器通信的sid。
同理,也可以使用其它特定Uri访问手机中renre.db中特定的表,如friends表等等,所有Uri详见RenRenProvider代码。

 

http://www.apkway.com/forum.php?mod=viewthread&tid=4839

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值