Content observer implements SMS listening and observer SMS listening

Source: Internet
Author: User

Content observer implements SMS listening and observer SMS listening
Content observer implements SMS monitoring

The content observer monitors whether the text message changes. If the text message changes, the content provider reads the text message.

Principle:

When a text message changes (for example, a text message), the text message application notifies the content observer that my text message has changed, and transmits the uri of the text message with the changed location, the content provider can easily read the text message content, but it must have sufficient permissions.

 

Package com. example. duanxinjianting; import android. app. activity; import android. database. contentObserver; import android. database. cursor; import android.net. uri; import android. OS. bundle; import android. OS. handler; import android. util. log; import android. view. menu; import android. view. menuItem; public class MainActivity extends Activity {private MyObserver observer; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); observer = new MyObserver (new Handler (); // register a content observer (observe specified data) Uri uri = Uri. parse ("content: // sms"); getContentResolver (). registerContentObserver (uri, true, observer);}/*** content observer **/private class MyObserver extends ContentObserver {public MyObserver (Handler handler) {super (handler );} /*** when the observed data changes, this method is called back **/@ Override public void onChange (boolean selfChange, Uri uri Uri) {Log. d ("bh", uri. toString (); Cursor cursor = getContentResolver (). query (uri, null, null); while (cursor. moveToNext () {int index = cursor. getColumnIndex ("body"); String body = cursor. getString (index); Log. d ("bh", body) ;}}@ Override protected void onDestroy () {super. onDestroy (); // uninstall the content observer getContentResolver (). unregisterContentObserver (observer );}}

 

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.