Android Address Book Management three short-term acquisition and delivery of short-interest

Source: Internet
Author: User
Tags sendmsg

The first two blogs and the knowledge to get contacts and call records, this article mainly introduces the knowledge of SMS, SMS in communication management should be said to be a difficult point, because SMS involves short-interest sessions and SMS details of two parts, and SMS data volume is larger, You can use the Asyncqueryhandler framework to query, while using CursorAdapter to bind the data



Text messages can be used to get the contact's avatar and name. This is implemented in the Code tool class, displays the name if the contact exists, otherwise displays the number, or displays the avatar if the contact avatar is present, otherwise displays the default avatar, as shown in the slice. Both of these functions can be implemented in contacts and call logs, and interested children's shoes can be modified.

Query Framework

Package Cn.zxw.contact.utils;import Android.content.asyncqueryhandler;import Android.content.ContentResolver; Import Android.database.cursor;import android.support.v4.widget.cursoradapter;import android.util.Log;/** * Queryhandler Frame * @author Zhan * */public class Queryhandler extends Asyncqueryhandler {private static final String TAG = " Queryhandler ";p ublic queryhandler (contentresolver cr) {super (CR);} @Overrideprotected void Onquerycomplete (int token, Object cookie, cursor cursor) {super.onquerycomplete (token, Cookie, cursor); String names[] = Cursor.getcolumnnames (); for (String name:names) {log.i (TAG, name);}  Query complete//if (cookie! = null && cookie instanceof CursorAdapter) {//Set the cursor result of the query to Adaptercursoradapter adapter = (CursorAdapter) cookie;adapter.changecursor (cursor);}}}
SMS Session Acquisition

Package Cn.zxw.contact;import Cn.zxw.contact.utils.contactsutils;import Cn.zxw.contact.utils.queryhandler;import Cn.zxw.contact.utils.timeutils;import Android.app.activity;import Android.content.context;import Android.content.intent;import Android.database.cursor;import Android.graphics.bitmap;import Android.net.Uri; Import Android.os.bundle;import Android.support.v4.widget.cursoradapter;import Android.text.textutils;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.adapterview;import Android.widget.adapterview.onitemclicklistener;import Android.widget.ImageView Import android.widget.listview;import android.widget.textview;/** * SMS list * Click on the entry in the short note list to enter the SMS Details page for the entry * @author Zhan * */p Ublic class Msgactivity extends Activity implements Onitemclicklistener {private ListView lv;public Mycursoradapter adapt er;//Query result set private string[] projection = new string[] {"Snippet", "sms.thread_id as _id", "Msg_count", "Sms.address as Ad Dress ","Sms.date as Date"};p rivate final static int sinppet_column_index = 0;private final static int thread_id_column_index = 1 ;p rivate final static int msg_count_column_index = 2;private final static int address_column_index = 3;private final Stati c int date_column_index = 4; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_contacts_msg_calllog); LV = (ListView) Findviewbyid (R.id.lv); Adapter=new Mycursoradapter (this, null);//Initialize Adapterlv.setadapter (adapter); Startquery (); Lv.setonitemclicklistener (this);} /** * Execute query */private void Startquery () {Queryhandler Mqueryhandler = new Queryhandler (Getcontentresolver ());//Execute Query/** * t Oken Unique identifier cookie can be used to pass data above the parameters are passed to a method onquerycomplete */uri Uri = Uri.parse ("content://sms/conversations");// Mqueryhandler.startquery (0, NULL, URI, projection, null,//null, "date desc"), Mqueryhandler.startquery (0, adapter, URI, projection, NULL, NULL, "date desc");} Custom Cursoradapterprivate CLASS Mycursoradapter extends CursorAdapter {private Layoutinflater minflater;public Mycursoradapter (context context, Cursor c) {Super (context, c); minflater = Layoutinflater.from (context);} Create an entry @overridepublic View Newview (context context, cursor cursor, viewgroup parent) {View view = Minflater.inflate (r.lay Out.activity_msg_list_item,null); return view;} Binding entry @overridepublic void BindView (view view, context context, cursor cursor) {//1. Get entry//2. Get Data//3. Bind Data ImageView Iv_h Eader = (ImageView) View.findviewbyid (R.id.iv_header); TextView Tv_number = (TextView) View.findviewbyid (R.id.tv_number); TextView tv_body = (TextView) View.findviewbyid (r.id.tv_body); TextView tv_date = (TextView) View.findviewbyid (r.id.tv_date);//Because the data display of the ListView is controlled by an adapter, So to the BindView method to control the display of the checkbox//get data int id = cursor.getint (thread_id_column_index); String address = cursor.getstring (address_column_index); int msg_count = Cursor.getint (msg_count_column_index); long Date = Cursor.getlong (Date_column_index); String body= Cursor.getstring (Sinppet_column_index); SYSTEM.OUT.PRINTLN (address);//bind data string displayName = Contactsutils.getcontactnamebyaddress ( Getapplicationcontext (), address), if (Textutils.isempty (displayName)) {//unknown contact Iv_header.setimageresource ( R.drawable.ic_launcher); Tv_number.settext (address + "(" + Msg_count + ")"); else {//known contact Tv_number.settext (DisplayName + "(" + Msg_count + ")"); Bitmap Bitmap = contactsutils.getcontactphotobyaddress (Getapplicationcontext (), address), if (Bitmap = = null) {IV_ Header.setimageresource (R.drawable.ic_launcher);} else {iv_header.setimagebitmap (bitmap);}} Tv_body.settext (body); String datastr = Timeutils.formatdate (Getapplicationcontext (), date); Tv_date.settext (DATASTR);}} @Overridepublic void Onitemclick (adapterview<?> parent, view view, int Position,long ID) {//pass data cursor cursor = (Cur SOR) Adapter.getitem (position); int _id = Cursor.getint (Thread_id_column_index); String address=cursor.getstring (Address_column_index); String Displayname=contactsutils.getContactnamebyaddress (Getapplicationcontext (), address);//Enter the list details page Intent intent=new Intent (this, Msgdetailactivity.class), Intent.putextra ("_id", _id), Intent.putextra ("address", address), Intent.putextra (" DisplayName ", displayName); startactivity (intent);}}
SMS Details page, can be implemented to send short interest

Package Cn.zxw.contact;import Cn.zxw.contact.utils.constants;import Cn.zxw.contact.utils.contactsmsgutils;import Cn.zxw.contact.utils.queryhandler;import Cn.zxw.contact.utils.timeutils;import Android.app.Activity;import Android.content.context;import Android.content.intent;import Android.database.cursor;import Android.os.Bundle; Import Android.support.v4.widget.cursoradapter;import Android.text.textutils;import Android.view.View;import Android.view.view.onclicklistener;import Android.view.viewgroup;import Android.widget.button;import Android.widget.edittext;import Android.widget.listview;import Android.widget.textview;import Android.widget.Toast ;p ublic class Msgdetailactivity extends Activity implements Onclicklistener {private Button bt_back;private TextView tv_n Umber;private ListView lv;private EditText et_msg_content;private Button bt_send;private int _id;private String address; Private Mycursoradapter adapter;private string[] projection=new string[]{"_id", "type", "Body", "date"};p Rivate final static int id_column_index=0;private final static int type_column_index=1;private final static int body_column_ind Ex=2;private final static int date_column_index=3; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_msg_list_detail); InitView (); StartQuery ();} public void Initview () {bt_back = (Button) Findviewbyid (r.id.bt_back); tv_number = (TextView) Findviewbyid (r.id.tv_ number); LV = (ListView) Findviewbyid (r.id.lv); et_msg_content = (EditText) Findviewbyid (r.id.et_msg_content); bt_send = (Button) Findviewbyid (r.id.bt_send); Bt_back.setonclicklistener (this); Bt_send.setonclicklistener (this); Intent Intent=getintent (); _id = Intent.getintextra ("_id", 0); address = Intent.getstringextra ("Address"); String Displayname=intent.getstringextra ("DisplayName"), if (Textutils.isempty (DisplayName)) {Tv_number.settext ( address);} Else{tv_number.settext (displayName);} adapter = new Mycursoradapter (this, null); Lv.setadapter (adaptER);} public void Startquery () {//Query Queryhandler mhandler=new queryhandler (Getcontentresolver ());//query specifies all SMS messages in the session ID string Selection= "thread_id=?"; String[] selectionargs=new string[]{_id+ ""};mhandler.startquery (0, adapter, Constants.sms_uri, projection, selection , Selectionargs, "date ASC");} Private class Mycursoradapter extends Cursoradapter{public mycursoradapter (context context, Cursor c) {Super (context, C) ;} @Overridepublic View Newview (context context, cursor cursor, viewgroup viewgroup) {view View=getlayoutinflater (). Inflate (R.layout.activity_msg_detial_item, null); return view;} @Overridepublic void BindView (view view, context context, cursor cursor) {TextView tv_date= (TextView) View.findviewbyid ( R.id.tv_date); TextView tv_send= (TextView) View.findviewbyid (r.id.tv_send); TextView tv_receive= (TextView) View.findviewbyid (r.id.tv_receive); long Date=cursor.getlong (Date_column_index); String body=cursor.getstring (body_column_index); int type=cursor.getint (TYPE_COLUMN_INDEX); String Str=timeutilS.formatdate (Getapplicationcontext (), date), Tv_date.settext (str), if (type==constants.receive_type) {//1 means receive tv_ Date.setvisibility (view.visible); Tv_receive.settext (body); tv_send.setvisibility (View.gone);} else if (type==constants.send_type) {//2 = Send tv_date.setvisibility (view.visible); Tv_send.setvisibility ( view.visible); Tv_send.settext (body); tv_receive.setvisibility (View.gone);}} /** * This method must be called by the system */@Overridepublic void notifydatasetchanged () {super.notifydatasetchanged ();// Let the ListView scroll to the Last Face Lv.setselection (Adapter.getcount ()-1);}} @Overridepublic void OnClick (View v) {switch (V.getid ()) {case r.id.bt_back:finish ();//close current Activitybreak;case R.id.bt_ send://Gets the short content string Content=et_msg_content.gettext (). toString (). Trim (); if (Textutils.isempty (content)) {// Send content is empty toast.maketext (Getapplicationcontext (), "SMS is Empty", 0). Show (); Else{contactsmsgutils.sendmsg (Getapplicationcontext (), address, content);//emptying Et_msg_content.settext ("");// Automatically scrolls to the last}break;default:break after the short message is sent successfully;}}
How to send SMS:

/** * Send SMS * @param address * @param content */public static void sendmsg (Context context,string address,string content) {S Msmanager Smsmanager=smsmanager.getdefault ();//If the text message is too long, split the SMS arraylist<string> parts=smsmanager.dividemessage (content); Smsmanager.sendmultiparttextmessage (address, NULL, parts, NULL, NULL);//Manually store SMS Contentresolver resolver= Context.getcontentresolver (); Uri Uri=constants.sms_uri; Contentvalues values=new contentvalues () values.put ("Address", address), Values.put ("Body", content), Values.put (" Type ", Constants.send_type); Values.put (" Date ", System.currenttimemillis ()); Resolver.insert (URI, values);}
Refer to http://blog.csdn.net/yuzhiboyi/article/details/8093408 and/HTTP for Asyncqueryhandler and CursorAdapter blog.csdn.net/yuzhiboyi/article/details/7654840 to learn




Android Address Book Management three short-term acquisition and delivery of short-interest

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.