編輯:關於Android編程
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" />
Afw流程演示Device OwnerL平台恢復出廠設置連接翻牆Wifi直到出現如下界面點擊”Set up work device”,輸入賬號和激
前言很長一段時間沒寫博客了,再不寫點東西真說不過去,把工作上的一些有價值的東西整理出來分享,在當下還有點時效性,不然遲早會爛在肚子裡的。還記得之前小巫有個開源計劃是想實現
1、總圖中的第一步 如圖:第一步 class ActivityManagerProxy implements IActivityManager
MediaPlayer通過如下兩個靜態方法來加載指定的音頻: 1、static MediaPlayer create(Context context,Uri uri