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: