Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You would need to set specific permissions (read below for the link) but here is example of the code to use a <code>Cursor</code> to retrieve the first SMS message.</p> <pre><code>Uri uri = Uri.parse("content://sms/inbox"); // returns all the results from the given Context Cursor c = context.getContentResolver().query(uri, null, null ,null,null); String body = null; String number = null; if(c.moveToFirst()) { // move cursor to first row // retrieves the body and number of the SMS body = c.getString(c.getColumnIndexOrThrow("body")).toString(); number = c.getString(c.getColumnIndexOrThrow("address")).toString(); } // when your done, close the cursor. c.close(); </code></pre> <p>I would recommend looking over <a href="http://mobdev.olin.edu/mobdevwiki/FrontPage/Tutorials/SMS%20Messaging" rel="noreferrer"><strong>FrontPage/Tutorials/SMS Messaging - Mobdev Wiki</strong></a> it gives a good introductory on dealing with SMS handling on Android.</p> <h3>EDIT:</h3> <p>Those methods were not visible to your application because it was not extending to the <code>Activity</code> superclass. By default, when you develop an <em>application</em>, it inherits methods from that relationship. But you are not creating an application, per se, you are developing a widget.</p> <p>Luckily, within the <code>onUpdate</code> method they pass in the current <code>Context</code> which is a super class for <code>Activity</code> so we can use the variable <code>context</code> to invoke <code>getContentResolver</code> (<em>see above in the code</em>)</p> <p>I also removed <a href="http://developer.android.com/reference/android/app/Activity.html#startManagingCursor%28android.database.Cursor%29" rel="noreferrer"><strong><code>startManagingCursor</code></strong></a> method from the code, it is not completely necessary to have, it allows the Activity to handle the given <code>Cursor</code>'s lifecycle based on the Activity's lifecycle.</p> <p>Let me know if theres any problems.</p> <h3>EDIT 2:</h3> <p>Inside your <code>AndroidManifest.xml</code> file you need to set the correct permissions to avoid any exceptions, add this line.</p> <pre><code>&lt;uses-permission android:name="android.permission.READ_SMS"&gt;&lt;/uses-permission&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload