Android开发:还原通讯录、历史通话记录、短信息记录(insert方法)

转自:http://www.2cto.com/kf/201207/141169.html

1.插入通讯录
[java] 
package com.dm.restroe; 
 
import java.util.ArrayList; 
 
import android.content.ContentUris; 
import android.content.ContentValues; 
import android.content.Context; 
import android.net.Uri; 
import android.provider.ContactsContract.CommonDataKinds.Email; 
import android.provider.ContactsContract.CommonDataKinds.Phone; 
import android.provider.ContactsContract.CommonDataKinds.StructuredName; 
import android.provider.ContactsContract.CommonDataKinds.StructuredPostal; 
import android.provider.ContactsContract.Data; 
import android.provider.ContactsContract.RawContacts; 
 
import com.dm.util.ContactInfo; 
 
public class AddContact { 
 
//  private ArrayList<ContactInfo> CIList = new ArrayList<ContactInfo>(); 
 
 
    public static String fileName = ""; 
    public AddContact() { 
        // TODO Auto-generated constructor stub 
    } 
    // CIList存储要插入的通话记录内容的类,cx是调用本方法的类的context 
    public AddContact(ArrayList<ContactInfo> CIList, Context cx) { 
         
        // 用于存储联系人的地址信息 
        String[] address; 
 
        for(ContactInfo ci:CIList){ 
             
            // andress存的内容格式:type+"-"+poBox+"-"+postalCode+"-"+country+"-"+state+"-"+city+"-"+street 
            address = ci.getCiAddr().split("-"); 
             /**
              * 首先向RawContacts.CONTENT_URI执行一个空值插入,目的是获取系统返回的rawContactId 
              * 这时后面插入data表的数据,只有执行空值插入,才能使插入的联系人在通讯录里面可见
              */ 
            ContentValues values = new ContentValues(); 
            //首先向RawContacts.CONTENT_URI执行一个空值插入,目的是获取系统返回的rawContactId  
            Uri rawContactUri = cx.getContentResolver().insert(RawContacts.CONTENT_URI, values); 
            long rawContactId = ContentUris.parseId(rawContactUri); 
            //往data表插入姓名数据 
            values.clear(); 
            values.put(Data.RAW_CONTACT_ID, rawContactId);  
            values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);//内容类型 
            values.put(StructuredName.GIVEN_NAME, ci.getCiName()); 
            cx.getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values); 
            //往data表插入电话数据 
            values.clear(); 
            values.put(Data.RAW_CONTACT_ID, rawContactId); 
            values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE); 
            values.put(Phone.NUMBER, ci.getPhoneNums()); 
            values.put(Phone.TYPE, Phone.TYPE_MOBILE); 
            cx.getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values); 
            //往data表插入Email数据 
            values.clear(); 
            values.put(Data.RAW_CONTACT_ID, rawContactId); 
            values.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE); 
            values.put(Email.DATA, ci.getCiEmail()); 
            values.put(Email.TYPE, Email.TYPE_WORK); 
            cx.getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values); 
            //往data表插入Address数据 
            switch(Integer.valueOf(address[0])){ 
                case 1: // TYPE_HOME 
                    values.clear(); 
                    values.put(Data.RAW_CONTACT_ID, rawContactId); 
                    values.put(Data.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE); 
                    values.put(StructuredPostal.POBOX, address[1]); 
                    values.put(StructuredPostal.TYPE, StructuredPostal.TYPE_HOME); 
                    cx.getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values); 
                     
                    values.clear(); 
                    values.put(Data.RAW_CONTACT_ID, rawContactId); 
                    values.put(Data.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE); 
                    values.put(StructuredPostal.POSTCODE, address[2]); 
                    values.put(StructuredPostal.TYPE, StructuredPostal.TYPE_HOME); 
                    cx.getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values); 
                     
                    values.clear(); 
                    values.put(Data.RAW_CONTACT_ID, rawContactId); 
                    values.put(Data.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE); 
                    values.put(StructuredPostal.COUNTRY, address[3]); 
                    values.put(StructuredPostal.TYPE, StructuredPostal.TYPE_HOME); 
                    cx.getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values); 
                     
                    values.clear(); 
                    values.put(Data.RAW_CONTACT_ID, rawContactId); 
                    values.put(Data.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE); 
                    values.put(StructuredPostal.REGION, address[4]); 
                    values.put(StructuredPostal.TYPE, StructuredPostal.TYPE_HOME); 
                    cx.getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values); 
                     
                    values.clear(); 
                    values.put(Data.RAW_CONTACT_ID, rawContactId); 
                    values.put(Data.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE); 
                    values.put(StructuredPostal.CITY, address[5]); 
                    values.put(StructuredPostal.TYPE, StructuredPostal.TYPE_HOME); 
                    cx.getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values); 
                     
                    values.clear(); 
                    values.put(Data.RAW_CONTACT_ID, rawContactId); 
                    values.put(Data.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE); 
                    values.put(StructuredPostal.STREET, address[6]); 
                    values.put(StructuredPostal.TYPE, StructuredPostal.TYPE_HOME); 
                    cx.getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values); 
                    break; 
                     
                case 2: // TYPE_WORK 
                    values.clear(); 
                    values.put(Data.RAW_CONTACT_ID, rawContactId); 
                    values.put(Data.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE); 
                    values.put(StructuredPostal.POBOX, address[1]); 
                    values.put(StructuredPostal.TYPE, StructuredPostal.TYPE_WORK); 
                    cx.getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values); 
                     
                    values.clear(); 
                    values.put(Data.RAW_CONTACT_ID, rawContactId); 
                    values.put(Data.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE); 
                    values.put(StructuredPostal.POSTCODE, address[2]); 
                    values.put(StructuredPostal.TYPE, StructuredPostal.TYPE_WORK); 
                    cx.getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values); 
                     
                    values.clear(); 
                    values.put(Data.RAW_CONTACT_ID, rawContactId); 
                    values.put(Data.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE); 
                    values.put(StructuredPostal.COUNTRY, address[3]); 
                    values.put(StructuredPostal.TYPE, StructuredPostal.TYPE_WORK); 
                    cx.getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values); 
                     
                    values.clear(); 
                    values.put(Data.RAW_CONTACT_ID, rawContactId); 
                    values.put(Data.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE); 
                    values.put(StructuredPostal.REGION, address[4]); 
                    values.put(StructuredPostal.TYPE, StructuredPostal.TYPE_WORK); 
                    cx.getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values); 
                     
                    values.clear(); 
                    values.put(Data.RAW_CONTACT_ID, rawContactId); 
                    values.put(Data.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE); 
                    values.put(StructuredPostal.CITY, address[5]); 
                    values.put(StructuredPostal.TYPE, StructuredPostal.TYPE_WORK); 
                    cx.getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values); 
                     
                    values.clear(); 
                    values.put(Data.RAW_CONTACT_ID, rawContactId); 
                    values.put(Data.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE); 
                    values.put(StructuredPostal.STREET, address[6]); 
                    values.put(StructuredPostal.TYPE, StructuredPostal.TYPE_WORK); 
                    cx.getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values); 
                    break; 
            } 
             
     
        } 
         
    } 
      
 
 
 

