views:

144

answers:

1

Hello, when I send an SMS on my Android emulator, it goes to the content provider:

content://sms/sent

right?

So I wanted to get the last sent SMS from the content provider. So I used this Uri as you can see above, and, I used the method query, with the Content Resolver Object. And I got the cursor, and used the movetofirst() method, so I would have the last sent SMS. Check the code below. I hope it helps.

package com.sys;

import android.app.Activity;  
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.net.Uri;
import android.database.Cursor;

    public class SMS extends Activity {

Button btnVerSms;
EditText txtFinal;

final Uri CONTENT_URI = Uri.parse("content://sms/sent");    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    btnVerSms= (Button)findViewById(R.id.btnVerSms);
    txtFinal =   (EditText)findViewById(R.id.txtFinal);   

    btnVerSms.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Cursor cursor = getContentResolver().query(CONTENT_URI, null, null, null,  null);                                   
        String body = null;    

        if(cursor.moveToFirst()){
            body = cursor.getString(cursor.getColumnIndexOrThrow("body")).toString();               
        }
        txtFinal.setText(body);
    }
});
}
}
+3  A: 

Hello, when I send an SMS on my Android emulator, it goes to the content provider: content://sms/sent right?

Not necessarily. You assume that content provider exists on all devices and is used by all SMS client applications. Those are not valid assumptions.

So I wanted to get the last sent SMS from the content provider.

That content provider is not part of the Android SDK.

CommonsWare
So what content provider that exists in all devices and is used by all SMS client app you suggest me to use, since this one I mentioned is not part of the framework. What URI should I pass to the query() method?
psyhclo
@psyhclo: There is no "content provider that exists in all devices and is used by all SMS client app", any more than every email application ever written for the desktop uses the Outlook API. Each email application, if it has an API, has its own API. Similarly, each SMS client application, if it has an API, has its own API.
CommonsWare
So, what am I suposed to do? If I let this the way it is, my app will have serious problems. right? What should I do?
psyhclo
@spyhclo: Eliminate the feature that depends on your being able to read the SMS outbox.
CommonsWare
@CommonsWare: What would be this feature? I dont get it when you say when it depends of me being able to read the SMS outbox. You want to say that I should be able to read everything, the outgoing and the incoming SMS?
psyhclo
@pyshclo: You wrote "So I wanted to get the last sent SMS from the content provider." Simply don't do that.
CommonsWare
@CommonsWare: HAHAHHHH. Puzzles!!! So I should get the incoming and outgoing SMS? Is it? Because when you say for me dont do that, dont get the last sent SMS, I dont understand, because, its not important for me get the incoming SMS. If you could be more specific, in what I should do. Give an example as a code,plz. I changed the URI from conten://sms/sent to content://sms. Is this it? Thank you so much!!!
psyhclo
@CommonsWare: And what if I make a query() method, not this that I did here, just passing the CONTENT_URI. But a method with the fields I want to retrieve, etc.
psyhclo