Various problems of text message monitoring in Android, enabling the function of listening for text messages

Source: Internet
Author: User

To write a text message listener, you can easily register a broadcast receiver to receive the broadcast when the system receives the text message. However, this broadcast is an ordered broadcast, that is, when other programs first get the broadcast and pass it to you, of course, it can also get rid of the broadcast, so that you can not receive it, in this way, your program will certainly not be able to receive the broadcast. There are a lot of such examples on the Internet, what is the priority of increasing the broadcast receiver is 1000 and 10000 ..

Let's take a look at the basic introduction of this broadcast:

The Android: Priority value in intent-filter is set to 1000, but in real time, if you set 1000, broadcast is not accepted, unless You unmount other text messages on your mobile phone that have accepted the broadcast, such as 360 go text messages and Feixin Shenma messages. Their priority is always higher than yours. You can continue with Baidu, some people will set it to 10000 9999, and so on, you will find that when you set it to 10000, You can occasionally receive it, only occasionally. That's swollen.

The following method is used not to receive System Broadcasts, but to listen to changes in the SMS database. When the SMS database changes, you can obtain the latest text message. Or you can do anything else.

Listening Class smsreceiver. Java

Package COM. pei. broadcastreceiver; import Java. util. list; import COM. pei. fixed. allfinalinfo; import smsinfo; import COM. pei. util. smscontent; import android. app. activity; import android. database. contentobserver; import uri; import android. OS. handler;/*** Class Name: smsreceiver <br> * class description: Database Change listener class <br> * PS: When data changes, change method only in execution <br> * Date: 2012-3-16 <br> ** @ Version 1.00 * @ author codyy) peijiangping */public class smsreceiver extends contentobserver {/*** activity object */private activity; private list <smsinfo> Infos; Public smsreceiver (handler, activity) {super (handler); this. activity = activity;} @ overridepublic void onchange (Boolean selfchange) {URI uri = Uri. parse (allfinalinfo. sms_uri_inbox); // set a URI to view the smscontent = new smscontent (activity, Uri); Infos = smscontent. getsmsinfo (); system. out. println (Infos. get (1 ). getsmsbody (); super. onchange (selfchange );}}

How can I register this listener:

ContentObserver co = new SmsReceiver(new Handler(),AndroidUtilActivity.this);this.getContentResolver().registerContentObserver(Uri.parse("content://sms/"), true, co);

For smscontent. Java content, refer to the previous blog. For more information about allfinalinfo. sms_uri_inbox, see the previous blog.

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: 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.