i tried to take a data from outbox sms.
Here is my code.
Cursor cursor = getContentResolver().query(
Uri.parse("content://sms/sent"), null, null, null, null);
String dateColumn = cursor.getColumnIndex("date");
String bodyColumn = cursor.getColumnIndex("body");
String addressColumn = cursor.getColumnIndex("address");
output += new StringBuilder("\nMessage to: ").append(addressColumn)
.append("\n\n").append(bodyColumn);
Toast.makeText(context, output, Toast.LENGTH_LONG).show();
But when i start the program will be error, and ask to "force close".
Can anyone help me?
You need add this command before get the cursor values:
cursor.moveToNext();
Related
Im building a app that is reading last sms from spec number (vb# 8888). Code is working great. I only have one prob. When i get a new sms from other number (vb# 7777)my code stops reading sms from (v#8888). if i delete the new sms from (7777) than my code strats working again. I'm using this to update a string in my app. Can someone help me
This is my code
Uri mSmsinboxQueryUri = Uri.parse("content://sms/inbox");
Cursor cursor1 = getContentResolver().query(mSmsinboxQueryUri, null, null, null, null);
String[] columns = new String[]{"address", "body"};
if(cursor1.moveToFirst()) {
String address = cursor1.getString(cursor1.getColumnIndex(columns[0]));
if address.equalsIgnoreCase("+597*******")) {
body = cursor1.getString(cursor1.getColumnIndex(columns[3]));
Koers = (TextView) findViewById(R.id.Koersdiedag);
Koers.setText(body);//body
Your code is just reading the most recent message in the inbox, whomever it's from. If you want the most recent message from that particular number, you can adjust your query to match the number, and limit the results to one record.
Uri mSmsinboxQueryUri = Uri.parse("content://sms/inbox");
String[] projection = {"address", "body"};
String phoneNumber = "+597*******";
Cursor cursor1 = getContentResolver().query(mSmsinboxQueryUri,
projection,
"address = ?",
new String[] {phoneNumber},
"date DESC LIMIT 1");
if (cursor1 != null && cursor1.moveToFirst()) {
body = cursor1.getString(cursor1.getColumnIndex("body"));
Koers = (TextView) findViewById(R.id.Koersdiedag);
Koers.setText(body);
}
I was trying to get call logs from android device by using the code from here.
Cursor c = managedQuery(allCalls, null, null, null, null);
String num= c.getString(c.getColumnIndex(CallLog.Calls.NUMBER));// for number
String name= c.getString(c.getColumnIndex(CallLog.Calls.CACHED_NAME));// for name
String duration = c.getString(c.getColumnIndex(CallLog.Calls.DURATION));// for duration
int type = Integer.parseInt(c.getString(c.getColumnIndex(CallLog.Calls.TYPE)));//
and getting an exception as:
android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 500
Can anyone help me on this?
Thanks
Can you try this?
Pass new String[]{"*"} as a projection instead of null.
Cursor c = managedQuery(allCalls, new String[]{"*"}, null, null, null);
I got the answer after trying this code
Cursor c = managedQuery(CallLog.Calls.CONTENT_URI, null,null, null, null);
int durationIndex = c.getColumnIndex(CallLog.Calls.DURATION);
String duration="";
while (c.moveToNext()) {
duration = c.getString(durationIndex);
}
number and name is not required for me, but we can get that too using the same way.
Can someone give me a correct example on how to load all MOBILE numbers saved on the phone into a List, Array or whatever is appropriate? All the examples I have found are either depreciated or do not work. Sorry to ask for a freebie like this but I am getting desparate, I can't find anything!
Here's what I have, it doesn't work. The Log.d doesn't happen.
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, "DISPLAY_NAME = '" + People.NAME + "'", null, null);
if (cursor.moveToFirst()){
Log.d("Number", "Cursor moved");
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Cursor phones = cr.query(People.CONTENT_URI, new String[]{People.NAME, People.NUMBER}, null, null, People.NAME + " ASC");
while (phones.moveToNext()) {
String number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
switch (type) {
case Phone.TYPE_MOBILE:
//Add to the list of numbers
Log.d("Number", number);
break;
}
}
}
Thank you!
Joel,
Cursor phones = cr.query( ContactsContract.CommonDataKind.Phone.NUMBER, .... );
And you need to compare with ContactsContract.CommonDataKind.Phone.TYPE = 2.
Thank you.
I'm trying to use the following code to grab a random mobile phone number from the contacts:
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, "DISPLAY_NAME = '" + "NAME" + "'", null, null);
cursor.moveToFirst();
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Cursor phones = cr.query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + " = " + contactId, null, null);
List numbers = new ArrayList();
while (phones.moveToNext()) {
String number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
switch (type) {
case Phone.TYPE_MOBILE:
numbers.add(number);
break;
}
}
Random randGen = new Random();
return (String) numbers.get(randGen.nextInt(numbers.size()));
However, running this code produces a crash on line 4, with a message saying "CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0". The crash seems to be caused by the cursor.getString() method. Does anyone know where I'm going wrong? This is using the ContactsContract in Android 2.1. Eclipse gives no errors.
Thanks!
The moveToFirst() method returns a boolean. It returns true if it was able to move to the first row and false otherwise, indicating that the query returned an empty set.
When using a cursor, you should follow something like:
if (cursor.moveToFirst()) {
do {
// do some stuff
} while (cursor.moveToNext());
}
cursor.close();
I am trying to get the emails from a contact on Android (2.0.1). I canĀ“t get the email. The code I am using is:
String columns = new String[]{ContactsContract.Data._ID,
ContactsContract.Data.DATA1,
ContactsContract.Data.DATA2,
ContactsContract.Data.DATA3,
ContactsContract.Data.DATA4,
ContactsContract.Data.DATA5,
ContactsContract.Data.DATA6,
ContactsContract.Data.DATA7,
ContactsContract.Data.DATA8,
ContactsContract.Data.DATA9
};
Cursor cursor = contentResolver.query(ContactsContract.Data.CONTENT_URI, columns, null, null, null);
When I try to get the values of the columns, I get null. How can I obtain the emails? Maybe the CONTENT_URI is not correct or tha data is stored in another table and I have to make a join.
I have done this way:
Cursor emailCur = getContentResolver().query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (emailCur.moveToNext()) {
String email = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
String emailType = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
}
emailCur.close();
It is working ok. I hope it can be helpful for another people.
I have done it in this way,
String id = contactData.getLastPathSegment();
Cursor emailsCur = getContentResolver().query(Email.CONTENT_URI, null,
Email.CONTACT_ID + " = " + id, null, null);
while (emailsCur.moveToNext()) {
String email = emailsCur.getString(emailsCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
}
emailsCur.close();
I hope it will help you.
ContactsContract.CommonDataKinds.Email.DATA?
http://developer.android.com/reference/android/provider/ContactsContract.CommonDataKinds.Email.html
I have solved the problem this way:
private static final int SUBACTIVITY_VIEW_CONTACT = 2;
.
.
.
Intent viewContactActivity = new Intent(Intent.ACTION_VIEW, data.getData());
startActivityForResult(viewContactActivity , SUBACTIVITY_VIEW_CONTACT);
.
.
.
Then I can see all the emails of the contact on the screen, just as I would have clicked on the contact itself on the Contacts application, but when I click on any email, it appears an "Unsupported Action. The action is not currently supported" screen. Does anybody knows something about it?
Thanks.