[toc]
批量插入
批量插入通話記錄
下面是添加通話數據的代碼,我們自己去構造Tb_calllogs數據,里面可以添加通話記錄數據字段,由于是個簡單的demo,所以只加了number、callType、date、callDuration。
/**
* 添加通話數據
*
* @throws RemoteException
* @throws OperationApplicationException
*/
public void addTbCalllogs() throws RemoteException, OperationApplicationException {
int calllogNumber = 0;
if (calllogEditText.getText() != null) {
calllogNumber = Integer.parseInt(calllogEditText.getText().toString());
}
if (calllogNumber == 0) {
Toast.makeText(this, "請輸入正確的條數", Toast.LENGTH_SHORT).show();
return;
}
for (int i = 0; i < calllogNumber; i++) {
Tb_calllogs tb_calllogs = new Tb_calllogs(getNumber(),
getCallType(), getDate(), getCallDuration());
mTbCalllogList.add(tb_calllogs);
}
showProgressDialog();
}
生成隨機號碼代碼如下,默認直接136開頭,如果有需求可以替換成其它號碼。
private String getNumber() {
StringBuilder number = new StringBuilder("136");
Random random = new Random();
for (int i = 0; i < 8; i++) {
number.append(random.nextInt(9));
}
return number.toString().toLowerCase();
}
批量添加通話記錄如下,通話applyBatch()方法去批量添加
public void BatchAddCallLogs()
throws RemoteException, OperationApplicationException {
ArrayList ops = new ArrayList();
ContentValues values = new ContentValues();
for (Tb_calllogs calllog : mTbCalllogList) {
values.clear();
values.put(CallLog.Calls.NUMBER, calllog.getmNumber());
values.put(CallLog.Calls.TYPE, calllog.getmCallLogType());
values.put(CallLog.Calls.DATE, calllog.getmCallLogDate());
values.put(CallLog.Calls.DURATION, calllog.getmCallLogDuration());
values.put(CallLog.Calls.NEW, "0");
ops.add(ContentProviderOperation
.newInsert(CallLog.Calls.CONTENT_URI).withValues(values)
.withYieldAllowed(true).build());
}
if (ops != null) {
getContentResolver().applyBatch(CallLog.AUTHORITY, ops);
}
}
批量插入聯系人
先構造聯系人數據Tb_contacts,目前只加了name和number,代碼如下
public void addTbContacts() throws RemoteException, OperationApplicationException {
int contactsNumber = 0;
if (contactsEditText.getText() != null) {
contactsNumber = Integer.parseInt(contactsEditText.getText().toString());
}
if (contactsNumber == 0) {
Toast.makeText(this, "請輸入正確的條數", Toast.LENGTH_SHORT).show();
return;
}
for (int i = 0; i < contactsNumber; i++) {
Tb_contacts tb_contacts = new Tb_contacts(getName(), getNumber());
mTbContactsList.add(tb_contacts);
}
showProgressDialog();
}
生成隨機漢子,隨便加了兩個漢子,代碼如下:
private String getName() {
StringBuilder stringBuilder = new StringBuilder("");
int hightPos;
int lowPos;
Random random = new Random();
for (int i = 0; i < 2; i++) {
hightPos = (176 + Math.abs(random.nextInt(39)));
lowPos = (161 + Math.abs(random.nextInt(93)));
byte[] b = new byte[2];
b[0] = (Integer.valueOf(hightPos)).byteValue();
b[1] = (Integer.valueOf(lowPos)).byteValue();
try {
stringBuilder.append(new String(b, "GBK"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return stringBuilder.toString().toLowerCase();
}
批量添加到聯系人,具體代碼如下:
public void BatchAddContact()
throws RemoteException, OperationApplicationException {
ArrayList ops = new ArrayList();
int rawContactInsertIndex = 0;
for (Tb_contacts contact : mTbContactsList) {
rawContactInsertIndex = ops.size();
ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
.withYieldAllowed(true).build());
// 添加姓名
ops.add(ContentProviderOperation
.newInsert(
android.provider.ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,
rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, contact.getName())
.withYieldAllowed(true).build());
// 添加號碼
ops.add(ContentProviderOperation
.newInsert(
android.provider.ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,
rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, contact.getNumber())
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
.withValue(ContactsContract.CommonDataKinds.Phone.LABEL, "").withYieldAllowed(true).build());
}
if (ops != null) {
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
}
}
批量插入短信
同樣的先構造Tb_mms數據,添加了number,smsType,smsIsRead等字段。添加短信數據如下:
private void addTbMms() throws RemoteException, OperationApplicationException {
int mmsNumber = 0;
if (mmsEditText.getText() != null) {
mmsNumber = Integer.parseInt(mmsEditText.getText().toString());
}
if (mmsNumber == 0) {
Toast.makeText(this, "請輸入正確的條數", Toast.LENGTH_SHORT).show();
return;
}
for (int i = 0; i < mmsNumber; i++) {
Tb_sms tb_mms = new Tb_sms(getNumber(), getSmsType(), getSmsIsRead());
mTb_sms.add(tb_mms);
}
showProgressDialog();
}
插入短信數據代碼如下:
private void insertSms() {
for (Tb_sms sms : mTb_sms) {
ContentValues values = new ContentValues();
values.put(Telephony.Sms.ADDRESS, sms.getNumber());
values.put(Telephony.Sms.DATE, System.currentTimeMillis());
long dateSent = System.currentTimeMillis() - 5000;
values.put(Telephony.Sms.DATE_SENT, dateSent);
values.put(Telephony.Sms.READ, sms.isRead());
//values.put(Telephony.Sms.SEEN, false);
values.put(Telephony.Sms.TYPE, sms.getType());
//values.put(Telephony.Sms.STATUS, Telephony.Sms.STATUS_COMPLETE);
values.put(Telephony.Sms.BODY, "您尾號為9386的招行儲蓄卡收到轉賬1,000,000人民幣");
values.put(Telephony.Sms.TYPE, Telephony.Sms.MESSAGE_TYPE_INBOX);
getContentResolver().insert(Telephony.Sms.CONTENT_URI, values);
}
}
PS: Android4.4之后,插入短信數據需要修改幾個地方,在AndroidManifest.xml里面的Activity添加如下代碼:
在MainActivity.java里面添加如下代碼去獲取默認短信應用:
defaultSmsPkg = Telephony.Sms.getDefaultSmsPackage(this);
mySmsPkg = this.getPackageName();
if (!defaultSmsPkg.equals(mySmsPkg)) {
Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, mySmsPkg);
startActivity(intent);
}
結束的時候需要在把默認的短信數據替換代碼如下:
String myPackageName = getPackageName();
if (Telephony.Sms.getDefaultSmsPackage(this)
.equals(myPackageName)) {
Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
//這里的defaultSmsApp是前面保存的
intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, defaultSmsPkg);
startActivity(intent);
}