Implementation of callback events for text message sharing and sending Based on the android community app

Source: Internet
Author: User

Implementation of callback events for text message sharing and sending Based on the android community app
Summary

Some time ago, because of project requirements, I used the ShareSDK sharing function, including text message sharing, and had to interact with the server after the system message sharing is called successfully (I don't care about it here, whether or not the recipient can receive the message ). However, ShareSDk does not support the callback function of text message sharing. It consulted technical customer service and did not discuss the solution. So I tried to implement it roughly.

Method

After the system sends a text message, The system monitors the changes in the text message sending frequency through the content observer. If the ID of the text message with the changed content is monitored, obtain the current content and check whether it contains certain keywords (of course, this keyword is defined by ourselves, for example, "jarlen "); if it is found, it indicates that the message has been sent (I don't care about it here, can the recipient receive it ).

Core Code
/*** Created by jarlen on 2015/6/4. */public class SMSContentObserver extends ContentObserver {private Context mContext; private boolean isGoing = false; private Handler mHandler; private String targetAddress = null; private String observerContent = null; /*** short message sending listener constructor ** @ param context * @ param handler listener callback * @ param address: The target mobile phone number of the listener * @ param content: the content keyword */public SMSContentObserver (Con Text context, Handler handler, String address, String content) {super (handler); this. mContext = context; this. mHandler = handler; if (address! = Null) {// remove all spaces in the mobile phone number this.tar getAddress = address. replaceAll ("", "");} this. observerContent = content;} Object obj = new Object (); @ Override public void onChange (boolean selfChange) {synchronized (obj) {if (! IsGoing) {isGoing = true; Cursor cursor = mContext. getContentResolver (). query (Uri. parse ("content: // sms/outbox"), null, null); String address = null; String smsContent = null; // retrieve the text message while (cursor. moveToNext () {StringBuffer sb = new StringBuffer (); // obtain the SMS sending address = cursor. getString (cursor. getColumnIndex ("address"); smsContent = cursor. getString (cursor. getColumn Index ("body");} if (address! = Null & smsContent! = Null) {// locate a text message Log. e ("=", "locate a text message being sent"); if (targetAddress! = Null) {// The specified recipient is not empty if (address. contains (targetAddress) & smsContent. contains (observerContent) {// exactly the specified recipient, and the information content contains a keyword Log. e ("=", "information content contains certain keywords"); Message msg = mHandler. obtainMessage (); msg. obj = address; msg. what = 1; msg. sendToTarget ();} else {Message msg = mHandler. obtainMessage (); msg. what = 0; msg. sendToTarget () ;}} else {// No recipient specified in advance if (smsContent. contains (observerContent) {// the information content contains a keyword Log. e ("=", "information content contains certain keywords"); Message msg = mHandler. obtainMessage (); msg. obj = address; msg. what = 1; msg. sendToTarget ();} else {Message msg = mHandler. obtainMessage (); msg. what = 0; msg. sendToTarget ();}}}}}}}
Create a listener
/*** Listener */private SMSContentObserver smsContentObserver; private boolean smsContentObserverFind = false; private Handler mHandler = new Handler () {public void handleMessage (Message msg) {if (msg. what = 1 &&! SmsContentObserverFind) {...... // handle smsContentObserverFind = true ;}}};
SmsContentObserver = new SMSContentObserver (this, mHandler, usernumber, "a keyword"); getContentResolver (). registerContentObserver (Uri. parse ("content: // sms"), true, smsContentObserver );
/*** Unbind a listener */if (smsContentObserver! = Null) {getContentResolver (). unregisterContentObserver (smsContentObserver); smsContentObserver = null ;}

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.