Android数据存储方式简括

Android提供以下四种存储方式:

SharePreference

SQLite

File

ContentProvider

如果要实现数据共享,正确的方式是使用ContentProvider

SharedPreference

  • “用户的偏好设置”通常用来存储程序的一些配置信息。
  • 其存储在“data/data/程序包名/shared_prefs目录下。
  • 使用键-值的形式来存储数据;我们只需要调用SharedPreferences的getXxx(name), 就可以根据键获得对应的值!
  • SharedPreference本身只能获取数据,存储和修改要通过Editor对象来实现。

代码速记:

存储数据

[java] view
plain
 copy

  1. SharedPreferencessp =context.getSharedPreferences("CITY", MODE_PRIVATE); //获取对象
  2. Editoreditor=sp.edit();
  3. editor.putString("CityName",City.getCity().getCityName());//存储数据
  4. editor.commit();//将数据提交

读取数据

//定义一个读取SP文件的方法

[java] view
plain
 copy

  1. public Map<String, String> read() {
  2. Map<String, String> data =new HashMap<String, String>();
  3. SharedPreferences sp =mContext.getSharedPreferences("mysp", Context.MODE_PRIVATE);
  4. data.put("username", sp.getString("username","")); //获取数据;
  5. data.put("passwd", sp.getString("passwd",""));
  6. return data;
  7. }

SharedPreference工具类:

SPUtils.java

[java] view
plain
 copy

  1. package com.jay.sharedpreferencedemo3;
  2. import android.content.Context;
  3. import android.content.SharedPreferences;
  4. import java.util.Map;
  5. /**
  6. * Created by Jay on 2015/9/2 0002.
  7. */
  8. public class SPUtils {
  9. /**
  10. * 保存在手机里的SP文件名
  11. */
  12. public static final String FILE_NAME = "my_sp";
  13. /**
  14. * 保存数据
  15. */
  16. public static void put(Context context, String key, Object obj) {
  17. SharedPreferences sp = context.getSharedPreferences(FILE_NAME, context.MODE_PRIVATE);
  18. SharedPreferences.Editor editor = sp.edit();
  19. if (obj instanceof Boolean) {
  20. editor.putBoolean(key, (Boolean) obj);
  21. } else if (obj instanceof Float) {
  22. editor.putFloat(key, (Float) obj);
  23. } else if (obj instanceof Integer) {
  24. editor.putInt(key, (Integer) obj);
  25. } else if (obj instanceof Long) {
  26. editor.putLong(key, (Long) obj);
  27. } else {
  28. editor.putString(key, (String) obj);
  29. }
  30. editor.commit();
  31. }
  32. /**
  33. * 获取指定数据
  34. */
  35. public static Object get(Context context, String key, Object defaultObj) {
  36. SharedPreferences sp = context.getSharedPreferences(FILE_NAME, context.MODE_PRIVATE);
  37. if (defaultObj instanceof Boolean) {
  38. return sp.getBoolean(key, (Boolean) defaultObj);
  39. } else if (defaultObj instanceof Float) {
  40. return sp.getFloat(key, (Float) defaultObj);
  41. } else if (defaultObj instanceof Integer) {
  42. return sp.getInt(key, (Integer) defaultObj);
  43. } else if (defaultObj instanceof Long) {
  44. return sp.getLong(key, (Long) defaultObj);
  45. } else if (defaultObj instanceof String) {
  46. return sp.getString(key, (String) defaultObj);
  47. }
  48. return null;
  49. }
  50. /**
  51. * 删除指定数据
  52. */
  53. public static void remove(Context context, String key) {
  54. SharedPreferences sp = context.getSharedPreferences(FILE_NAME, context.MODE_PRIVATE);
  55. SharedPreferences.Editor editor = sp.edit();
  56. editor.remove(key);
  57. editor.commit();
  58. }
  59. /**
  60. * 返回所有键值对
  61. */
  62. public static Map<String, ?> getAll(Context context) {
  63. SharedPreferences sp = context.getSharedPreferences(FILE_NAME, context.MODE_PRIVATE);
  64. Map<String, ?> map = sp.getAll();
  65. return map;
  66. }
  67. /**
  68. * 删除所有数据
  69. */
  70. public static void clear(Context context) {
  71. SharedPreferences sp = context.getSharedPreferences(FILE_NAME, context.MODE_PRIVATE);
  72. SharedPreferences.Editor editor = sp.edit();
  73. editor.clear();
  74. editor.commit();
  75. }
  76. /**
  77. * 检查key对应的数据是否存在
  78. */
  79. public static boolean contains(Context context, String key) {
  80. SharedPreferences sp = context.getSharedPreferences(FILE_NAME, context.MODE_PRIVATE);
  81. return sp.contains(key);
  82. }
  83. }

