Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 599397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:29:39+00:00 2026-05-13T16:29:39+00:00

I am desperate to find the solution so I ask for help! I am

  • 0

I am desperate to find the solution so I ask for help!
I am a new french programmer. My objective is to create a widget able to show SMS.
My problem is that I don’t know how create a cursor which select the first SMS in content://sms/inbox
Excuse my bad English, I hope you will able to understand my wich.
Thank you for your answer.
this is my code:

package sfeir.monwidget;
import android.R.string;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.net.Uri;
import android.widget.RemoteViews;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.widget.ArrayAdapter;   


public class MonWidget extends AppWidgetProvider {

 public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
     Uri uri = Uri.parse("content://sms/inbox");
    // returns all the results.
    Cursor c= getContentResolver().query(uri, null, null ,null,null); 
    // called by the Activity.
    startManagingCursor(c);
    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(); 
 RemoteViews updateViews = new RemoteViews(context.getPackageName(),
         R.layout.widget_layout);


 updateViews.setTextColor(R.id.text, 0xFF000000);
 updateViews.setTextViewText(R.id.text, (CharSequence) body);

 ComponentName thisWidget = new ComponentName(context, MonWidget.class);
 appWidgetManager.updateAppWidget(thisWidget, updateViews);
 }

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:29:40+00:00Added an answer on May 13, 2026 at 4:29 pm

    You would need to set specific permissions (read below for the link) but here is example of the code to use a Cursor to retrieve the first SMS message.

    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(); 
    

    I would recommend looking over FrontPage/Tutorials/SMS Messaging – Mobdev Wiki it gives a good introductory on dealing with SMS handling on Android.

    EDIT:

    Those methods were not visible to your application because it was not extending to the Activity superclass. By default, when you develop an application, it inherits methods from that relationship. But you are not creating an application, per se, you are developing a widget.

    Luckily, within the onUpdate method they pass in the current Context which is a super class for Activity so we can use the variable context to invoke getContentResolver (see above in the code)

    I also removed startManagingCursor method from the code, it is not completely necessary to have, it allows the Activity to handle the given Cursor‘s lifecycle based on the Activity’s lifecycle.

    Let me know if theres any problems.

    EDIT 2:

    Inside your AndroidManifest.xml file you need to set the correct permissions to avoid any exceptions, add this line.

    <uses-permission android:name="android.permission.READ_SMS"></uses-permission>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Desperate, please help. Will work for food :) I want to be able to
I have spent three days trying to find the solution to this problem to
I desperately need to find a solution for the following problem: namespace test {
I have a tough problem where I'm desperately trying to find a working solution.
So far I haven't been able to find any solution that would allow me
I read in an ebook somewhere (which I'm desperate to find again), that, by
Hello i am in desperate need of help on this i have an image
I am in desperate need of help, I need to manage an application dependency
I'm in desperate need of help. I've been working on this for over 20
I'm desperate to find any DFS which supports Windows. The only such DFS is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.