Android Address Book Management 3 short message acquisition and Sending Short Messages

Source: Internet
Author: User
Tags sendmsg

Android Address Book Management 3 short message acquisition and Sending Short Messages

The first two blogs respectively talked about how to obtain contacts and call records. This topic describes how to get short messages. Short Messages are a difficult point in communication management, because SMS involves two parts: Short Message session and text message details, and the text message data volume is large, you can use AsyncQueryHandler framework to query and use CursorAdapter to bind data.

You can obtain the contact's profile picture and name in the text message. This is implemented in the Code tool class. If a contact exists, the name is displayed. Otherwise, the number is displayed. If a contact profile exists, the Avatar is displayed. Otherwise, the default profile is displayed. These two functions can be implemented in contacts and call records, and can be modified if you are interested.

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 framework * @ author zhan **/public class QueryHandler extends AsyncQueryHandler {private static final String TAG = QueryHandler; public 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 completed // if (cookie! = Null & cookie instanceof CursorAdapter) {// set the queried cursor result to adapterCursorAdapter adapter = (CursorAdapter) cookie; adapter. changeCursor (cursor );}}}
SMS session Retrieval

 

 

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; /** text message list * click the entry in the text message list to go to the text message details page corresponding to the entry * @ author zhan **/public class MsgActivity extends Activity implements OnItemClickListener {private ListView lv; public MyCursorAdapter adapter; // the query result set private String [] projection = new String [] {snippet, sms. thread_id as _ id, msg_count, sms. address as address, sms. date as date}; private final static int SINPPET_COLUMN_INDEX = 0; private final static int THREAD_ID_COLUMN_INDEX = 1; private final static int MSG_COUNT_COLUMN_INDEX = 2; private final static int ADDRESS_COLUMN_INDEX = 3; private final static 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 the query/*** token Unique Identifier cookie can be used to pass data the above parameters will be 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. layout. activity_msg_list_item, null); return view;} // bind the entry @ Overridepublic void bindView (View view, Context context, Cursor cursor) {// 1. get entry // 2. get Data // 3. bind the data ImageView iv_header = (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 Listview is controlled by the adapter, you can control the display of checkbox in the bindView method. // obtain the 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 = (Cursor) 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 Intent intent Intent = new Intent (this, MsgDetailActivity. class); intent. putExtra (_ id, _ id); intent. putExtra (address, address); intent. putExtra (displayName, displayName); startActivity (intent );}}
SMS details page for Sending Short Messages

 

 

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. text Utils; 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; public class MsgDetailActivity extends Activity implements OnClickListener {private Button bt_back; private TextView TV _number; private ListView lv; pr Ivate 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}; private final static int ID_COLUMN_INDEX = 0; private final static int TYPE_COLUMN_INDEX = 1; private final static int BODY_COLUMN_INDEX = 2; private final static int DATE_COLUMN_INDEX = 3; @ Overrideprotected void onCreate (Bund Le 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 voi D startQuery () {// query QueryHandler mHandler = new QueryHandler (getContentResolver (); // query all text messages in the specified 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 indicates receiving TV _date.setVisibility (View. VISIBLE); TV _receive.setText (body); TV _send.setVisibility (View. GONE);} else if (type = Constants. SEND_TYPE) {// 2 indicates to send TV _date.setVisibility (View. VISIBLE); TV _send.setVisibility (View. VISIBLE); TV _send.setText (body); TV _receive.setVisibility (View. GONE) ;}}/*** this method will certainly be called by the system */@ Overridepublic void yydatasetchanged () {super. notifyDataSetChanged (); // scroll the Listview to the last lv. setSelection (adapter. getCount ()-1) ;}}@ Overridepublic void onClick (View v) {switch (v. getId () {case R. id. bt_back: finish (); // close the current activitybreak; case R. id. bt_send: // get the short message content String content = et_msg_content.getText (). toString (). trim (); if (TextUtils. isEmpty (content) {// The sent content is empty Toast. makeText (getApplicationContext (), SMS is empty, 0 ). show ();} else {ContactsMsgUtils. sendMsg (getApplicationContext (), address, content); // clear et_msg_content.setText (); // The message is automatically rolled to the end after the message is sent successfully.} break; default: break ;}}}
SMS sending method:

 

 

/*** Send SMS * @ param address * @ param content */public static void sendMsg (Context context, String address, String content) {SmsManager smsManager = SmsManager. getDefault (); // if the text message is too long, split the text message ArrayList
 
  
Parts = smsManager. divideMessage (content); smsManager. sendMultipartTextMessage (address, null, parts, null, null); // manually store the text message 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 );}
 
 


 

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.