1、Android短信数据库表结构
URI主要有:
content://sms/ 所有短信
content://sms/inbox 收件箱
content://sms/sent 已发送
content://sms/draft 草稿
content://sms/outbox 发件箱
content://sms/failed 发送失败
content://sms/queued 待发送列表
sms主要结构:
- _id => 短消息序号 如100
- thread_id => 对话的序号 如100
- address => 发件人地址,手机号.如+8613811810000
- person => 发件人,返回一个数字就是联系人列表里的序号,陌生人为null
- date => 日期 long型。如1256539465022
- protocol => 协议 0 SMS_RPOTO, 1 MMS_PROTO
- read => 是否阅读 0未读, 1已读
- status => 状态 -1接收,0 complete, 64 pending, 128 failed
- type => 类型 1是接收到的,2是已发出
- body => 短消息内容
- service_center => 短信服务中心号码编号。如+8613800755500
所有字段:
sms数据库中的字段如下:
_id 一个自增字段,从1开始
thread_id 序号,同一发信人的id相同
address 发件人手机号码
person 联系人列表里的序号,陌生人为null
date 发件日期
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
locked
字段源码:
private void createSmsTables(SQLiteDatabase db) {
// N.B.: Whenever the columns here are changed, the columns in
// {@ref MmsSmsProvider} must be changed to match.
db.execSQL("CREATE TABLE sms (" +
"_id INTEGER PRIMARY KEY," +
"thread_id INTEGER," +
"address TEXT," +
"person INTEGER," +
"date INTEGER," +
"date_sent INTEGER DEFAULT 0," +
"protocol INTEGER," +
"read INTEGER DEFAULT 0," +
"status INTEGER DEFAULT -1," + // a TP-Status value
// or -1 if it
// status hasn't
// been received
"type INTEGER," +
"reply_path_present INTEGER," +
"subject TEXT," +
"body TEXT," +
"service_center TEXT," +
"locked INTEGER DEFAULT 0," +
"error_code INTEGER DEFAULT 0," +
"seen INTEGER DEFAULT 0" +
");");
....
}
2、操作代码(拿到contentProvider的Uri即可进行增删改查操作十分简易)(注意:敏感数据需要申请权限android.permission.READ_SMS
android.permission.WRITE_SMS)
package com.pas.getsms;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.xmlpull.v1.XmlSerializer;
import com.pas.model.SmsInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.text.format.DateFormat;
import android.util.Xml;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity
{
private List<SmsInfo> smslist;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void click(View view)
{
smslist = new ArrayList<SmsInfo>();
Uri uri = Uri.parse("content://sms/");
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(uri, new String[]
{ "address", "date", "type", "body" }, null, null, null);
while (cursor.moveToNext())
{
String address = cursor.getString(0);
String date = cursor.getString(1);
String type = cursor.getString(2);
String body = cursor.getString(3);
SmsInfo smsInfo = new SmsInfo(address, type, date, body);
smslist.add(smsInfo);
}
cursor.close();
backupData();
}
private void backupData()
{
try
{
String ENCODING = "utf-8";
XmlSerializer serializer = Xml.newSerializer();
File file = new File(this.getExternalFilesDir(null), "back.xml");
FileOutputStream fos = new FileOutputStream(file);
serializer.setOutput(fos, ENCODING);
serializer.startDocument(ENCODING, true);
serializer.startTag(null, "smses");
for (SmsInfo sms : smslist)
{
serializer.startTag(null, "sms");
serializer.startTag(null, "address");
serializer.text(sms.getAddress());
serializer.endTag(null, "address");
serializer.startTag(null, "body");
serializer.text(sms.getBody());
serializer.endTag(null, "body");
serializer.startTag(null, "date");
serializer.text(sms.getDate());
serializer.endTag(null, "date");
serializer.startTag(null, "type");
serializer.text(sms.getType());
serializer.endTag(null, "type");
serializer.endTag(null, "sms");
}
serializer.endTag(null, "smses");
serializer.endDocument();
fos.close();
Toast.makeText(this, "备份成功", Toast.LENGTH_SHORT).show();
} catch (Exception e)
{
e.printStackTrace();
// Toast.makeText(this, e.getMessage(), 0).show();
}
}
public void addSms(View view)
{
new Thread()
{
@Override
public void run()
{
try
{
sleep(50000);
smslist = new ArrayList<SmsInfo>();
Uri uri = Uri.parse("content://sms/");
ContentResolver resolver = getContentResolver();
ContentValues values=new ContentValues();
values.put("address", "95533");
values.put("type", 1);
values.put("date", System.currentTimeMillis());
values.put("body", "到账100,000,000元");
resolver.insert(uri, values);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
}