Android text message listener (3)-using Loader to implement text message listener

Source: Internet
Author: User

MainActivity is as follows:

Package cc. c; import android.net. uri; import android. OS. bundle; import android. app. activity; import android. app. loaderManager. loaderCallbacks; import android. content. context; import android. content. loader; import android. database. cursor;/*** Demo Description: * using Loader to eavesdrop SMS ** reference: * 1 http://blog.csdn.net/niu_gao/article/details/7244117 * 2 http://www.cnblogs.com/jisheng/archive/2013/01/09/2852553.html * 3 http://blog.csdn.net/linmiansheng/article/details/25836937 * Thank you very much ** note: * 1 Loader is introduced only after 3.0.*2 if Loader is used before 3.0, the Activity must inherit FragmentActivity * 3. ContentOberver is encapsulated in Loader. **/public class MainActivity extends Activity implements LoaderCallbacks
 
  
{Private final int LOADER_ID = 9527; private Context mContext; private Uri mUri = null; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); init ();} private void init () {mContext = this; mUri = Uri. parse ("content: // sms/inbox"); this. getLoaderManager (). initLoader (LOADER_ID, null, this) ;}@ Overridepublic Loader
  
   
OnCreateLoader (int arg0, Bundle bundle) {String [] projection = new String [] {"_ id", "address", "body", "type"}; Loader
   
    
CursorLoader = new android. content. CursorLoader (mContext, mUri, projection, null, null, "date desc"); return cursorLoader ;}@ Overridepublic void onLoadFinished (Loader
    
     
Loader, Cursor cursor) {while (cursor. moveToNext () {String address = cursor. getString (cursor. getColumnIndex ("address"); String body = cursor. getString (cursor. getColumnIndex ("body"); int id = cursor. getInt (cursor. getColumnIndex ("_ id"); String type = cursor. getString (cursor. getColumnIndex ("type"); System. out. println ("------> received a new text message:" + "from =" + address + ", content =" + body + ", id =" + id + ", category = "+ type); break; }}@ Overridepublic void onLoaderReset (Loader
     
      
Loader ){}}
     
    
   
  
 

Main. xml is as follows:

     
  
 

AndroidManifest. xml is as follows:
 
     
            
                          
                                  
               
              
 


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.