您好,登錄后才能下訂單哦!
小編給大家分享一下android ContentResolver如何獲取手機電話號碼和短信內容,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
通過ContentResolver 獲取到手機的聯系人跟手機短信信息并顯示出來,具體內容如下
1、實現的代碼:
package com.example.phone; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import android.net.Uri; import android.os.Bundle; import android.provider.ContactsContract; import android.provider.ContactsContract.PhoneLookup; import android.app.Activity; import android.content.ContentResolver; import android.database.Cursor; import android.database.sqlite.SQLiteException; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener{ private TextView text; private Button button; private Button sms; HashMap<String, Object> map = new HashMap<String, Object>(); List<String> con = new ArrayList<String>(); String msg;//聯系人姓名+號碼 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initUI(); } private void initUI() { // TODO Auto-generated method stub text = (TextView)findViewById(R.id.text); button = (Button)findViewById(R.id.button);//獲取手機電話號碼 button.setOnClickListener(this); sms = (Button)findViewById(R.id.sms);//獲取手機短信信息 sms.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()){ case R.id.button: getPhoneNumber();//獲取手機電話號碼 break; case R.id.sms://獲取手機短信內容 getSmsMessage(); Map<String,Object> item = new HashMap<String,Object>(); for(int i= 0;i<con.size();i++){ text.setText(item.put("con", con.get(i))+"");//僅顯示一條 System.out.println("ningshengcai:"+item.put("con", con.get(i)));//打印顯示全部數據 } break; default : break; } } /** * 獲取手機聯系人號碼 */ public void getPhoneNumber(){ // smslist=getListView(); //得到ContentResolver對象 ContentResolver cr = getContentResolver(); //取得電話本中開始一項的光標 Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); while (cursor.moveToNext()) { // 取得聯系人名字 int nameFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME); String name = cursor.getString(nameFieldColumnIndex); //name += (name); // 取得聯系人ID String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = "+ contactId, null, null); // 取得電話號碼(可能存在多個號碼) while (phone.moveToNext()) { String strPhoneNumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); System.out.println(name+":"+strPhoneNumber); msg += name+":"+strPhoneNumber+"\n"; text.setText(msg); } phone.close(); } cursor.close(); } /** * 獲取短信信息 * @return smsBuilder.toString() */ @SuppressWarnings("unused") public String getSmsMessage(){ final String SMS_URI_ALL = "content://sms/"; final String SMS_URI_INBOX = "content://sms/inbox"; final String SMS_URI_SEND = "content://sms/sent"; final String SMS_URI_DRAFT = "content://sms/draft"; StringBuilder smsBuilder = new StringBuilder(); try{ ContentResolver cr = getContentResolver(); String[] projection = new String[]{"_id", "address", "person", "body", "date", "type"}; Uri uri = Uri.parse(SMS_URI_ALL); Cursor cur = cr.query(uri, projection, null, null, "date desc"); if (cur.moveToFirst()) { String name; String phoneNumber; String smsbody; String date; String type; int nameColumn = cur.getColumnIndex("person"); //發送人 int phoneNumberColumn = cur.getColumnIndex("address"); //號碼 int smsbodyColumn = cur.getColumnIndex("body"); //內容 int dateColumn = cur.getColumnIndex("date"); //時間 int typeColumn = cur.getColumnIndex("type"); //接收還是發送 do{ name = cur.getString(nameColumn); phoneNumber = cur.getString(phoneNumberColumn); smsbody = cur.getString(smsbodyColumn); SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss"); Date d = new Date(Long.parseLong(cur.getString(dateColumn))); date = dateFormat.format(d); int typeId = cur.getInt(typeColumn); if(typeId == 1){ type = "接收"; } else if(typeId == 2){ type = "發送"; } else { type = ""; } //System.out.println("nsc :"+name+":"+phoneNumber+":"+smsbody+":"+date+":"+type +"\n"); String smsmsg = name+":"+phoneNumber+":"+smsbody+":"+date+":"+type+"\n"; con.add(smsmsg); if(smsbody == null) smsbody = ""; }while(cur.moveToNext()); } else { smsBuilder.append("no result!"); } smsBuilder.append("getSmsInPhone has executed!"); } catch(SQLiteException ex) { Log.d("SQLiteException in getSmsInPhone", ex.getMessage()); } return smsBuilder.toString(); } }
2、代碼布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <Button android:id="@+id/button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/ph"/> <Button android:id="@+id/sms" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/sms"/> <ScrollView android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </ScrollView> </LinearLayout>
3、需要用到的權限
<uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.WRITE_SMS" /> <uses-permission android:name="android.permission.READ_SMS" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" />
Android是一種基于Linux內核的自由及開放源代碼的操作系統,主要使用于移動設備,如智能手機和平板電腦,由美國Google公司和開放手機聯盟領導及開發。
看完了這篇文章,相信你對“android ContentResolver如何獲取手機電話號碼和短信內容”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。