[java] 
package com.dm.util; 
 
import android.database.Cursor; 
import android.provider.ContactsContract; 
 
public class ContactInfo { 
 
    private String contactId;  
      
    private String ciName; 
    private String phoneNums; 
 
    private String ciEmail; 
    private String ciAddr; 
    public String getContactId() { 
        return contactId; 
    } 
    public void setContactId(String contactId) { 
        this.contactId = contactId; 
    } 
    public String getCiName() { 
        return ciName; 
    } 
    public void setCiName(String ciName) { 
        this.ciName = ciName; 
    } 
    public String getPhoneNums() { 
        return phoneNums; 
    } 
    public void setPhoneNums(String phoneNums) { 
        this.phoneNums = phoneNums; 
    } 
    public String getCiEmail() { 
        return ciEmail; 
    } 
    public void setCiEmail(String ciEmail) { 
        this.ciEmail = ciEmail; 
    } 
    public String getCiAddr() { 
        return ciAddr; 
    } 
    public void setCiAddr(String ciAddr) { 
        this.ciAddr = ciAddr; 
    } 
 

2.还原历史通话记录
[java] 
package com.dm.restroe; 
 
import java.util.ArrayList; 
 
import com.dm.util.CallHistory; 
 
import android.content.ContentValues; 
import android.content.Context; 
import android.provider.CallLog; 
 
 
public class AddCallHistory { 
 
     public static final int INCOMING_TYPE = 1; 
     public static final int OUTGOING_TYPE = 2; 
     public static final int MISSED_TYPE = 3; 
     public ContentValues values = new ContentValues(); 
      
