天天看点

Android 手机短信简单开发

使用到的权限有:

<uses-permission android:name="android.permission.READ_SMS"/>
    <uses-permission android:name="android.permission.WRITE_SMS"/>
           

通过ContentResover查询删除添加修改短信信息

1.查询打印所有的短信信息

cursor=context.getContentResolver().query(Uri.parse("content://sms/sent"),null,null,null, "_id desc");
        if(cursor!=null){
            while(cursor.moveToNext()){
                   for(int i=0;i<cursor.getColumnCount();i++){
                   Log.i("mi", ""+cursor.getColumnName(i)+":"+cursor.getString(i));
                   }
            }
           cursor.close();
       }
           

会得到如下信息:

    _id:1980           //词条短信的唯一id

  thread_id:359                 //会话(和同一个人发送的所有短信在一个会话里)的ID

  address:106571005610093       //手机号

  person:null

  date:1413799125022            //发送或接收时间

  date_sent:1413799118000       //这个不知道是什么时间,

  protocol:0

  read:1                        //是否已读   0未读;1已读

  status:-1

  type:1                        //1对方发送;2自己发送

  reply_path_present:0

  subject:null

  body:【微店】<交易完成>买家:该笔订  单支持7天无理由退货,货款将在7天后结算,您可以在“我的收入”中查询。

  service_center:+8613800100558

  locked:0

  error_code:0

  seen:1

  deletable:0

  hidden:0

  group_id:null

  group_type:null

  delivery_date:null

  app_id:0

  msg_id:0

  callback_number:null

  reserved:0

  pri:0

  teleservice_id:0

  link_url:null

  svc_cmd:0

  svc_cmd_content:null

  roam_pending:0

其中比较重要的字段有:_id,thread_id,address,date,read,type,body

content://sms                 是查询所有的短信

content://sms/inbox        是查询所有收到的短信

content://sms/sent        是查询所有发出的短信

2.插入一条短信信息

/**
	 * 插入一条短信到数据库
	 */
	public void insertSms(){
		ContentResolver cr=context.getContentResolver();
		ContentValues values=new ContentValues();
	//        String _id=getNewSms_id();
        //        values.put("_id", _id);//每次执行插入操作,此处的id必须是唯一的;也可以不用写入此值,数据库会自动赋值(primary key)
		values.put("thread_id", 359);
		values.put("address", "106571005610093");
		values.put("date", System.currentTimeMillis());
		values.put("date_sent", "1410799125022");
		values.put("read", 1);
		values.put("type", 1);
		values.put("body", "苍天已死,黄天当立");
		cr.insert(Uri.parse("content://sms/inbox"), values);
	}
           

3.删除一短信

public void deleteSms(){
		ContentResolver cr=context.getContentResolver();
		int result=cr.delete(Uri.parse("content://sms"), "_id=?", new String[]{"1990"});
		Log.i("mi", "result==="+result);//0失败,1成功
	}
           

“_id=?”,删除的条件

new [] String{"1990"}   给删除的条件赋值