tags:

views:

1117

answers:

2

Hi, I am desesperate 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 no 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);
 }

}

A: 

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>
Anthony Forloney
Thank's for this post! I wrote something quietly similar but now I understand what isn't correct. When I use this part of code I have the same problem as when I write mine, getContentResolver().query and startManagingCursor are not recognize by eclipse. I probably have forgoten to import something, I'll search in your tuto. Thank's a lot for your help!
Olivier
Not a problem, good luck.
Anthony Forloney
In fact I don't understand what is the problem. This one come from those fonctions (getContentResolver().query and startManagingCursor) for which eclipse want to create a method. I already have import the classes concerned so do I forget something?
Olivier
@Oliver, if you hover over where that code lies within Eclipse, when you right-click it will have a `import` option, like `import android.app.Activity` (where *`startManagingCursor`* lies) and `import android.content.ContentResolver` (where *`query`* lies)
Anthony Forloney
I already have import those classes, perhaps is my extend false, it's named AppWidgetProvider. Is it an error to not create an another class for writing this fonction? Or should I create a class wich I import in my widget after?
Olivier
You can extend to your own provider class (*AppWidgetProvider*) that contains retrieving SMS messages and inside of *AppWidgetProvider* you would need to import the necessary classes. I hope I am understanding your problem correctly.
Anthony Forloney
@Olivier, there is a character limit for comments, edit your question above to include your code and I can take a look.
Anthony Forloney
I import the necessary. I don't understand where is the problem
Olivier
I wrote my code, hope you find the problem!
Olivier
I can't find the reason why getContentResolver().query and startManagingCursor or not apply by beclipse...
Olivier
It should work, refresh the project to see if that makes a different. Also try `import android.app.Activity`
Anthony Forloney
I import more classes before, like import android.app.Activity and it make no difference. I tried again and still no difference. it's gonna make me crazy, I search on the web and can't find any solution..
Olivier
Sometimes Eclipse may not recognize things until you hit refresh or `F5`, I hope thats what fixes it.
Anthony Forloney
nothing happend, there seems to be an error. Per hap's may I use something else as a cursor bet it seems complicated...
Olivier
`Cursor` is what you need, indeed. It only works on those two methods?
Anthony Forloney
yes there is such those methode which are warning by eclipse
Olivier
What SDK are you using?
Anthony Forloney
I am using android 2.1 SDK, the last publish by google including all library i need i think. I am going to sleep and think about a solution tomorow. It is my first widget, I will manage to create it! good night or good day wherever you are on earth!
Olivier
Reason why those methods cannot be found is because they are implementing in the `Activity` class and when extending to `Activity` opposed to `AppWidgetManager` the error goes away. But, since you are dealing with a widget, opposed to an actual application there might be some caveats with that. I can look into it more.
Anthony Forloney
Ok problem solved! It was such a problem of permission in the manifest.
Olivier
A: 

Another option is to listen for the SMS_RECEIVED broadcast intent and update your app widget in the broadcast receiver code. There is some information in this question's answers regarding usage.

Roman Nurik
I allready have readen this topic but what I need is the first step of a bigger conception which is to acces with a cursor to sms/inbox. Later, I will develop some button to move the cursor and naturally to permit to see other sms. An easy fast navigation sms in a widget.
Olivier
Ok problem solved! It was such a problem of permission in the manifest.
Olivier