編輯:關於Android編程
作為一部手機,最重要的功能當屬電話、短信、聯系人了,所以今天想和大家分享的是關於Android電話、短信、聯系人這塊的API接口。
1、通話記錄的獲取
ListmRecords=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;
}
}
監聽來電:繼承BoardcastReciver在onReceive方法中判斷Intent的類型或者使用TelephoneManager獲取電話狀態
監聽去電:繼承BoardcastReciver在onReceive方法中判斷Intent的類型
監聽短信:在BoardcastReciver中,從Bundle中獲取pdus數據,轉換為SmsMessage對象
3、讀取聯系人
List同樣給出Contact的定義: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();
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;
}
}
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();
} public static void OutputByXml(ListmMessages,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(); } }