Send callback event implementation based on Android Community app SMS sharing

Source: Internet
Author: User

Profile

Some time ago, because of the project needs, using SHARESDK sharing features, including SMS sharing, and after the call system SMS sharing successfully to interact with the server (I do not care, the other side can receive, only care sent out). But SHARESDK does not support the callback function of SMS sharing, consults with technical customer service, and does not discuss the solution. So I tried to achieve a rough.

Method

After the call system text message sends, through the content observer listens to the text message outbox The change, if hears has the content change SMS ID, then obtains the current content and detects whether it contains some keywords (of course, this keyword is our own definition, for example: "Jarlen"); (I don't care whether the other person can receive it).

Core Code
/** * Created by Jarlen on 2015/6/4. * * Public  class smscontentobserver extends contentobserver {    PrivateContext Mcontext;Private BooleanIsgoing =false;PrivateHandler Mhandler;PrivateString targetAddress =NULL;PrivateString observercontent =NULL;/** * SMS Send Listener Builder * * @param context * @param Handler Listener callback * @param A Ddress Listener's target phone number * @param Content-Listening keyword * /     Public Smscontentobserver(context context, Handler Handler, string address, string content) {Super(handler); This. Mcontext = Context; This. Mhandler = handler;if(Address! =NULL) {//Remove all spaces from the phone number             This. targetAddress = Address.replaceall (" ",""); } This. observercontent = content; } Object obj =NewObject ();@Override     Public void OnChange(BooleanSelfchange) {synchronized(obj) {if(!isgoing) {isgoing =true; cursor cursor = mcontext.getcontentresolver (). Query (Uri.parse ("Content://sms/outbox"),NULL,NULL,NULL,NULL); String address =NULL; String smscontent =NULL;//Traverse query results to get the text message that the user is sending                 while(Cursor.movetonext ()) {StringBuffer SB =NewStringBuffer ();//Get the sending address of the SMSAddress = cursor. getString (Cursor.getcolumnindex ("Address")); smscontent = cursor. getString (Cursor.getcolumnindex ("Body")); }if(Address! =NULL&& smscontent! =NULL) {//Find a text message that is being sentLOG.E ("===","Find a text message that is being sent");if(TargetAddress! =NULL) {//Pre-specified recipients are not empty                        if(Address.contains (targetAddress) && smscontent.contains (observercontent)) {//happens to be a pre-specified recipient, and the information content contains some kind of keywordLOG.E ("===","Information content contains a keyword");                            Message msg = Mhandler.obtainmessage ();                            Msg.obj = address; Msg.what =1;                        Msg.sendtotarget (); }Else{Message msg = Mhandler.obtainmessage (); Msg.what =0;                        Msg.sendtotarget (); }                    }Else{//Recipients not specified in advance                        if(Smscontent.contains (observercontent)) {//Information content contains some kind of keywordLOG.E ("===","Information content contains a keyword");                            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
 /** * monitor */ private  smscontentobserver smscontentobserver; private     boolean  smscontentobserverfind = false ; private  Handler Mhandler = new  Handler () {public  void  handlemessage  (Message msg) {if  (msg.what = = 1  &&!smscontentobserverfind) {.. ......            //related handling  smscontentobserverfind = true ; }        }    };
new SMSContentObserver(this, mHandler, usernumber,"某关键词");getContentResolver().registerContentObserver(Uri.parse("content://sms"true, smsContentObserver);
/***监听解绑*/ifnull) {            getContentResolver().unregisterContentObserver(smsContentObserver);            null;        }

Send callback event implementation based on Android Community app SMS sharing

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.