SMS numbers are incorrect? - android

I am using this code to retrieve the SMS from Emulator.After that I need to Post it in to the DropBox.I saved some messages to Emulator, it retrieves two things Number and Body.At the time of retrieval it displays the
Wrong Numbers but correct Body. How to correct the number, I save the numbers in to a variable Number. My code is here, I am using 2.1 version.
code
ContentResolver cr = getContentResolver();
Cursor c = getContentResolver().query(Uri.parse("content://sms/inbox"), null, 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();
Any solution ?

In your code you are using
Number = c.getInt(c.getColumnIndexOrThrow("address"));
use c.getString(c.getColumnIndexOrThrow("address")) Method instead of getInt() method
see below link too, The type column in SMS uri
retrieving number.

Treat the address as String instead of a Int.
I think it should resolve the issue.
String str = cursor.getString( c.getColumnIndexOrThrow("address"));
regards,
dattatray.

Related

In what format Cursor of ContentResolver retruns contacts saved on the device?

I am fetching the contacts saved on the device using following code
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
Cursor cur = mParentActivity.getContentResolver().
query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, null, null, sortOrder);
while (cur.moveToNext()) {
String phoneNo = cur.getString(cur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
}
I am using my Huawei phone to test this and getting local contacts like
+61 455 553 970
+972 55-644-4862
My question is, will i get space after the country telephone code for all the devices ?
From the documentation:
NUMBER
public static final String NUMBER
The phone number as the user entered it.
Type: TEXT
So it seems that it will be like it was provided. I doubt that you can assume that it will always have space after country code.
You can check NORMALIZED_NUMBER. It may be better for your use case.
Reference: https://developer.android.com/reference/android/provider/ContactsContract.CommonDataKinds.Phone.html#NUMBER
Edit:
You can use https://github.com/googlei18n/libphonenumber to validate phone number.

simple code, but cursor is still returning null from content provider

Got a problem here with a simple section of code that uses managedQuery cursors. Two parts, the top half of the code puts a string into the LATITUDE column of the MediaStore database content provider.
The second part of the code below that reads that same string back from the database. This is where it is returning a null result. Either because the string was not correctly read into the database in the first part of the code or there is an error in the second part where it reads it back from the database.
I am using the LATITUDE column of the Media.images content provider to store a string. There is no other unused column that is available so that is why I am using it.
The goal is to put the string path name of the mp3 file into the LATITUDE column of an image and read it back out again later with another query.
I tracked down the problem to the following code. The cursor in the second part of code is returning null. Is there something wrong with my use of cursors, or some error in this that I don't know about?
String displayName; // string pathname of the mp3 file to be put into LATITUDE column
String filename; // the pathname of the image that I want to add the database info to
ContentValues imageValues = new ContentValues();
String selection3 = MediaStore.Images.Media.DATA + "='" + filename +"'";
imageValues.put(MediaStore.Images.Media.LATITUDE, displayName);
getContentResolver().update(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
imageValues, selection3, null);
String[] proj6 = { MediaStore.Images.Media.LATITUDE };
String selection6 = MediaStore.Images.Media.DATA + "='" + filename +"'";
Cursor cursor2 = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
proj6, selection6, null, null);
cursor2.moveToFirst();
String displayer = (String)
cursor2.getString(cursor2.getColumnIndex(MediaStore.Images.Media.LATITUDE));
Found out that there was not an error like I thought it would be. It was only a typing error.
put method has "Images.Media.LATITUDE"
update method has "Audio.Media.LATITUDE", changed this to "Images.Media.LATITUDE" like it is in the put method and it works now.
imageValues.put(MediaStore.Images.Media.LATITUDE, displayName);
getContentResolver().update(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
imageValues, selection3, null);

Android: null string issue for 'address' column for sms content provider