SQLite

  • SQLite是一个轻量级关系型数据库,只有NULL、INTEGER、REAL(浮点数)、TEXT(字符串)和BLOB(大数据)五种类型,不存在BOOLEAN和DATE类型。
  • SQLiteOpenHelper:抽象类,我们通过继承该类,然后重写数据库创建以及更新的方法, 我们还可以通过该类的对象获得数据库实例,或者关闭数据库!
  • SQLiteDatabase:数据库访问类:我们可以通过该类的对象来对数据库做一些增删改查的操作
  • Cursor:游标,有点类似于JDBC里的resultset,结果集!可以简单理解为指向数据库中某 一个记录的指针!

代码速记:

(1).创建数据库

[java] view
plain
 copy

  1. public class MyDBOpenHelper extends SQLiteOpenHelper {
  2. public MyDBOpenHelper(Contextcontext, String name, CursorFactory factory,
  3. int version){super(context, "my.db", null, 1); }
  4. @Override
  5. //数据库第一次创建时被调用
  6. public voidonCreate(SQLiteDatabase db) {
  7. db.execSQL("CREATETABLE person(personid INTEGER PRIMARY KEY AUTOINCREMENT,nameVARCHAR(20))");
  8. }
  9. //软件版本号发生改变时调用
  10. @Override
  11. public voidonUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  12. db.execSQL("ALTERTABLE person ADD phone VARCHAR(12) NULL");
  13. }
  14. }

(2).插入数据:

dbOpenHelper.getWritableDatabase()

