Implementation of sending callback events based on the app SMS sharing in Android Society

Source: Internet
Author: User

Summary

Some time ago. Due to the needs of the project, SHARESDK is used to share this function. It contains the SMS unit. And the calling system to share the message to interact with the successful processing server after (I don't care here, received.) Just care to send it out). However, SHARESDK does not support the text message sharing callback function, consulting the technical customer service, also did not discuss the way to solve. 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, assumes the monitor hears has the content change the message ID, then obtains the current content and detects whether contains certain keywords (of course, this keyword is our custom. For example: "Jarlen"); If found then the description has been sent (I don't care if 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 add Ress 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;        }

Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.

Implementation of sending callback events based on the app SMS sharing in Android Society

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.