I am working on a android application which read the sms from content provider. The application works fine and read sms fine from content provider. But sometime (very rare) the 'address' column returns null for the sms message.
Here is sample code What I am using:
String whereClause = "_id > " + String.valueOf(Database.getLastSmsId(this));
Cursor cursor = getContentResolver().query(smsUri, null, whereClause, null, null);
if(cursor.moveToFirst()) {
do {
int id = cursor.getInt(cursor.getColumnIndex("_id"));
String protocol = cursor.getString(cursor.getColumnIndex("protocol"));
String body = cursor.getString(cursor.getColumnIndex("body"));
String address = cursor.getString(cursor.getColumnIndex("address")); // <----- Here is the problem
// address returns as null string
String date = cursor.getString(cursor.getColumnIndex("date"));
Log.d(Constants.TAG, "SMS event received. address="+address);
} while(cursor.moveToNext());
}
I am getting this issue on Motorola Driod Android v2.3.5. Please advise.
Ali
I found the problem.
The android OS save the message as draft in SMS content provider, if start writing and then close the SMS app (due to incoming call or any other reason). This generate a new entry in SMS database with null value in 'address' column.
Salman
This is a normal behavior. Found in the SmsMessage documentation:
Returns the originating address (sender) of this SMS message in String
form or null if unavailable
If your app breaks because of a NPE later, you should insert a null check and set the address to an empty string.
An advice: A question should asked something and you should provide a stack trace if you have an error.
This can happen in case of masked number.

Retrieve contacts from Android Froyo 2.2

im using the following code segment to fetch contact names and numbers, it works fine on the emulator, but when i install the app in my Froyo 2.2.1, it just returns me the name, and instead of returning me the number it returns, 'null', can anyone help me solve this issue ?
will greatly appreciate any solution. Thanks
ContentResolver r = getContentResolver();
Cursor cursor = r.query(People.CONTENT_URI, null, null, null, null);
// Let activity manage the cursor
// startManagingCursor(cursor);
// Log.d(TAG, "cursor.getCount()=" + cursor.getCount());
// Get value from content provider
int nameIndex = cursor.getColumnIndex(People.NAME);
int numberIndex = cursor.getColumnIndex(People.NUMBER);//OrThrow(People.NUMBER);
cursor.moveToFirst();
StringBuilder s = new StringBuilder();
do {
String name = cursor.getString(nameIndex);
String number = cursor.getString(numberIndex);
s.append(name+ ": " + number + "\n");
} while (cursor.moveToNext());
The People API is deprecated try using the ContactsContract API which was already introduced for android 2.0+.
You can look at this blog post about using the contactscontract or the api documentation

How to get the number of sent SMS messages between specific dates in android

Is there a way to retrieve the number of sent SMS messages between specific dates in android?
I would prefer an officially supported SDK functionality, this stating in your answer whether this is part of the official SDK would be helpful.
I am aware of this stack overflow question but it seems to use the not officially supported android.provider.Telephony.SMS_RECEIVED and content://sms/sent, so I would rather not use it (please correct me if i'm wrong about this being unsupported).
I know it is very old question, I just came across and spotted as unanswered. So please do not downvote me just because of it. Maybe someone will need this answer.
All you have to do is to query the phone content provider (I used CallLog.Calls constants instead hardcoded strings for columns names)
String[] projection = { CallLog.Calls.DATE };
Uri smsContentUri = Uri.parse("content://sms/");
Cursor cursor = this.getContentResolver().query(smsContentUri , selection,where, null, null);
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yy");
cursor.moveToFirst();
Log.d("SMS COUNT", ""+cursor.getCount());
while(cursor.isAfterLast() == false){
Log.d("SMS DATE", ""+cursor.getString(cursor.getColumnIndex(CallLog.Calls.DATE))); //raw format of the date in milliseconds
long callDate = cursor.getLong(cursor.getColumnIndex(CallLog.Calls.DATE));
String dateString = format.format(new Date(callDate));
Log.i("SMS DATE",dateString);
cursor.moveToNext();
}
For your case all you have to do is to supply a where clause which will make selection over the projection. You need to convert bounds of dates you've mentioned in miliseconds (get the date and convert it to ms). Make sure your pattern corresponds to "dd-MM-yy".
The where clause should look like:
String where = CallLog.Calls.DATE+"<"+FRIST_BOUND_IN_MS + " AND "+ CallLog.Calls.DATE + "< " + SECOND_BOUND_IN_MS;
which take us to the final form of our query: SELECT date FROM sms_content WHERE date < 'firstbound' AND date < 'second bound'
Modify Uri smsContentUri = Uri.parse("content://sms/"); adding to the end of the "content://sms/" sent or inbox to get "content://sms/sent" or "content://sms/inbox", regarding which box you want to query.
Cheers.

Categories

Resources