As a mobile phone, the most important function is the phone number, text message, and contact. So today I want to share with you the API interface for Android phones, text messages, and contacts.
1. Call record acquisition
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 is an entity class for call records, which is defined as follows:
/ *
* call record description class
* /
Package com. Android. TelephoneAPIs. The Tools;
The import android. Database. Cursor;
Public class TelePhoneRecord
{
/** call record ID **/
Private long mID;
/** phone number **/
Private String mNumber;
/** call date **/
Private long mDate;
/** call type **/
Private long mType;
/ *
* 1: incoming call
* 2: electricity
* /
Private long mDuration;
/** call record ID index **/
Private static int Index_ID;
/** telephone number index **/
Private static int Index_Number;
/** call date index **/
Private static int Index_Date;
/** call type index **/
Private static int Index_Type;
/** airtime index **/
Private static int Index_Duration;
Public static void getColumnIndex(Cursor c)
{
Be sad etColumnIndex chtistina georgina rossetti.british poetess Index_ID = (" _id ");
Be sad etColumnIndex chtistina georgina rossetti.british poetess Index_Number = (" number ");
Be sad etColumnIndex chtistina georgina rossetti.british poetess Index_Date = (" date ");
Be sad etColumnIndex chtistina georgina rossetti.british poetess Index_Type = (" type ");
Be sad etColumnIndex chtistina georgina rossetti.british poetess Index_Duration = (" duration ");
}
Public TelePhoneRecord Cursor (c)
{
Be sad etLong chtistina georgina rossetti.british poetess mID = (Index_ID);
Be sad etString chtistina georgina rossetti.british poetess mNumber = (Index_Number);
Be sad etLong chtistina georgina rossetti.british poetess mDate = (Index_Date);
Be sad etLong chtistina georgina rossetti.british poetess mType = (Index_Type);
Be sad etLong chtistina georgina rossetti.british poetess mDuration = (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) {
Enclosing mNumber = mNumber;
}
Public long getDate () {
Return mDate;
}
Public void setDate(long mDate) {
Enclosing mDate = mDate;
}
Public long getType () {
Return mType;
}
Public void setType(long mType) {
Enclosing mType = mType;
}
Public long getDuration () {
Return mDuration;
}
Public void setDuration(long duration) {
Enclosing mDuration = mDuration;
}
}
2. Call, power-off, and short-term monitoring
Listen for incoming calls: Inherit the BoardcastReciver to determine the Intent type in the onReceive method or use TelephoneManager to get the phone status
Listening to power-off: inherits the BoardcastReciver to determine the Intent type in the onReceive Method
Listen to SMS: In BoardcastReciver, get pdus data from Bundle and convert it to SmsMessage object
3. Read contacts
The List mContacts = new ArrayList ();
// this path can only query the contact name
/ * Cursor c1 = mContext getContentResolver () query (Uri) withAppendedPath (ContactsContract. AUTHORITY_URI,
"Contacts "), null, null, null, null); * /
// this path can query the contact name and number
Cursor c2 = mContext. GetContentResolver () query (Uri) withAppendedPath (ContactsContract. AUTHORITY_URI,
"Data /phones"), null, null, null, null);
If (c2! = null && c2. MoveToFirst ())
{
The do
{
Contact. GetColumnIndex (c2);
Contact mContact = new Contact (c2);
MContacts. Add (mContact);
} while (c2 moveToNext ());
}
C2. The close ();
The definition of Contact is also given:
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. add Contacts
ContentValues mValues = new ContentValues ();
/ / get RawContactID
Uri mRawContactUri = mContext. GetContentResolver (), insert (RawContacts CONTENT_URI, mValues);
Long mRawContactID = ContentUris. ParseId (mRawContactUri);
MValues. The clear ();
/ / set RawContactID
MValues. Put (StructuredName RAW_CONTACT_ID, mRawContactID);
/ / set the MIMETYPE
MValues. Put (Data. The MIMETYPE, StructuredName CONTENT_ITEM_TYPE);
// set the contact name
MValues. Put (StructuredName DISPLAY_NAME, Name);
// insert contact name information
MContext. GetContentResolver (), insert (Data. CONTENT_URI, mValues);
MValues. The clear ();
/ / set RawContactID
MValues. Put (Phone. RAW_CONTACT_ID mRawContactID);
/ / set the MIMETYPE
MValues. Put (Data. The MIMETYPE, StructuredName CONTENT_ITEM_TYPE);
// set the contact number
MValues. Put (Phone NUMBER, Num);
// insert contact number information
MContext. GetContentResolver (), insert (Data. CONTENT_URI, mValues);
5. Read text messages
/ *
* SMS reading tool class
* @author: qin yuanpei
* time: January 23, 2014
* /
Package com. Android. TelephoneAPIs. The Tools;
Import the Java. Util. ArrayList;
Import the Java. Util. List;
The import android. The content. The Context;
The import android. Database. Cursor;
The import android.net.Uri;
Public class ShortMessageReader
{
Define the inbox query address **/
Public static final Uri SMS_INBOX= uri.parse ("content:// SMS /inbox");
/** define all information query address **/
Public static final Uri SMS_ALL= uri.parse ("content:// SMS ");
/** define outbox query address **/
Public static final Uri SMS_TOBOX= uri.parse ("content:// SMS /sent");
/** get all the short messages **/
Public static List getAllMessages(Context mContext)
{
The List of Messages = new ArrayList ();
Messages = queryMessage (mContext SMS_ALL, null, null);
Return Messages.
}
Get the specified number of short message **/
Public static List getMessageByNumber(Context mContext,Uri mUri,String[]mSelectionAns)
{
The List of Messages = new ArrayList ();
Messages = queryMessage (mContext, mUri, "address =?" , mSelectionAns);
Return Messages.
}
Gets the short message for the specified session ID **/
Public static List getMessageByThreadID(Context mContext,Uri mUri,String[]mSelectionAns)
{
The List of Messages = new ArrayList ();
Messages = queryMessage (mContext, mUri, "thread_id =?" , mSelectionAns);
Return Messages.
}
Get any short messages that meet the criteria **/
Public static List queryMessage(Context mContext,Uri mUri,String mSelection,String[]mSelectionAns)
{
The List of Messages = new ArrayList ();
Cursor mCursor = mContext. GetContentResolver () query (mUri, null, mSelection mSelectionAns, null);
If (mCursor! . = null && mCursor moveToFirst ())
{
The do
{
ShortMessage. GetColumnIndex (mCursor);
ShortMessage mMessage = new ShortMessage (mCursor);
/** add to Messages **/
Messages. The add (mMessage);
} while (mCursor moveToNext ());
}
MCursor. Close ();
Return Messages.
}
}
The publish message is defined as follows:
Package com. Android. TelephoneAPIs. The Tools;
The import android. Database. Cursor;
Public class ShortMessage
{
/** short message session ID **/
Private long mThreadID;
/** short message Id **/
Private long mID;
/** short message content **/
Private String mBody;
/** short message subject **/
Private String mSubject;
/** short message sending time **/
Private long mDate;
/** SMS sender number **/
Private String mAddress;
/** short message sender **/
Private int mPerson;
/** short message status **/
Private int mRead;
/** short message type **/
Private int mType;
/** gets the session ID index **/
Private static int Index_Thread_ID;
/** get ID index **/
Private static int Index_ID;
/** get address index **/
Private static int Index_Address;
/** get time index **/
Private static int Index_Date;
/** gets the subject index **/
Private static int Index_Subject;
Get content index **/
Private static int Index_Body;
/** gets sender index **/
Private static int Index_Person;
/** gets the status index **/
Private static int Index_Read;
/** gets the type index **/
Private static int Index_Type;
Public ShortMessage Cursor (c)
{
/** get session ID **/
Be sad etLong chtistina georgina rossetti.british poetess mThreadID = (Index_Thread_ID);
/** get ID **/
Be sad etLong chtistina georgina rossetti.british poetess mID = (Index_ID);
/** gets Address **/
Be sad etString chtistina georgina rossetti.british poetess mAddress = (Index_Address);
/** gets Subject **/
Be sad etString chtistina georgina rossetti.british poetess mSubject = (Index_Subject);
/** get Date **/
Be sad etLong chtistina georgina rossetti.british poetess mDate = (Index_Date);
/** gets Body **/
Be sad etString chtistina georgina rossetti.british poetess mBody = (Index_Body);
/** gets Person **/
Be sad etInt chtistina georgina rossetti.british poetess mPerson = (Index_Person);
/** get Read **/
Be sad etInt chtistina georgina rossetti.british poetess mRead = (Index_Read);
/** gets Type **/
Be sad etInt chtistina georgina rossetti.british poetess mType = (Index_Type);
}
Public static void getColumnIndex(Cursor c)
{
/** gets the session ID index **/
Be sad etColumnIndex chtistina georgina rossetti.british poetess Index_Thread_ID = (" thread_id ");
/** get ID index **/
Be sad etColumnIndex chtistina georgina rossetti.british poetess Index_ID = (" _id ");
/** get address index **/
Be sad etColumnIndex chtistina georgina rossetti.british poetess Index_Address = (" address ");
/** get time index **/
Be sad etColumnIndex chtistina georgina rossetti.british poetess Index_Date = (" date ");
/** gets the subject index **/
Be sad etColumnIndex chtistina georgina rossetti.british poetess Index_Subject = (" subject ");
Get content index **/
Be sad etColumnIndex chtistina georgina rossetti.british poetess Index_Body = (" body ");
/** gets sender index **/
Be sad etColumnIndex chtistina georgina rossetti.british poetess Index_Person = (" person ");
/** gets the status index **/
Be sad etColumnIndex chtistina georgina rossetti.british poetess Index_Read = (" read ");
/** gets the type index **/
Be sad etColumnIndex chtistina georgina rossetti.british poetess Index_Type = (" type ");
}
Public long getThreadID () {
Return mThreadID;
}
Public void setThreadID(long mThreadID) {
Enclosing 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) {
Enclosing mBody = mBody;
}
Public long getDate () {
Return mDate;
}
Public void setDate(long mDate) {
Enclosing mDate = mDate;
}
Public String getAddress () {
Return mAddress;
}
Public void setAddress(String mAddress) {
Enclosing mAddress = mAddress;
}
Public long getRead () {
Return mRead;
}
Public void setRead(int mRead) {
Enclosing mRead = mRead;
}
Public long getPerson () {
Return mPerson;
}
Public void setPerson(int mPerson) {
Enclosing mPerson = mPerson;
}
Public String getSubject () {
Return mSubject;
}
Public void setSubject(String mSubject) {
Enclosing mSubject = mSubject;
}
Public long getType () {
Return mType;
}
Public void setType(int mType) {
Enclosing mType = mType;
}
}
6. send text messages
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)
{
ListmMessages=ShortMessageReader.getMessageByNumber(mContext, ShortMessageReader.SMS_INBOX,SelectionArg );
ShortMessage m=mMessages.get(0);
return m.getThreadID();
}
The two methods above are respectively sending text messages and obtaining short-term session IDs. You can set whether to save the messages to the database.
7. Export text messages in Xml format
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();
}
}
All the above Code passes the test. The required permissions are as follows: