Reputation: 87
I am using 2.1 platform.Trying to retrieve all the SMS from the Emulator. No Compile error, NO Logcat Error in my code.I included the permission for ReadSMS in manifest file.When I run the code no display in the ListView. In my XML file there is only one ListView. On that ListView I am trying to display all the SMS of format ( Number : Sms Body) from the Emulator.
Code
public class SMSActivity extends Activity {
/** Called when the activity is first created. */
ListView lview;
String Body = "" ;
int Number;
ArrayList<String> smslist=new ArrayList<String>();
ArrayAdapter<String> itemAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lview =(ListView)findViewById(R.id.lv);
itemAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,smslist);
lview.setAdapter(itemAdapter);
ContentResolver cr = getContentResolver();
Cursor c = cr.query(Uri.parse("content://sms/"), new String[] { "_id", "address", "date", "body","read" },"_id = "+null, null, null);
while(c.moveToNext()){
Number = c.getInt(c.getColumnIndexOrThrow("address"));
Body = c.getString(c.getColumnIndexOrThrow("body")).toString();
smslist.add( Number + "\n" + Body);
}
itemAdapter.notifyDataSetChanged();
c.close();
}
}
How to solve this problem ?
Upvotes: 0
Views: 217
Reputation: 28093
The error you are doing is you just mentioning that you want to read sms but you forgot to say from which folder you need them.e.g from inbox,outbox etc..
If you need to read SMS from inbox use following.
Cursor cursor = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);
You can also query column names using
for (int i = 0; i < cursor.getColumnCount(); i++) {
Log.i("info", cursor.getColumnName(i));
}
Upvotes: 1