Android 的SMS讀取簡訊,可以擷取發信人/收信人的手機號碼(address),Contacts的連絡人,可以過濾手機號碼(address),因此SMS可以通過手機號碼(address)關聯到Contacts連絡人
SMS - Contacts 關聯代碼
// 通過address手機號關聯Contacts連絡人的顯示名字<br />private String getPeopleNameFromPerson(String address){<br />if(address == null || address == ""){<br />return "( no address )\n";<br />}</p><p>String strPerson = "null";<br />String[] projection = new String[] {Phone.DISPLAY_NAME, Phone.NUMBER};</p><p>Uri uri_Person = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, address);// address 手機號過濾<br />Cursor cursor = getContentResolver().query(uri_Person, projection, null, null, null);</p><p>if(cursor.moveToFirst()){<br />int index_PeopleName = cursor.getColumnIndex(Phone.DISPLAY_NAME);<br />String strPeopleName = cursor.getString(index_PeopleName);<br />strPerson = strPeopleName;<br />}<br />cursor.close();</p><p>return strPerson;<br />}
SMS - Contacts 關聯範例程式碼:
package com.homer.phone;</p><p>import java.sql.Date;<br />import java.text.SimpleDateFormat;</p><p>import android.app.Activity;<br />import android.database.Cursor;<br />import android.database.sqlite.SQLiteException;<br />import android.net.Uri;<br />import android.os.Bundle;<br />import android.provider.ContactsContract;<br />import android.provider.ContactsContract.CommonDataKinds.Phone;<br />import android.util.Log;<br />import android.widget.ScrollView;<br />import android.widget.TextView;</p><p>public class phoneRead2 extends Activity {</p><p>@Override<br />public void onCreate(Bundle savedInstanceState) {<br />super.onCreate(savedInstanceState);</p><p>TextView tv = new TextView(this);<br />tv.setText(getSmsInPhone());</p><p>ScrollView sv = new ScrollView(this);<br />sv.addView(tv);</p><p>setContentView(sv);<br />}</p><p>public String getSmsInPhone() {<br />final String SMS_URI_ALL = "content://sms/";<br />final String SMS_URI_INBOX = "content://sms/inbox";<br />final String SMS_URI_SEND = "content://sms/sent";<br />final String SMS_URI_DRAFT = "content://sms/draft";<br />final String SMS_URI_OUTBOX = "content://sms/outbox";<br />final String SMS_URI_FAILED = "content://sms/failed";<br />final String SMS_URI_QUEUED = "content://sms/queued";</p><p>StringBuilder smsBuilder = new StringBuilder();</p><p>try {<br />Uri uri = Uri.parse(SMS_URI_ALL);<br />String[] projection = new String[] { "_id", "address", "person", "body", "date", "type" };<br />Cursor cur = getContentResolver().query(uri, projection, null, null, "date desc");// 擷取手機內部簡訊</p><p>if (cur.moveToFirst()) {<br />int index_Address = cur.getColumnIndex("address");<br />int index_Person = cur.getColumnIndex("person");<br />int index_Body = cur.getColumnIndex("body");<br />int index_Date = cur.getColumnIndex("date");<br />int index_Type = cur.getColumnIndex("type");</p><p>do {<br />String strAddress = cur.getString(index_Address);<br />int intPerson = cur.getInt(index_Person);<br />String strbody = cur.getString(index_Body);<br />long longDate = cur.getLong(index_Date);<br />int intType = cur.getInt(index_Type);</p><p>SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");<br />Date d = new Date(longDate);<br />String strDate = dateFormat.format(d);</p><p>String strType = "";<br />if (intType == 1) {<br />strType = "接收";<br />} else if (intType == 2) {<br />strType = "發送";<br />} else {<br />strType = "null";<br />}</p><p>String strAddress2 = getPeopleNameFromPerson(strAddress);</p><p>smsBuilder.append("[ ");<br />//smsBuilder.append(strAddress + ", ");<br />smsBuilder.append(strAddress + " : " + strAddress2 + ", ");<br />smsBuilder.append(intPerson + ", ");<br />smsBuilder.append(strbody + ", ");<br />smsBuilder.append(strDate + ", ");<br />smsBuilder.append(strType);<br />smsBuilder.append(" ]\n\n");<br />} while (cur.moveToNext());</p><p>if (!cur.isClosed()) {<br />cur.close();<br />cur = null;<br />}<br />} else {<br />smsBuilder.append("no result!");<br />} // end if</p><p>smsBuilder.append("getSmsInPhone has executed!");</p><p>} catch (SQLiteException ex) {<br />Log.d("SQLiteException in getSmsInPhone", ex.getMessage());<br />}</p><p>return smsBuilder.toString();<br />}</p><p>// 通過address手機號關聯Contacts連絡人的顯示名字<br />private String getPeopleNameFromPerson(String address){<br />if(address == null || address == ""){<br />return "( no address )\n";<br />}</p><p>String strPerson = "null";<br />String[] projection = new String[] {Phone.DISPLAY_NAME, Phone.NUMBER};</p><p>Uri uri_Person = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, address);// address 手機號過濾<br />Cursor cursor = getContentResolver().query(uri_Person, projection, null, null, null);</p><p>if(cursor.moveToFirst()){<br />int index_PeopleName = cursor.getColumnIndex(Phone.DISPLAY_NAME);<br />String strPeopleName = cursor.getString(index_PeopleName);<br />strPerson = strPeopleName;<br />}<br />cursor.close();</p><p>return strPerson;<br />}</p><p>}<br />
AndroidManifest.xml 許可權
記得在AndroidManifest.xml中加入android.permission.READ_SMS和android.permission.READ_CONTACTS這兩個permission
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
運行結果:
範例程式碼
參考推薦:
Android 之 Contacts 連絡人讀取