• android101 获取、备份、插入短信


    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"/>
  • 相关阅读:
    怎么查看当前进程?怎么执行退出?怎么查看当前路径?
    简述正则表达式及其用途?
    Java 中,抽象类与接口之间有什么不同?
    哪个命令专门用来查看后台任务?
    什么是线程池?有哪几种创建方式?
    什么是多线程的上下文切换?
    WebApplicationContext?
    synchronized、volatile、CAS 比较?
    使用 Spring 有哪些方式?
    线程池的优点?
  • 原文地址:https://www.cnblogs.com/yaowen/p/4972739.html
Copyright © 2020-2023  润新知