android ContentResolver获取手机电话号码和短信内容
程序员文章站
2022-05-17 22:36:24
通过contentresolver 获取到手机的联系人跟手机短信信息并显示出来,供大家参考,具体内容如下
1、实现的代码:
package com.exampl...
通过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如何获取联系人所有信息