查看短信需要使用系统提供的内容提供者,短信的数据库在com.android.providers.telephony下datebase目录下的mmssms.db中,如下
sms表:
body:短信内容
date:短信时间
address:对方的号码
type:发送还是接收
案例1:获取短信并备份
读取短信需要权限
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
布局文件activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="获取系统短信"
android:onClick="click1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="备份系统短信"
android:onClick="click2"
/>
</LinearLayout>
bean文件,Message.java
package com.ldw.messageProvider.bean;
public class Message {
private String address;
private Long date;
private String body;
private String type;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Long getDate() {
return date;
}
public void setDate(Long date) {
this.date = date;
}
public String getBody() {
return body;
}
public void setBody(String content) {
this.body = content;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "Message [address=" + address + ", date=" + date + ", body="
+ body + ", type=" + type + "]";
}
public Message(String address, Long date, String body, String type) {
super();
this.address = address;
this.date = date;
this.body = body;
this.type = type;
}
}
MainActivity.java
package com.ldw.messageProvider;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlSerializer;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Xml;
import android.view.View;
import com.ldw.messageProvider.bean.Message;
public class MainActivity extends Activity {
List<Message> smsList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
smsList = new ArrayList<Message>();
}
public void click1(View v){
//访问内容提供者
ContentResolver cr = getContentResolver();
//根据源代码,查询所有的信息不需要匹配参数,里面还有其他的参数,例如inbox,sent,draft等等
//短信内容提供者的主机名Uri:sms
Cursor cursor = cr.query(Uri.parse("content://sms"), new String[]{"address", "date", "body", "type"}, null, null, null);
while(cursor.moveToNext()){
String address = cursor.getString(0);
long date = cursor.getLong(1);
String body = cursor.getString(2);
String type = cursor.getString(3);
Message sms = new Message(address, date, body, type);
smsList.add(sms);
System.out.println(address + ";" + date + ";" + body + ";" + type);
}
}
//备份短信
public void click2(View v){
XmlSerializer xs = Xml.newSerializer();
File file = new File("sdcard/sms.xml");
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
xs.setOutput(fos, "utf-8");
xs.startDocument("utf-8", true);
xs.startTag(null, "message");
for(Message sms:smsList){
xs.startTag(null, "sms");
xs.startTag(null, "address");
xs.text(sms.getAddress());
xs.endTag(null, "address");
xs.startTag(null, "date");
xs.text(sms.getDate() + "");
xs.endTag(null, "date");
xs.startTag(null, "body");
xs.text(sms.getBody());
xs.endTag(null, "body");
xs.startTag(null, "type");
xs.text(sms.getType());
xs.endTag(null, "type");
xs.endTag(null, "sms");
}
xs.endTag(null, "message");
xs.endDocument();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
案例2:插入系统短信
权限
<uses-permission android:name="android.permission.WRITE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="insert"
android:onClick="click"
/>
</RelativeLayout>
MainActivity.java
package com.ldw.insertsms;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View v){
//访问内容提供者
ContentResolver cr = getContentResolver();
//根据源代码,查询所有的信息不需要匹配参数,里面还有其他的参数,例如inbox,sent,draft等等
//短信内容提供者的主机名Uri:sms
ContentValues values = new ContentValues();
values.put("address", 99999);
values.put("date", System.currentTimeMillis());
values.put("body", "ninhaoaaaaaaaaaaaaaaa");
values.put("type", 1);
cr.insert(Uri.parse("content://sms"), values);
}
}