自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

人人網(wǎng)官方Android客戶端源碼分析

移動開發(fā) Android
本文針對最新人人網(wǎng)官方Android客戶端(4.2.1,該版本新增聊天功能)進行源碼分析,僅供Android學習交流使用!

ContentProvider是不同應(yīng)用程序之間進行數(shù)據(jù)交換的標準API,ContentProvider以某種Uri的形式對外提供數(shù)據(jù),允許其他應(yīng)用訪問或修改數(shù)據(jù);其他應(yīng)用程序使用ContentResolver根據(jù)Uri去訪問操作指定數(shù)據(jù)。

人人網(wǎng)Android客戶端也是使用ContentProvider對需要保存于Android客戶端的數(shù)據(jù)進行管理。

1. renren.db

SQLLiteOpenHelper是Android提供的一個管理數(shù)據(jù)庫的工具類,可用于管理數(shù)據(jù)庫的創(chuàng)建和版本更新。一般的用法是創(chuàng)建SQLiteOpenHelper的子類,并擴展它的onCreate(SQLiteDatabase db)和onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)方法。

人人網(wǎng)Android客戶端使用該方法創(chuàng)建及修改用戶手機中的人人網(wǎng)數(shù)據(jù)庫(renren.db)。

下面是RenRenProvider$DatabaseHelper的代碼:

  1. public class RenRenProvider$DatabaseHelper extends SQLiteOpenHelper {   
  2.     public RenRenProvider$DatabaseHelper(Context context) {   
  3.         super(context, "renren.db"null71);   
  4.     }   
  5.    
  6.     @Override   
  7.     public void onCreate(SQLiteDatabase db) {   
  8.         db.execSQL("CREATE TABLE pic (_id INTEGER PRIMARY KEY,url TEXT UNIQUE ON CONFLICT REPLACE,_data TEXT,size INTEGER);");   
  9.         db.execSQL("CREATE TABLE account (_id INTEGER PRIMARY KEY,uid INTEGET,account TEXT UNIQUE ON CONFLICT REPLACE,pwd TEXT,proxy INTEGER,sessionkey TEXT,srt_key TEXT,ticket TEXT,name TEXT,headphoto BLOB,isdefault INTEGER,last_login INTEGER,friend_count INTEGER);");   
  10.         db.execSQL("CREATE TABLE home (_id INTEGER PRIMARY KEY,item_id INTEGER UNIQUE ON CONFLICT REPLACE,data BLOB);");   
  11.         db.execSQL("CREATE TABLE profile (_id INTEGER PRIMARY KEY,type INTEGER UNIQUE ON CONFLICT REPLACE,data BLOB);");   
  12.         db.execSQL("CREATE TABLE friends (_id INTEGER PRIMARY KEY,uid INTEGER UNIQUE ON CONFLICT REPLACE,username TEXT,headurl TEXT,doing TEXT,nameindex TEXT,namepinyin TEXT,friendgroup TEXT,network TEXT,gender TEXT,isfriend INTEGER,suggest_text_1 TEXT,suggest_intent_query TEXT);");   
  13.         db.execSQL("CREATE TABLE messages (_id INTEGER PRIMARY KEY,messageid INTEGER UNIQUE ON CONFLICT REPLACE,message BLOB);");   
  14.         db.execSQL("CREATE TABLE favorites (_id INTEGER PRIMARY KEY,favoriteid BIGINT UNIQUE ON CONFLICT REPLACE,favoriteowner INTEGER,type INTEGER,favorite BLOB);");   
  15.         db.execSQL("CREATE TABLE emonticons (_id INTEGER PRIMARY KEY,url TEXT,emotion TEXT  UNIQUE ON CONFLICT REPLACE,img BLOB,size INTEGER,_data TEXT);");   
  16.         db.execSQL("CREATE TABLE favoritefriends (_id INTEGER PRIMARY KEY,owner INTEGER,uid INTEGER,name TEXT);");   
  17.         db.execSQL("CREATE TABLE chathistory (_id INTEGER PRIMARY KEY,uid INTEGER,tochatid INTEGER,chatmessage TEXT,comefrom INTEGER,chatname TEXT,chattime LONG);");   
  18.     }   
  19.    
  20.     @Override   
  21.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {   
  22.         db.execSQL("DROP TABLE IF EXISTS pic");   
  23.         db.execSQL("DROP TABLE IF EXISTS account");   
  24.         db.execSQL("DROP TABLE IF EXISTS home");   
  25.         db.execSQL("DROP TABLE IF EXISTS profile");   
  26.         db.execSQL("DROP TABLE IF EXISTS friends");   
  27.         db.execSQL("DROP TABLE IF EXISTS messages");   
  28.         db.execSQL("DROP TABLE IF EXISTS favorites");   
  29.         db.execSQL("DROP TABLE IF EXISTS emonticons");   
  30.         db.execSQL("DROP TABLE IF EXISTS favoritefriends");   
  31.         db.execSQL("DROP TABLE IF EXISTS favoritefriends");   
  32.         db.execSQL("DROP TABLE IF EXISTS chathistory");   
  33.         onCreate(db);   
  34.     }   
  35. }   