     // CHList存储要插入的通话记录内容的类,cx是调用本方法的类的context 
     public AddCallHistory (ArrayList<CallHistory> CHList, Context cx){ 
          
         /**
          *  ch_id  Number(10)  No      通话记录id
          *  ch_dialing_tel     Varchar2(255)   No      主叫人的手机号码
          *  ch_called_tel  Varchar2(255)   No      被叫人手机号码
          *  ch_call_start  date    No  sysdate  通话开始的时间
          *  ch_call_end    date    No  sysdate 通话的结束时间
          *  ch_calt_time   Number(10)  No      通话持续时间
          *  ch_talk_type   Number(1)   No      通话类型,0表示语音通话,1表示视频通话
          *  ch_call_type   Number(1)   No      呼叫类型,0表示呼出电话,1 表示呼入电话,2表示未接电话
          *  ch_du_id   Number(10)  No      用户id
          *  ch_insert_time Date    No  sysdate 入库时间
          *  ch_remark_info Varchar2(2048)          备注
          */ 
         for(CallHistory ch:CHList){ 
              
             values.clear(); 
              
             values.put(CallLog.Calls.NUMBER, ch.getChCalledTel());   
             values.put(CallLog.Calls.DATE,  System.currentTimeMillis());   
             values.put(CallLog.Calls.DURATION, ch.getChCaltTime());   
             values.put(CallLog.Calls.TYPE, ch.getChNumberType());   
             values.put(CallLog.Calls.NEW, "0");//0已看1未看 ,由于没有获取默认全为已读 
                    
             cx.getContentResolver().insert(CallLog.Calls.CONTENT_URI, values);   
         } 
          
          
     } 

[java] 
package com.dm.util; 
 
import android.provider.CallLog.Calls; 
 
public class CallHistory { 
 
    private String chDialingTel; 
    private String chNumberName; 
    private String chNumberLable; 
    private String chNumberType; 
 
    private String chCaltTime; 
    private String chId; 
    private String chCallType; 
     
    public String getChDialingTel() { 
        return chDialingTel; 
    } 
    public void setChDialingTel(String chDialingTel) { 
        this.chDialingTel = chDialingTel; 
    } 
    public String getChCalledTel() { 
        return chCalledTel; 
    } 
    public void setChCalledTel(String chCalledTel) { 
        this.chCalledTel = chCalledTel; 
    } 
    public String getChNumberName() { 
        return chNumberName; 
    } 
    public void setChNumberName(String chNumberName) { 
        this.chNumberName = chNumberName; 
    } 
    public String getChNumberLable() { 
        return chNumberLable; 
    } 
    public void setChNumberLable(String chNumberLable) { 
        this.chNumberLable = chNumberLable; 
    } 
    public String getChNumberType() { 
        return chNumberType; 
    } 
    public void setChNumberType(String chNumberType) { 
        this.chNumberType = chNumberType; 
    } 
    public String getChCaltTime() { 
        return chCaltTime; 
    } 
    public void setChCaltTime(String chCaltTime) { 
        this.chCaltTime = chCaltTime; 
    } 
    public String getChId() { 
        return chId; 
    } 
    public void setChId(String chId) { 
        this.chId = chId; 
    } 
    public String getChCallType() { 
        return chCallType; 
    } 
    public void setChCallType(String chCallType) { 
        this.chCallType = chCallType; 
    } 
    private String chCalledTel; 
 
    // chCall+="\n<chTalkType>"+1+"</chTalkType>"; 
 

3.还原短信息
[java] 
package com.dm.restroe; 
 
import java.util.ArrayList; 
 
import com.dm.util.ContactInfo; 
import com.dm.util.SMS; 
 
import android.content.ContentValues; 
import android.content.Context; 
import android.net.Uri; 
 
public class AddShortMessage { 
 
    public static Uri mSmsUri = Uri.parse("content://sms/inbox");  
     
    public final String ADDRESS = "address";   
    public final String DATE = "date";   
    public final String READ = "read";   
    public final String STATUS = "status";   
    public final String TYPE = "type";   
    public final String BODY = "body";   
    public int MESSAGE_TYPE_INBOX = 1;   
    public int MESSAGE_TYPE_SENT = 2;   
    // 用于存储联系人的地址信息 
    public String[] smRemarkInfo; 
    public ContentValues values = new ContentValues();   
 
