作為一部手機,最重要的功能當屬電話、簡訊、連絡人了,所以今天想和大家分享的是關於Android電話、簡訊、連絡人這塊的API介面。
1、通話記錄的擷取
List mRecords=new ArrayList(); Cursor mCursor=mContext.getContentResolver().query(CallLog.Calls.CONTENT_URI, null,null, null,null); if(mCursor!=null && mCursor.moveToFirst()) { do { TelePhoneRecord.getColumnIndex(mCursor); TelePhoneRecord mRecord=new TelePhoneRecord(mCursor); mRecords.add(mRecord); }while(mCursor.moveToNext()); } mCursor.close();
其中TelephoneRecord是對通話記錄的一個實體類,定義如下:
/* * 通話記錄描述類 */package com.android.TelephoneAPIs.Tools;import android.database.Cursor;public class TelePhoneRecord { /**通話記錄ID **/ private long mID; /**電話號碼 **/ private String mNumber; /**通話日期 **/ private long mDate; /** 通話類型 **/ private long mType; /* * 1:來電 * 2:去電 */ private long mDuration; /**通話記錄ID索引**/ private static int Index_ID; /** 電話號碼索引**/ private static int Index_Number; /** 通話日期索引 **/ private static int Index_Date; /** 通話類型索引 **/ private static int Index_Type; /** 通話時間索引 **/ private static int Index_Duration; public static void getColumnIndex(Cursor c) { Index_ID=c.getColumnIndex("_id"); Index_Number=c.getColumnIndex("number"); Index_Date=c.getColumnIndex("date"); Index_Type=c.getColumnIndex("type"); Index_Duration=c.getColumnIndex("duration"); } public TelePhoneRecord(Cursor c) { mID=c.getLong(Index_ID); mNumber=c.getString(Index_Number); mDate=c.getLong(Index_Date); mType=c.getLong(Index_Type); mDuration=c.getLong(Index_Duration); } public long getID() {return mID; } public void setID(long mID) {this.mID = mID; } public String getNumber() {return mNumber; } public void setNumber(String mNumber) {this.mNumber = mNumber; } public long getDate() {return mDate; } public void setDate(long mDate) {this.mDate = mDate; } public long getType() {return mType; } public void setType(long mType) {this.mType = mType; } public long getDuration() {return mDuration; } public void setDuration(long mDuration) {this.mDuration = mDuration; }}
2、來電、去電、短線的監聽
監聽來電:繼承BoardcastReciver在onReceive方法中判斷Intent的類型或者使用TelephoneManager擷取電話狀態
監聽去電:繼承BoardcastReciver在onReceive方法中判斷Intent的類型
監聽簡訊:在BoardcastReciver中,從Bundle中擷取pdus資料,轉換為SmsMessage對象
3、讀取連絡人
List mContacts=new ArrayList(); //此路徑只能查詢連絡人名稱 /*Cursor c1=mContext.getContentResolver().query(Uri.withAppendedPath(ContactsContract.AUTHORITY_URI, "contacts"), null, null, null, null);*/ //此路徑可以查詢連絡人名稱和號碼 Cursor c2=mContext.getContentResolver().query(Uri.withAppendedPath(ContactsContract.AUTHORITY_URI, "data/phones"), null, null, null, null); if(c2!=null && c2.moveToFirst()) { do { Contact.getColumnIndex(c2); Contact mContact=new Contact(c2); mContacts.add(mContact); }while(c2.moveToNext()); } c2.close(); 同樣給出Contact的定義:
package com.android.TelephoneAPIs.Tools;import android.database.Cursor;public class Contact { private String mName; private String mNum; private static int Index_Name; private static int Index_Num; public static void getColumnIndex(Cursor c) { Index_Name=c.getColumnIndex("display_name"); Index_Num=c.getColumnIndex("data1"); } public Contact(Cursor c) { mName=c.getString(Index_Name); mNum=c.getString(Index_Num); } public String getName() {return mName; } public void setName(String mName) {this.mName = mName; } public String getNum() {return mNum; } public void setNum(String mNum) {this.mNum = mNum; }} 4、增加連絡人
ContentValues mValues=new ContentValues(); //擷取RawContactID Uri mRawContactUri=mContext.getContentResolver().insert(RawContacts.CONTENT_URI, mValues); long mRawContactID=ContentUris.parseId(mRawContactUri); mValues.clear(); //設定RawContactID mValues.put(StructuredName.RAW_CONTACT_ID, mRawContactID); //設定MIMETYPE mValues.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE); //設定連絡人姓名 mValues.put(StructuredName.DISPLAY_NAME, Name); //插入連絡人姓名資訊 mContext.getContentResolver().insert(Data.CONTENT_URI, mValues); mValues.clear(); //設定RawContactID mValues.put(Phone.RAW_CONTACT_ID, mRawContactID); //設定MIMETYPE mValues.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE); //設定連絡人號碼 mValues.put(Phone.NUMBER, Num); //插入連絡人號碼資訊 mContext.getContentResolver().insert(Data.CONTENT_URI, mValues);
5、簡訊讀取
/* * 簡訊讀取工具類 * @author:秦元培 * 時間:2014年1月23日 */package com.android.TelephoneAPIs.Tools;import java.util.ArrayList;import java.util.List;import android.content.Context;import android.database.Cursor;import android.net.Uri;public class ShortMessageReader {/** 定義收件匣查詢地址 **/public static final Uri SMS_INBOX=Uri.parse("content://sms/inbox");/** 定義所有資訊查詢地址 **/public static final Uri SMS_ALL=Uri.parse("content://sms");/** 定義寄件匣查詢地址 **/public static final Uri SMS_TOBOX=Uri.parse("content://sms/sent");/** 擷取所有的短訊息**/public static List getAllMessages(Context mContext){ List Messages=new ArrayList(); Messages=queryMessage(mContext,SMS_ALL, null,null); return Messages;}/** 擷取指定號碼的短訊息 **/ public static List getMessageByNumber(Context mContext,Uri mUri,String[]mSelectionAns) { List Messages=new ArrayList(); Messages=queryMessage(mContext, mUri, "address=?",mSelectionAns); return Messages; } /** 擷取指定會話ID的短訊息 **/ public static List getMessageByThreadID(Context mContext,Uri mUri,String[]mSelectionAns) { List Messages=new ArrayList(); Messages=queryMessage(mContext, mUri, "thread_id=?",mSelectionAns); return Messages; } /** 擷取任何滿足條件的短訊息 **/ public static List queryMessage(Context mContext,Uri mUri,String mSelection,String[]mSelectionAns) { List Messages=new ArrayList(); Cursor mCursor=mContext.getContentResolver().query(mUri, null,mSelection,mSelectionAns, null); if(mCursor!=null && mCursor.moveToFirst()) { do { ShortMessage.getColumnIndex(mCursor); ShortMessage mMessage=new ShortMessage(mCursor); /** 添加到Messages **/ Messages.add(mMessage); }while(mCursor.moveToNext()); } mCursor.close();return Messages; }} ShortMessage定義如下:
package com.android.TelephoneAPIs.Tools;import android.database.Cursor;public class ShortMessage { /** 短訊息會話ID **/ private long mThreadID; /** 短訊息Id **/ private long mID; /** 短訊息內容 **/ private String mBody; /** 短訊息主題 **/ private String mSubject; /** 短訊息發送時間 **/ private long mDate; /** 短訊息發送人號碼 **/ private String mAddress; /** 短訊息發送人 **/ private int mPerson; /** 短訊息狀態 **/ private int mRead; /** 短訊息類型 **/ private int mType; /** 擷取會話ID索引**/ private static int Index_Thread_ID; /** 擷取ID索引**/ private static int Index_ID; /** 擷取地址索引 **/ private static int Index_Address; /** 擷取時間索引 **/ private static int Index_Date; /** 擷取主題索引 **/ private static int Index_Subject; /** 擷取內容索引 **/ private static int Index_Body; /** 擷取發送人索引 **/ private static int Index_Person; /** 擷取狀態索引 **/ private static int Index_Read; /** 擷取類型索引 **/ private static int Index_Type; public ShortMessage(Cursor c) { /** 擷取會話ID **/ mThreadID=c.getLong(Index_Thread_ID); /** 擷取ID **/ mID=c.getLong(Index_ID); /** 擷取Address **/ mAddress=c.getString(Index_Address); /** 擷取Subject **/ mSubject=c.getString(Index_Subject); /** 擷取Date **/ mDate=c.getLong(Index_Date); /** 擷取Body **/ mBody=c.getString(Index_Body); /** 擷取Person **/ mPerson=c.getInt(Index_Person); /** 擷取Read **/ mRead=c.getInt(Index_Read); /** 擷取Type **/ mType=c.getInt(Index_Type); } public static void getColumnIndex(Cursor c) { /** 擷取會話ID索引**/Index_Thread_ID=c.getColumnIndex("thread_id");/** 擷取ID索引**/Index_ID=c.getColumnIndex("_id");/** 擷取地址索引 **/Index_Address=c.getColumnIndex("address");/** 擷取時間索引 **/Index_Date=c.getColumnIndex("date");/** 擷取主題索引 **/Index_Subject=c.getColumnIndex("subject");/** 擷取內容索引 **/Index_Body=c.getColumnIndex("body");/** 擷取發送人索引 **/Index_Person=c.getColumnIndex("person");/** 擷取狀態索引 **/Index_Read=c.getColumnIndex("read");/** 擷取類型索引 **/Index_Type=c.getColumnIndex("type"); } public long getThreadID() {return mThreadID; } public void setThreadID(long mThreadID) {this.mThreadID = mThreadID; } public long getID() {return mID; } public void setID(long mID) {this.mID = mID; } public String getBody() {return mBody; } public void setBody(String mBody) {this.mBody = mBody; } public long getDate() { return mDate; } public void setDate(long mDate) {this.mDate = mDate; } public String getAddress() {return mAddress; } public void setAddress(String mAddress) {this.mAddress = mAddress; } public long getRead() {return mRead; } public void setRead(int mRead) {this.mRead = mRead; } public long getPerson() {return mPerson; } public void setPerson(int mPerson) {this.mPerson = mPerson; } public String getSubject() {return mSubject; } public void setSubject(String mSubject) {this.mSubject = mSubject; } public long getType() {return mType; } public void setType(int mType) {this.mType = mType; } }
6、簡訊發送
public static void sendMessage(Context mContext,String mNum,String mContent,boolean mSave) { if(mSave) { SmsManager mManager=SmsManager.getDefault(); mManager.sendTextMessage(mNum, null, mContent, null, null); ContentValues mValues=new ContentValues(); mValues.put("thread_id",getThreadIDByAddress(mContext,new String[]{mNum})); mValues.put("body", mContent); mValues.put("date", new Date().getTime()); mValues.put("address", mNum); mValues.put("type", 2); mValues.put("read", 1); mContext.getContentResolver().insert(Uri.parse("content://sms"), mValues); }else{ SmsManager mManager=SmsManager.getDefault(); mManager.sendTextMessage(mNum, null, mContent, null, null); } } private static long getThreadIDByAddress(Context mContext,String[] SelectionArg) { List mMessages=ShortMessageReader.getMessageByNumber(mContext, ShortMessageReader.SMS_INBOX,SelectionArg ); ShortMessage m=mMessages.get(0); return m.getThreadID(); }
上面的兩個方法分別是傳送簡訊和擷取短線會話ID的方法,可以設定是否儲存到資料庫中
7、將簡訊以Xml檔案形式匯出
public static void OutputByXml(List mMessages,String mXmlFilePath) { File XmlFile = new File(mXmlFilePath); try{ XmlFile.createNewFile(); }catch (IOException e) { e.printStackTrace(); } FileOutputStream fos = null; try{ fos = new FileOutputStream(XmlFile); }catch (FileNotFoundException e) { e.printStackTrace(); } XmlSerializer serializer = Xml.newSerializer(); try { serializer.setOutput(fos,"UTF-8"); serializer.startDocument(null, true); serializer.startTag(null, "ShortMessages"); for(int i = 0; i < mMessages.size(); i ++){ serializer.startTag(null, "Message"); serializer.startTag(null, "Address"); serializer.text(mMessages.get(i).getAddress()); serializer.endTag(null, "Address"); serializer.startTag(null, "Body"); serializer.text(mMessages.get(i).getBody()); serializer.endTag(null, "Body"); serializer.endTag(null, "Message"); } serializer.endTag(null, "ShortMessages"); serializer.endDocument(); serializer.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); } }
以上代碼全部測試通過,要求的權限如下: