首页 > 代码库 > message

message

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=http://www.mamicode.com/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 = http://www.mamicode.com/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
 
 
技术分享

message