[java] view
plain
 copy

  1. public voidsave(Person p)
  2. {
  3. SQLiteDatabase db =dbOpenHelper.getWritableDatabase();
  4. db.execSQL("<span style="color:#339999;">INSERT</span> INTOperson(name,phone) values(?,?)",new String[]{p.getName(),p.getPhone()});
  5. }

3.删除数据:同上

db.execSQL("DELETE FROM person WHEREpersonid = ?",new String[]{id});

4.修改数据:同上

db.execSQL("UPDATE person SET name =?,phone = ? WHERE personid = ?",newString[]{p.getName(),p.getPhone(),p.getId()});

5.查询数据:

dbOpenHelper.getReadableDatabase()

[java] view
plain
 copy

  1. public Personfind(Integer id)
  2. {
  3. SQLiteDatabase db =dbOpenHelper.getReadableDatabase();
  4. Cursor cursor =  db.rawQuery("SELECT * FROM person WHEREpersonid = ?",new String[]{id.toString()});
  5. //存在数据才返回true
  6. if(cursor.moveToFirst())
  7. {
  8. int personid =cursor.getInt(cursor.getColumnIndex("personid"));
  9. String name =cursor.getString(cursor.getColumnIndex("name"));
  10. String phone =cursor.getString(cursor.getColumnIndex("phone"));
  11. return new Person(personid,name,phone);
  12. }
  13. cursor.close();
  14. return null;
  15. }

6.数据分页:

dbOpenHelper.getReadableDatabase()

[java] view
plain
 copy

  1. publicList<Person> getScrollData(int offset,int maxResult)
  2. {
  3. List<Person> person = newArrayList<Person>();
  4. SQLiteDatabase db =dbOpenHelper.getReadableDatabase();
  5. Cursor cursor =  db.rawQuery("SELECT * FROM person ORDERBY personid ASC LIMIT= ?,?",
  6. newString[]{String.valueOf(offset),String.valueOf(maxResult)});
  7. while(cursor.moveToNext())
  8. {
  9. int personid =cursor.getInt(cursor.getColumnIndex("personid"));
  10. String name =cursor.getString(cursor.getColumnIndex("name"));
  11. String phone =cursor.getString(cursor.getColumnIndex("phone"));
  12. person.add(newPerson(personid,name,phone)) ;
  13. }
  14. cursor.close();
  15. return person;
  16. }

7.查询记录数:

dbOpenHelper.getReadableDatabase()

[java] view
plain
 copy

  1. public longgetCount()
  2. {
  3. SQLiteDatabase db =dbOpenHelper.getReadableDatabase();
  4. Cursor cursor =  db.rawQuery("SELECT COUNT (*) FROMperson",null);
  5. cursor.moveToFirst();
  6. long result = cursor.getLong(0);
  7. cursor.close();
  8. return result;
  9. }

PS:除了上面获取条数的方法外还可以使用cursor.getCount()方法获得数据的条数, 但是SQL语句要改改!比如SELECT * FROMperson;

File

文件存储读写

openFileOutput(filename,mode)         往文件写数据

openFileIntput(filename)       读取文件中的信息到程序

getDir(name,mode)             在app的data目录下获取或创建name对应的子目录

getFileDir()                   获取app的data目录的file目录的绝对路径

String[]fileList()      返回app的data目录下的全部文件

deleteFile(filename)             删除app的data目录下的指定文件

ContentProvider

ContentProvider相对于其它的方式比较复杂。它能够实现跨应用之间的数据操作。利用ContentResolver对象的delete、update、insert、query等方法去操ContentProvider的对象,让ContentProvider对象的方法去对数据操作。实现方式为:

  • 在A程序中定义一个ContentProvider,重载其增删查改等方法;

    • 在A程序中的AndroidManifest.xml中注册ContentProvider;
    • 在B程序中通过ContentResolver和Uri来获取ContentProvider的数据,同样利用Resolver的增删查改方法来获得和处理数据。

    在A程序定义一个Provider

    新建一个类,继承ContentProvider,并重载其delete()、insert()、query()、update()、getType()、onCreate()方法。譬如下面的例子,重载其onCreate和query方法

    注:从5.0开始,默认短信应用外的软件不能以写入短信数据库的形式发短信!

    [java] view
    plain
     copy

    1. public class MyProvider extends ContentProvider {
    2. @Override
    3. public int delete(Uri uri, String selection, String[] selectionArgs) {
    4. // TODO Auto-generated method stub
    5. return 0;
    6. }
    7. @Override
    8. public String getType(Uri uri) {
    9. // TODO Auto-generated method stub
    10. return null;
    11. }
    12. @Override
    13. public Uri insert(Uri uri, ContentValues values) {
    14. // TODO Auto-generated method stub
    15. return null;
    16. }
    17. @Override
    18. public boolean onCreate() {
    19. // 新建个数据库并插入一条数据
    20. SQLiteDatabase db=this.getContext().openOrCreateDatabase("test_db2.db", Context.MODE_PRIVATE, null);
    21. db.execSQL("CREATE TABLE t_user (id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT NOT NULL)");
    22. ContentValues values=new ContentValues();
    23. values.put("name", "liangjh2");
    24. db.insert("t_user", "id", values);
    25. db.close();
    26. return false;
    27. }
    28. @Override
    29. public Cursor query(Uri uri, String[] projection, String selection,
    30. String[] selectionArgs, String sortOrder) {
    31. // 获取数据
    32. SQLiteDatabase db=this.getContext().openOrCreateDatabase("test_db2.db", Context.MODE_PRIVATE, null);
    33. Cursor c = db.query("t_user", null, null, null, null, null, null);
    34. db.close();
    35. return c;
    36. }
    37. @Override
    38. public int update(Uri uri, ContentValues values, String selection,
    39. String[] selectionArgs) {
    40. // TODO Auto-generated method stub
    41. return 0;
    42. }
    43. }

    注册ContentProvider

    在AndroidManifest.xml中声明ContentProvider,authorities属性定义了ContentProvider的Uri标识。关于Uri标识属另一个范畴,自行查询。provider标识要放在<application></application>里面。如果遇到了"Permission Denial: opening provide..."的错误,可以试试在节点加“android:exported="true"”。

    <application ...>
        ...
        <provider android:name=".MyProvider" android:authorities="com.example.androidtestdemo" android:exported="true"/>
    </application>

    在B程序获取数据

    用Context获取到当前的ContentResolver,根据Uri地址和ContentResolver的query方法获取A程序的数据。Uri地址和A程序中AndroidManifest.xml定义的autorities要一致。当然,同类可以进行其它的操作。

    [java] view
    plain
     copy

    1. Context ctx=MainActivity.this;
    2. ContentResolver resolver =ctx.getContentResolver();
    3. Uri uri=Uri.parse("content://com.example.androidtestdemo");
    4. Cursor c = resolver.query(uri, null, null, null, null);
    5. c.moveToFirst();
    6. while(!c.isAfterLast()){
    7. for(int i=0,j=c.getColumnCount();i<j;i++){
    8. Log.v("Android2",""+c.getString(i));
    9. }
    10. c.moveToNext();
    11. }

    读取收件箱信息:

    <span style="font-size:18px;"><span class="kwd" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 136);">private</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 136);">void</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> getMsgs</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(){</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
        </span><span class="typ" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 0, 102);">Uri</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> uri </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="typ" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 0, 102);">Uri</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">parse</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="str" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"content://sms/"</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">);</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
        </span><span class="typ" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 0, 102);">ContentResolver</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> resolver </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> getContentResolver</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">();</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
        </span><span class="com" style="border: 0px; margin: 0px; padding: 0px; color: rgb(136, 0, 0);">//获取的是哪些列的信息</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
        </span><span class="typ" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 0, 102);">Cursor</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> cursor </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> resolver</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">query</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">uri</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">,</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 136);">new</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="typ" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 0, 102);">String</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">[]{</span><span class="str" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"address"</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">,</span><span class="str" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"date"</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">,</span><span class="str" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"type"</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">,</span><span class="str" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"body"</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">},</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 136);">null</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">,</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 136);">null</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">,</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 136);">null</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">);</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
        </span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 136);">while</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">cursor</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">moveToNext</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">())</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
        </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">{</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
            </span><span class="typ" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 0, 102);">String</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> address </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> cursor</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">getString</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="lit" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 102, 102);">0</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">);</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
            </span><span class="typ" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 0, 102);">String</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> date </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> cursor</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">getString</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="lit" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 102, 102);">1</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">);</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
            </span><span class="typ" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 0, 102);">String</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> type </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> cursor</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">getString</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="lit" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 102, 102);">2</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">);</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
            </span><span class="typ" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 0, 102);">String</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> body </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> cursor</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">getString</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="lit" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 102, 102);">3</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">);</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
            </span><span class="typ" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 0, 102);">System</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 136);">out</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">println</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="str" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"地址:"</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">+</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> address</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">);</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
            </span><span class="typ" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 0, 102);">System</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 136);">out</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">println</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="str" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"时间:"</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">+</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> date</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">);</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
            </span><span class="typ" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 0, 102);">System</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 136);">out</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">println</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="str" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"类型:"</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">+</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> type</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">);</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
            </span><span class="typ" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 0, 102);">System</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 136);">out</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">println</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="str" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"内容:"</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">+</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> body</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">);</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
            </span><span class="typ" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 0, 102);">System</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 136);">out</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">println</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="str" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"======================"</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">);</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
        </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">}</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
        cursor</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">close</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">();</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
    </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">}</span></span>

    往AndroidManifest.xml加入读取收件箱的权限:

    <span style="font-size:18px;"><span class="tag" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 136);"><uses-permission</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="atn" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 0, 102);">android:name</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="atv" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"android.permission.READ_SMS"</span><span class="tag" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 136);">/></span></span>

    往收件箱里插入一条信息

    <span style="font-size:18px;"><span class="kwd" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 0, 136);">private</span><span class="pln" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 0, 136);">void</span><span class="pln" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> insertMsg</span><span class="pun" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">()</span><span class="pln" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="pun" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">{</span><span class="pln" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
        </span><span class="typ" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(102, 0, 102);">ContentResolver</span><span class="pln" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> resolver </span><span class="pun" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="pln" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> getContentResolver</span><span class="pun" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">();</span><span class="pln" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
        </span><span class="typ" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(102, 0, 102);">Uri</span><span class="pln" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> uri </span><span class="pun" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="pln" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="typ" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(102, 0, 102);">Uri</span><span class="pun" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="pln" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">parse</span><span class="pun" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="str" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"content://sms/"</span><span class="pun" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">);</span><span class="pln" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
        </span><span class="typ" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(102, 0, 102);">ContentValues</span><span class="pln" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> conValues </span><span class="pun" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="pln" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 0, 136);">new</span><span class="pln" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="typ" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(102, 0, 102);">ContentValues</span><span class="pun" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">();</span><span class="pln" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
        conValues</span><span class="pun" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="pln" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">put</span><span class="pun" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="str" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"address"</span><span class="pun" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">,</span><span class="pln" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="str" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"123456789"</span><span class="pun" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">);</span><span class="pln" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
        conValues</span><span class="pun" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="pln" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">put</span><span class="pun" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="str" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"type"</span><span class="pun" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">,</span><span class="pln" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="lit" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 102, 102);">1</span><span class="pun" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">);</span><span class="pln" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
        conValues</span><span class="pun" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="pln" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">put</span><span class="pun" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="str" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"date"</span><span class="pun" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">,</span><span class="pln" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="typ" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(102, 0, 102);">System</span><span class="pun" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="pln" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">currentTimeMillis</span><span class="pun" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">());</span><span class="pln" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
        conValues</span><span class="pun" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="pln" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">put</span><span class="pun" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="str" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"body"</span><span class="pun" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">,</span><span class="pln" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="str" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"no zuo no die why you try!"</span><span class="pun" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">);</span><span class="pln" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
        resolver</span><span class="pun" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="pln" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">insert</span><span class="pun" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="pln" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">uri</span><span class="pun" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">,</span><span class="pln" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> conValues</span><span class="pun" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">);</span><span class="pln" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
        </span><span class="typ" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(102, 0, 102);">Log</span><span class="pun" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="pln" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">e</span><span class="pun" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="str" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"HeHe"</span><span class="pun" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">,</span><span class="pln" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="str" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"短信插入完毕~"</span><span class="pun" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">);</span><span class="pln" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
    </span><span class="pun" style="border: 0px; border-image-source: initial; border-image-slice: initial; border-image-width: initial; border-image-outset: initial; border-image-repeat: initial; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">}</span></span>
    时间: 2024-10-09 07:19:13

    Android数据存储方式简括的相关文章

    Android 数据存储方式分为哪几种?

    10道题值得思考 第一道题 1.Android 数据存储方式分为哪几种? SharedPreferences存储数据 2. CotentProvider内容提供者 3. 文件存储 4. 数据库存储(Sqlite) 5.网络存储 2. NDK是什么? 1. 一系列工具类的集合 2. Arm指令集 3. NDK提供了一系列的工具,帮助开发者快速开发C(或C++)的动态库,并能自动将so和java应用一起打包成apk.这些工具对开发者的帮助是巨大的. NDK集成了交叉编译器,并提供了相应的mk文件隔离

    Android数据存储方式

    在Android开发中,我们有时需要存储一些数据 便于我们清楚知道存储的信息,很直观的看到 可以用来分析.Java中 中也有一些存储方式,在这里我就不一一介绍了,感兴趣的盆友http://blog.csdn.net/aaa1117a8w5s6d/article/details/8251456,今天的重点是来研究Android中的数据存储,Android中提供了几种方式来存储,下面来一一为大家介绍. 在Android系统中 提供了四种存储方式 分别是SQLite .File.SharedPrefe

    Android数据存储方式之:ContentProvider

    ContentProvider是Android的四大组件之一,可见其重要性.我们使用到ContentProvider时,也要同Activity等其它组件一样,在AndroidManifest.xml中注册对应的组件才能使用.为什么会用ContentProvider来作为数据存储方式之一呢? ContentProvider在android中的作用是对外共享数据(应用之间数据共享),可以通过ContentProvider把应用中的数据共享给其他应用访问,其他应用可以通过ContentProvider

    Android数据存储方式之SharedPreferences

    Android平台给我们提供了一个SharedPreferences类,它是一个轻量级的存储类,特别适合用于保存软件配置参数.使用SharedPreferences保存数据,其背后是用xml文件存放数据,文件存放在/data/data/<package name>/shared_prefs目录下: 存储: SharedPreferences sp=getSharedPreferences("UItest",Activity.MODE_APPEND);SharedPrefer

    android 数据存储方式

    韩梦飞沙  韩亚飞  [email protected]  yue31313  han_meng_fei_sha 1,文件 2,内容提供者 3,偏好设置 4,数据库 5,网络存储. 网络存储,就是上传到网络,从网络获取 或者下载 数据.

    Android数据存储之SharedPreferences浅析

    SharedPreferences 是 Android 数据存储方式中的一种,特别适合用来存储少量的.格式简单的数据,比如应用程序的各种配置信息,如是否打开音效,是否开启震动等等. SharedPreferences 存储数据的位置和格式 SharedPreferences 将数据以键值对的形式,存储在 /data/data/<package name>/shared_prefs 目录下面,以 XML 的格式保存,该 XML 文件的根元素是 <map.../>,该元素里每个子元素代

    Android——数据存储(四种方式之一)SharedPrefereces

    Android--数据存储(四种方式) 1.SharedPrefereces   轻量级.XML  存储文件名,数据保存在data/data/basepackage/shared_prefs/myopt.xml中   实例-收藏-记住密码自动登录 //一种轻量级的数据存储方式//通过KEY 存入数据--putxxxx(key,value) 取出数据--getxxxx(key  default)   2.读写SD卡  SD的根目录  适用于数据流读写 3.SQLite  轻量级.dp文件多用于手机

    android五种数据存储方式

    在Android中提供了如下五种数据存储方式: SharedPreferences 文件存储 SQLite数据库方式 内容提供器(ContentProvider) 网络存储 下面具体介绍一下这五种数据存储方式的用法. 1. SharedPreferences SharedPreferences是android平台上的一个轻量级存储类,主要保存一些常用的配置信息.可以保存long.int.String类型数据. SharedPreferences以键值对方式读取和存入,最终会以XML方式保存数据,

    (转载)Android数据存储三种方式总结

    本文转载自:http://www.cnblogs.com/ITtangtang/p/3920916.html 本文介绍Android平台进行数据存储的三大方式,分别如下: 1 使用SharedPreferences存储数据 2 文件存储数据 3 SQLite数据库存储数据 其他: 4 使用ContentProvider存储数据 5 网络存储数据 下面详细讲解这五种方式的特点 第一种: 使用SharedPreferences存储数据     适用范围:保存少量的数据,且这些数据的格式非常简单:字符