    // SMSList存储要插入的通话记录内容的类,cx是调用本方法的类的context     
    public AddShortMessage(ArrayList<SMS> SMSList, Context cx){ 
         
        for(SMS sms:SMSList){ 
             
             
            values.clear(); 
             
            /**
             *  smId:_id
             *  smFilePlace:1
             *  smSendTime:发送时间
             *  smMesContent:短信内容
             *  smMesSize:短信尺寸
             *  smCharacterCount:短信长度
             *  smMesType:短信类型
             *  smRemarkInfo:短信中心 短信主题  短信协议 是否阅读
             *  smAcceptTel:收信人电话
             *  smSendTel:发信人电话 
             */ 
             
            // smRemarkInfo:[0]短信中心 [1]短信主题  [2]短信协议 [3]是否阅读 
            smRemarkInfo = sms.getSmRemarkInfo().split("-");; 
                     
            values.put(ADDRESS, sms.getSmSendTel());   
            values.put(BODY,  sms.getSmMesContent());   
            values.put(DATE, sms.getSmSendTime());          
            values.put(READ, smRemarkInfo[3]);   
            values.put(STATUS, -1);   
            values.put("service_center", smRemarkInfo[0]); 
             
            // 收件箱 
            if(sms.getSmMesType().equals(MESSAGE_TYPE_INBOX)){ 
                mSmsUri = Uri.parse("content://sms/inbox"); 
                values.put(TYPE, MESSAGE_TYPE_INBOX); 
            } 
            // 发件箱 
            if(sms.getSmMesType().equals(MESSAGE_TYPE_SENT)){ 
                mSmsUri = Uri.parse("content://sms/sent"); 
                values.put(TYPE, MESSAGE_TYPE_SENT); 
            } 
             
            cx.getContentResolver().insert(mSmsUri, values);  
             
        } 
          
    } 
     
 

[java] 
package com.dm.util; 
 
import java.text.SimpleDateFormat; 
import java.util.Date; 
 
import android.provider.CallLog.Calls; 
 
public class SMS { 
    private String smSendTel; 
    private String smAcceptTel; 
    private String smId; 
    private String smFilePlace; 
    private String smSendTime; 
    private String smMesContent; 
    private String smMesSize; 
    private String smCharacterCount; 
    private String smMesType; 
    private String smRemarkInfo; 
 
    public String getSmSendTel() { 
        return smSendTel; 
    } 
 
    public void setSmSendTel(String smSendTel) { 
        this.smSendTel = smSendTel; 
    } 
 
    public String getSmAcceptTel() { 
        return smAcceptTel; 
    } 
 
    public void setSmAcceptTel(String smAcceptTel) { 
        this.smAcceptTel = smAcceptTel; 
    } 
 
    public String getSmId() { 
        return smId; 
    } 
 
    public void setSmId(String smId) { 
        this.smId = smId; 
    } 
 
    public String getSmFilePlace() { 
        return smFilePlace; 
    } 
 
    public void setSmFilePlace(String smFilePlace) { 
        this.smFilePlace = smFilePlace; 
    } 
 
    public String getSmSendTime() { 
        return smSendTime; 
    } 
 
    public void setSmSendTime(String smSendTime) { 
        this.smSendTime = smSendTime; 
    } 
 
    public String getSmMesContent() { 
        return smMesContent; 
    } 
 
    public void setSmMesContent(String smMesContent) { 
        this.smMesContent = smMesContent; 
    } 
 
    public String getSmMesSize() { 
        return smMesSize; 
    } 
 
    public void setSmMesSize(String smMesSize) { 
        this.smMesSize = smMesSize; 
    } 
 
    public String getSmCharacterCount() { 
        return smCharacterCount; 
    } 
 
    public void setSmCharacterCount(String smCharacterCount) { 
        this.smCharacterCount = smCharacterCount; 
    } 
 
    public String getSmMesType() { 
        return smMesType; 
    } 
 
    public void setSmMesType(String smMesType) { 
        this.smMesType = smMesType; 
    } 
 
    public String getSmRemarkInfo() { 
        return smRemarkInfo; 
    } 
 
    public void setSmRemarkInfo(String smRemarkInfo) { 
        this.smRemarkInfo = smRemarkInfo; 
    } 


最后是权限设置,别忘了
[java] 
<uses-permission android:name="android.permission.READ_SMS" />   
<uses-permission android:name="android.permission.WRITE_SMS" />    
<uses-permission android:name="android.permission.READ_CONTACTS" />   
<uses-permission android:name="android.permission.WRITE_CONTACTS" />    


你可能感兴趣的:(Android开发:还原通讯录、历史通话记录、短信息记录(insert方法))