Android SMS Monitor (iii)--using loader to realize SMS monitoring

Source: Internet
Author: User

Mainactivity 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: * Tap SMS with loader * * Reference: * 1 http://blog.csdn.net/niu_gao/article/details/724411 7 * 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 only introduced after 3.0 * 2 if you use loader before 3.0 then activity must inherit fragmentactivity     * 3 encapsulated inside Loader Contentoberver * */public class Mainactivity extends Activity implements loadercallbacks<cursor>{    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<cursor> oncreateloader (int arg0, bundle bundle) {string[] projection = new string[] {"_id", " Address "," body "," type "}; Loader<cursor> cursorloader=new Android.content.CursorLoader (mcontext, mUri, projection, NULL, NULL, "Date desc" ); return cursorloader;}        @Overridepublic void onloadfinished (loader<cursor> 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 ("------> Receive New SMS:" + "from =" +address+ ", content =" +body+ ", id=" +id+ ", category =" +type "; Break;}} @Overridepublic void Onloaderreset (loader<cursor> Loader) {}}

Main.xml as follows:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    >    <textview        android:layout_width= "wrap_content"        android:layout_height= "Wrap_content"        android:text= "Tapping SMS with loader"        android:layout_centerinparent= "true"/></relativelayout>

Androidmanifest.xml as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" CC.C "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android:min sdkversion= "android:targetsdkversion="/> <uses-permission android:name= "ANDROID.PERMISSION.R" Ead_sms "/> <application android:allowbackup=" true "android:icon=" @drawable/ic_launcher "and Roid:label= "@string/app_name" android:theme= "@style/apptheme" > <activity android:name= "CC. C.mainactivity "android:label=" @string/app_name "> <intent-filter> <actio n android:name= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.category.LAUNCH ER "/> </intent-filter> </activity> </application></manifest>


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.