安卓的SMS 短信的增删改查

这是安卓学习之旅1——-

关于安卓的SMS 短信的增删改查

首先我们要知道是的是我们用的是 ContentResolver cr = getContentResolver(); 这个是内容提供者。
cr有4个方法
1:查询
2:增加
3:删除
4:更新

1.查询:
原型:query(table,columns, selection, selectionArgs, groupBy, having, orderBy, limit)
例子:Cursor cur = cr.query(SMS_INBOX, projection, null, null, “date desc”);
参数解析: 可以参考这个
table:表名。
columns:列名。
selection:选择条件 类似 where 后的一串
selectionArgs:这是为了避免出现 name=’1’传入” 的做法,可以new String[] {name}, 在selection前面使用?代替 (name 是 字符类型 ,所以在sql里 是要加引号的),可参考query() 中的 selectionArgs 的用法
groupBy:分组
having :
orderby desc(倒序 大——小) asc(正序 小——大)


例子解析:
private Uri SMS_INBOX = Uri.parse(“content://sms/”);
SMS_INBOX 是 一个URI 地址,指向的是所有的全部短信
projection 是选出来的列,当然也可以默认。
String[] projection = new String[] { “_id”, “address”, “person”, “body”, “date”, “type”, };
之后要解释的是有多少个列名:
  _id:短信序号,如100
  
  thread_id:对话的序号,如100,与同一个手机号互发的短信,其序号是相同的
  
  address:发件人地址,即手机号,如+86138138000
  
  person:发件人,如果发件人在通讯录中则为具体姓名,陌生人为null
  
  date:日期,long型,如1346988516,可以对日期显示格式进行设置
  
  protocol:协议0SMS_RPOTO短信,1MMS_PROTO彩信
  
  read:是否阅读0未读,1已读
  
  status:短信状态-1接收,0complete,64pending,128failed
  
  type:短信类型1是接收到的,2是已发出
  
  body:短信具体内容
  
  service_center:短信服务中心号码编号,如+8613800755500


下一步是getColumnIndex,获取列名 :例: int index_Address = cur.getColumnIndex(“address”);
之后获取对应的值:String strbody = cur.getString(index_Body); //获取了短信的内容

这里注意的是Data 时间的获取。 这里时间是用的Unix 时间戳。需要用

 //转换Linux 时间戳 很关键//

                    long longDate = cur.getLong(index_Date);  
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");  
                    Date d = new Date(longDate);  
                    String strDate = dateFormat.format(d);    // 格式化时间


 //转换Linux 时间戳 很关键//

java.lang.String.contains(“sms_body”) 方法返回true,当且仅当此字符串包含指定的char值序列
可以用来匹配短信内容
代码如下:
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
private Button myButton;
private Uri SMS_INBOX = Uri.parse(“content://sms/”);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myButton = (Button) findViewById(R.id.button1);

}

public void xtx(View view) {
    Toast.makeText(getApplicationContext(), "ok", 0).show();
    getSmsFromPhone();
}

private void getSmsFromPhone() {
    // TODO Auto-generated method stub
     ContentResolver cr = getContentResolver();  
     String[] projection = new String[] { "_id", "address", "person",
             "body", "date", "type", };  //"_id", "address", "person",, "date", "type  
        Cursor cur = cr.query(SMS_INBOX, projection, null, null, "date desc");  
        if (cur.moveToFirst()) {  
            int index_Address = cur.getColumnIndex("address");  
            int index_Person = cur.getColumnIndex("person");  
            int index_Body = cur.getColumnIndex("body");  
            int index_Date = cur.getColumnIndex("date");  
            int index_Type = cur.getColumnIndex("type");  
            do {  
                String strAddress = cur.getString(index_Address);  
                int intPerson = cur.getInt(index_Person);  
                String strbody = cur.getString(index_Body);  
                int intType = cur.getInt(index_Type);   
                //转换Linux 时间戳 很关键//

                long longDate = cur.getLong(index_Date);  
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");  
                Date d = new Date(longDate);  
                String strDate = dateFormat.format(d);    // 格式化时间


                //转换Linux 时间戳 很关键//


                Log.e("tag", "发件人:"+strAddress+", "+strDate+","+strbody+"\n");
            } while (cur.moveToNext());  

            //增加---
            ContentValues values = new ContentValues();  
            values.put("address", "10010");  
            values.put("type", "1");  
            values.put("body", "喜欢你啊---小贺志霄");  
            values.put("date",System.currentTimeMillis());  
            cr.insert(SMS_INBOX, values);        
            ///----
            cr.delete(SMS_INBOX, "address=95533",null);            //删除
            ContentValues values_1 = new ContentValues(); 
            values_1.put("read",  "0");
            cr.update(SMS_INBOX, values_1, "address=10010",null);  //更新
        Log.e("tag", "Other");
}
}

}
`
最后权限什么的小注意一下:

<uses-permission android:name="android.permission.READ_SMS" />   <!-- 读写短信 -->>
<uses-permission android:name="android.permission.WRITE_SMS"/>

第一次写博客,写的比较渣:: 还可以在改进,路一步步走,嗯嗯!!

一些其中解决问题的参考链接:

http://blog.csdn.net/gubaohua/article/details/575488/ //这是SimpleDateFormat使用详解
http://blog.csdn.net/tianyitianyi1/article/details/18037911 //这是 Android中读取短信
http://blog.csdn.net/mad1989/article/details/22426415/ 这是 Android中读取短信(多了监听)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值