009_01访问系统短信数据库
系统短息数据库mmssms.db存放位置:
导出mmssms.db,使用SQLite Expert打开,可以看到有多张表:
打开sms表格:重要的列名有_id, address, type, body
访问短信数据库的uri
content://sms/inbox 收件箱
content://sms/sent 已发送
content://sms/draft 草稿
content://sms/outbox 发件箱
content://sms/failed 发送失败
content://sms/queued 待发送列表
数据库相关字段如下:
_id 一个自增字段,从1开始
thread_id 序号,同一发信人的id相同
address 发件人手机号码
person 联系人列表里的序号,陌生人为null
date 发件日期,单位是milliseconds,从1970/01/01至今所经过的时间
protocol 协议,分为: 0 SMS_RPOTO, 1 MMS_PROTO
read 是否阅读,0未读, 1已读
status 状态,-1接收,0 complete, 64 pending, 128 failed
type ALL = 0;
INBOX = 1;
SENT = 2;
DRAFT = 3;
OUTBOX = 4;
FAILED = 5;
QUEUED = 6;
body 短信内容
service_center 短信服务中心号码编号
subject 短信的主题
reply_path_present TP-Reply-Path
MainActivity.java
1 package com.example.visit_sms_db; 2 3 import android.app.Activity; 4 import android.content.ContentResolver; 5 import android.content.ContentValues; 6 import android.database.Cursor; 7 import android.net.Uri; 8 import android.os.Bundle; 9 import android.util.Log; 10 import android.view.Menu; 11 import android.view.MenuItem; 12 import android.view.View; 13 14 public class MainActivity extends Activity { 15 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity_main); 20 21 } 22 23 //访问系统短信数据 24 public void getSms(View v){ 25 ContentResolver cr = getContentResolver(); 26 Cursor c = cr.query(Uri.parse("content://sms"), new String[]{"_id", "address", "type", "body"}, null, null, null); 27 while(c.moveToNext()){ 28 int id = c.getInt(0); 29 String number = c.getString(1); 30 int type = c.getInt(2); 31 String body =c.getString(3); 32 Log.i("getSms", id+"," + number + "," + type + "," + body); 33 } 34 } 35 36 public void addSms(View v){ 37 ContentResolver cr = getContentResolver(); 38 ContentValues contentValues = new ContentValues(); 39 contentValues.put("_id", 5); 40 contentValues.put("address", 95555+""); 41 contentValues.put("body", "您的尾号为9527的招行银行卡收到xxx转账1000000,00.请查收!"); 42 cr.insert(Uri.parse("content://sms"), contentValues); 43 } 44 }
activity_main.xml
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context="com.example.visit_sms_db.MainActivity" 10 android:orientation="vertical" > 11 12 <TextView 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:text="@string/hello_world" /> 16 <Button 17 android:layout_width="wrap_content" 18 android:layout_height="wrap_content" 19 android:text="获取系统短信" 20 android:onClick="getSms" 21 /> 22 23 <Button 24 android:layout_width="wrap_content" 25 android:layout_height="wrap_content" 26 android:text="插入系统短信" 27 android:onClick="addSms" 28 /> 29 </LinearLayout>
物随心转,境由心造,一切烦恼皆由心生。