從代碼中我們可以看到人人網(wǎng)Android客戶端在用戶手機上創(chuàng)建了renren.db數(shù)據(jù)庫,數(shù)據(jù)庫中共有10張表,分別為pic、account、home、profile、friends、messages、favorites、emonticons、favoritefriends、chathistory。#p#

2. RenRenProvider

前面我們已經(jīng)提到過ContentProvider,下面我們來看看人人網(wǎng)Android客戶端是如何開發(fā)ContentProvider的。開發(fā)ContentProvider的兩步:1)開發(fā)一個ContentProvider的子類,該子類需要實現(xiàn)增、刪、改、查等方法。2)在AndroidManifest.xml文件中注冊該ContentProvider。

下面是RenRenProvider核心代碼:

  1. public class RenRenProvider extends ContentProvider {   
  2.    
  3.     public static final String AUTHORITY = "com.renren.mobile.provider";   
  4.    
  5.     public static final class Account implements BaseColumns {   
  6.         public static final Uri ACCOUNT_CONTENT_URI = Uri   
  7.                 .parse("content://com.renren.mobile.provider/account");   
  8.     }   
  9.     public static final class ChatHistory implements BaseColumns {   
  10.         public static final Uri CHAT_HISTORY_CONTENT_URI = Uri   
  11.                 .parse("content://com.renren.mobile.provider/chathistory");   
  12.     }   
  13.     public static final class Emonticons implements BaseColumns {   
  14.         public static final Uri EMONTICONS_CONTENT_URI = Uri   
  15.                 .parse("content://com.renren.mobile.provider/emonticons");   
  16.     }   
  17.     public static final class Favorite implements BaseColumns {   
  18.         public static final Uri FAVORITE_CONTENTURI = Uri   
  19.                 .parse("content://com.renren.mobile.provider/favorites");   
  20.     }   
  21.     public static final class FavoriteFriends implements BaseColumns {   
  22.         public static final Uri FAVORITE_FRIENDS_CONTENT_URI = Uri   
  23.                 .parse("content://com.renren.mobile.provider/favoritefriends");   
  24.     }   
  25.     public static final class Friends implements BaseColumns {   
  26.         public static final Uri FRIENDS_CONTENT_URI = Uri   
  27.                 .parse("content://com.renren.mobile.provider/friends");   
  28.     }   
  29.     public static final class Home implements BaseColumns {   
  30.         public static final Uri HOME_CONTENT_URI = Uri   
  31.                 .parse("content://com.renren.mobile.provider/home");   
  32.     }   
  33.    
  34.     public static final class Messages implements BaseColumns {   
  35.         public static final Uri MESSAGES_CONTENT_URI = Uri   
  36.                 .parse("content://com.renren.mobile.provider/messages");   
  37.     }   
  38.    
  39.     public static final class Pic implements BaseColumns {   
  40.         public static final Uri PIC_CONTENT_URI = Uri   
  41.                 .parse("content://com.renren.mobile.provider/pic");   
  42.     }   
  43.     public static final class Profile implements BaseColumns {   
  44.         public static final Uri PROFILE_CONTENT_URI = Uri   
  45.                 .parse("content://com.renren.mobile.provider/profile");   
  46.     }   
  47.    
  48.     private static final UriMatcher URI_MATCHER;   
  49.     private RenRenProvider.DatabaseHelper renrenDatabaseHelper;   
  50.    
  51.     static {   
  52.         URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);   
  53.         URI_MATCHER.addURI(AUTHORITY, "pic"3);   
  54.         URI_MATCHER.addURI(AUTHORITY, "pic/#"4);   
  55.         URI_MATCHER.addURI(AUTHORITY, "account"5);   
  56.         URI_MATCHER.addURI(AUTHORITY, "account/#"6);   
  57.         URI_MATCHER.addURI(AUTHORITY, "home"7);   
  58.         URI_MATCHER.addURI(AUTHORITY, "home/#"8);   
  59.         URI_MATCHER.addURI(AUTHORITY, "profile"9);   
  60.         URI_MATCHER.addURI(AUTHORITY, "profile/#"10);   
  61.         URI_MATCHER.addURI(AUTHORITY, "friends"11);   
  62.         URI_MATCHER.addURI(AUTHORITY, "friends/#"12);   
  63.         URI_MATCHER.addURI(AUTHORITY, "search_suggest_query/*"1);   
  64.         URI_MATCHER.addURI(AUTHORITY, "search_suggest_query"2);   
  65.         URI_MATCHER.addURI(AUTHORITY, "messages"13);   
  66.         URI_MATCHER.addURI(AUTHORITY, "messages/#"14);   
  67.         URI_MATCHER.addURI(AUTHORITY, "favorites"15);   
  68.         URI_MATCHER.addURI(AUTHORITY, "favorites/#"16);   
  69.         URI_MATCHER.addURI(AUTHORITY, "emonticons"17);   
  70.         URI_MATCHER.addURI(AUTHORITY, "emonticons/#"18);   
  71.         URI_MATCHER.addURI(AUTHORITY, "favoritefriends"19);   
  72.         URI_MATCHER.addURI(AUTHORITY, "favortiefriends/#"20);   
  73.         URI_MATCHER.addURI(AUTHORITY, "chathistory"21);   
  74.         URI_MATCHER.addURI(AUTHORITY, "chathistory/#"22);   
  75.         URI_MATCHER.addURI(AUTHORITY, "chathistory/*/*"23);   
  76.     }   
  77.    
  78.     public boolean onCreate()   
  79.     {   
  80.         renrenDatabaseHelper = new RenRenProvider.DatabaseHelper(this.getContext());   
  81.         return true;   
  82.     }   
  83.    
  84.     //其它代碼省略...   
  85. }   

