package com.itheima.getsms;
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 com.itheima.getsms.domain.Message;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.util.Xml;
import android.view.Menu;
import android.view.View;
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 click(View v){
//访问内容提供者获取短信
ContentResolver cr = getContentResolver();
// 短信内容提供者的主机名,sms是系统的短信数据库的内容提供者,sms/send表示查询发送的短信,不写表示所有短信。
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(body, type, address, date);
smsList.add(sms);
}
}
//备份系统短信
public void click2(View v){
XmlSerializer xs = Xml.newSerializer();
File file = new File("sdcard/sms.xml");//短信备份的地方sd卡中
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, "body");
xs.text(sms.getBody());
xs.endTag(null, "body");
xs.startTag(null, "date");
xs.text(sms.getDate() + "");
xs.endTag(null, "date");
xs.startTag(null, "type");
xs.text(sms.getType());
xs.endTag(null, "type");
xs.startTag(null, "address");
xs.text(sms.getAddress());
xs.endTag(null, "address");
xs.endTag(null, "sms");
}
xs.endTag(null, "message");
xs.endDocument();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//清单文件<uses-permission android:name="android.permission.READ_SMS"/>
//<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
package com.itheima.insertsms;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.view.Menu;
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){
Thread t = new Thread(){
@Override
public void run() {
try {
sleep(7500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put("address", 95555);
values.put("type", 1);
values.put("date", System.currentTimeMillis());//System.currentTimeMillis()是long类型
values.put("body", "您尾号为XXXX的信用卡收到1,000,000RMB转账,请注意查收");
cr.insert(Uri.parse("content://sms"), values);
}
};
t.start();
}
}
//<uses-permission android:name="android.permission.WRITE_SMS"/>
//<uses-permission android:name="android.permission.READ_SMS"/>