message
阿新 • • 發佈:2017-06-29
out array right 一行 bsp androi album msi ugo 1.SmsInfo
public class SmsInfo {
private int id;
private long data;
private int type;
private String body;
private String address;
public SmsInfo(long data, int type, String body, String address){
this.data=data;
this.address=address;
this.type=type;
this.body=body;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public long getData() {
return data;
}
public void setData(long data) {
this.data = data;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
2.SmsUtils
public class SmsUtils {
public static void backUpSms(List smsInfoList,Context context) throws IOException {
//創建序列化器serializer
XmlSerializer serializer= Xml.newSerializer();
//獲取sd卡根目錄的路徑
File file=new File(Environment.getExternalStorageDirectory(),"sms.xml");
//獲得輸出流
FileOutputStream os=new FileOutputStream(file);
//設置編碼方式
serializer.setOutput(os,"utf-8");
//寫入xml文件標識
serializer.startDocument("utf-8",true);
serializer.startTag(null,"smss");
for(SmsInfo info:smsInfoList){
serializer.startTag(null,"sms");
serializer.attribute(null,"id",info.getId()+"");
serializer.startTag(null,"body");
serializer.text(info.getBody());
serializer.endTag(null,"body");
serializer.startTag(null,"type");
serializer.text(info.getType()+"");
serializer.endTag(null,"type");
serializer.startTag(null,"address");
serializer.text(info.getAddress());
serializer.endTag(null,"address");
serializer.startTag(null,"date");
serializer.text(info.getData()+"");
serializer.endTag(null,"date");
serializer.endTag(null,"sms");
}
serializer.endTag(null,"smss");
serializer.endDocument();
Toast.makeText(context,"成功",Toast.LENGTH_LONG).show();
}
}
3Duanxin.Activity
public class DuanxinActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_duanxin);
}
public void onclick(View view) throws IOException {
//content://sms查新系統所有短信的uri
Uri uri= Uri.parse("content://sms");
//獲得 訪問內容提供者
ContentResolver contentResolver=getContentResolver();
//通過contentResolver查詢數據
Cursor cursor=contentResolver.query(uri,new String[]{"address","date","type","body"},null,null,null);
List smsInfoList=new ArrayList();
//cursor.moveToNext()移動到下一行
while (cursor.moveToNext()){
String address=cursor.getString(0);
long date=cursor.getLong(1);
int type=cursor.getType(2);
String body=cursor.getString(3);
SmsInfo smsInfo=new SmsInfo(date,type,body,address);
smsInfoList.add(smsInfo);
}
//讀取數據結束
cursor.close();
//備份
SmsUtils.backUpSms(smsInfoList,this);
}
}
4.布局文件
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
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="com.example.believer.gougoude.DuanxinActivity">
android:layout_centerHorizontal="true"
android:onClick="onclick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="獲取"/>
5.Android
5.Android
message