下面是人人網(wǎng)android客戶端在AndroidMantifest.xml中對該ContentProvider的注冊。 

  1. <providerandroid:nameproviderandroid:nameproviderandroid:nameproviderandroid:name=".contentprovider.RenRenProvider" android:permission="com.renren.mobile.android.permission.PERMISSION_ADD_ACCOUNT"    
  2. android:authorities="com.renren.mobile.provider" />   

從上面的分析我們了解到只要得到com.renren.mobile.android.permission.PERMISSION_ADD_ACCOUNT權(quán)限我們就可以通過特定Uri訪問人人網(wǎng)Android客戶端在用戶手機上創(chuàng)建的renren.db中特定表了。#p#

3. 開發(fā)Android應(yīng)用訪問renren.db中的數(shù)據(jù)

從上面分析中我們已經(jīng)知道renren.db中表結(jié)構(gòu),及訪問特定表對應(yīng)的Uri,如我們可以通過content://com.renren.mobile.provider/account訪問renren.db中的account表等等。下面我們寫個很簡單的例子來訪問account表中的account和ticket字段。

main.xml根節(jié)點下簡單添加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.                 nullnullnull);   
  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結(jié)果集   
  35.         while (cursor.moveToNext()) {   
  36.             // 將結(jié)果集中的數(shù)據(jù)存入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. }   

需要指出的是,上面的應(yīng)用程序需要操作人人網(wǎng)android客戶端中的數(shù)據(jù)庫,因此要記得在AndroidMantifest.xml文件中為該應(yīng)用程序授權(quán)。也就是在該文件的根元素中添加如下元素: 

  1. <uses-permission android:name="com.renren.mobile.android.permission.PERMISSION_ADD_ACCOUNT" />   

如果你android手機中安裝有人人網(wǎng)Android客戶端且曾經(jīng)使用過,那么renren.db中應(yīng)該有數(shù)據(jù)存在,把上面應(yīng)用打包為apk文件安裝到你android手機中,運行它,應(yīng)該能看到屏幕中將顯示你的人人網(wǎng)賬號及一串ticket,該ticket是人人網(wǎng)Andriod客戶端部分功能與人人網(wǎng)服務(wù)器通信的sid。

同理,也可以使用其它特定Uri訪問手機中renre.db中特定的表,如friends表等等,所有Uri詳見RenRenProvider代碼。

 

責任編輯:佚名 來源: Iteye
相關(guān)推薦

2011-10-31 17:29:27

人人網(wǎng)客戶端照片

2021-07-16 06:56:50

Nacos注冊源碼

2011-11-04 15:03:36

照片客戶端人人飛傳

2011-07-20 15:55:07

人人手機客戶端

2015-03-30 14:24:06

網(wǎng)易布局

2022-04-01 08:31:11

RabbitMQ客戶端Channel

2022-03-29 08:31:18

RabbitMQMQ客戶端

2015-01-09 11:49:26

Android源碼下載

2021-03-12 16:21:02

LinuxEvernote客戶端

2022-04-07 08:30:57

AMQP協(xié)議RabbitMQ客戶端源碼

2021-08-01 23:18:21

Redis Golang命令

2018-12-27 13:11:04

愛奇藝APP優(yōu)化

2011-07-01 10:00:11

Ubuntu OneAndroid

2022-04-20 08:32:09

RabbitMQ流控制

2011-08-17 10:10:59

2021-09-22 15:46:29

虛擬桌面瘦客戶端胖客戶端

2009-11-09 15:49:01

WCF異步調(diào)用

2012-08-29 09:40:12

SkyDrive An

2015-01-14 13:59:50

騰訊微博客戶端源碼下載

2022-07-05 15:18:51

Kuro開源
點贊
收藏

51CTO技術(shù)棧公眾號