读写系统数据库中的短信:ContentResolver

      /**
       * 读取系统短信方法
       *   @param   view
       */
       public   void   readSms(View view){
            
            ContentResolver  resolver=getContentResolver();       //创建ContentResolver对象,得到内容解析者
            
          Uri uri= Uri. parse( "content://sms/" );         //全部短信的uri
         
          Cursor cursor = resolver.query(uri,   null ,   null ,   null ,   null );     //得到结果集
         
            while (cursor.moveToNext()){
            String address=cursor.getString(cursor.getColumnIndex( "address"   ));     //得到地址
             long   date=cursor.getLong(cursor.getColumnIndex(   "date" ));       //得到时间
            String  body=cursor.getString(cursor.getColumnIndex( "body"   ));       //内容
            String  type=cursor.getString(cursor.getColumnIndex( "type"   ));       //短信类型
            
            SmsBean  smsBean=   new   SmsBean();
            smsBean.setAddress(address);
            smsBean.setBody(body);
            smsBean.setDate(date);
            smsBean.setType(type);
            
            
            
               tv .setText(smsBean.toString());
            
            
               /*if("1".equals(type)){
                  System.out.println("接收到的短信:");
            }else{
                  
                  System.out.println("发送的短信");
            }
            
            System.out.println("号码:"+address+"   时间:"+date+"   内容:"+body);    //打印短信信息
            */
      
          }
         
          cursor.close();
            
      }



---------------------------------------------------------------------------------------------------



public   void   testBatch()   throws   Exception {
            ContentResolver resolver = getContext().getContentResolver();
            ArrayList<ContentProviderOperation> list =   new   ArrayList<ContentProviderOperation>();
            
            ContentProviderOperation operation1 = ContentProviderOperation.newInsert( rawContactsUri )
                        .withValue(   "_id" ,   null )
                        .build();
            
            ContentProviderOperation operation2 = ContentProviderOperation.newInsert( dataUri )
                        .withValueBackReference(   "raw_contact_id" , 0)         // 把同组第一个操作的结果作为值
                        .withValue(   "mimetype" ,   "vnd.android.cursor.item/name"   )
                        .withValue(   "data1" ,   "CR" )
                        .build();
            
            ContentProviderOperation operation3 = ContentProviderOperation.newInsert( dataUri )
                        .withValueBackReference(   "raw_contact_id" , 0)    
                        .withValue(   "mimetype" ,   "vnd.android.cursor.item/phone_v2"   )
                        .withValue(   "data1" ,   "131999888888" )
                        .build();
            
            ContentProviderOperation operation4 = ContentProviderOperation.newInsert( dataUri )
                        .withValueBackReference(   "raw_contact_id" , 0)    
                        .withValue(   "mimetype" ,   "vnd.android.cursor.item/email_v2"   )
                        .withValue(   "data1" ,   "cr@hotmail.com" )
                        .build();
            
            list.add(operation1);
            list.add( operation2);
            list.add(operation3);
            list.add(operation4);
            
            resolver.applyBatch(   "com.android.contacts" , list);
      }

}










      
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值