Failed to retrieve the message from inbox in the real device - android

I am trying to get the inbox and sent messages. For this I have written my code below. My code runs well in the emulator but it doesn't work when I try to run it on a real device. It fails to collect the inbox messages even though there are messages in the inbox. I'd like to add that I want make the query with the contact number to retrieve the messages.
Cursor cursorsender = contentResolverSender.query(
Uri.parse("content://sms/sent"), null, "address = "
+ senderAddress, null, null);
Cursor cursor = contentResolver.query(Uri.parse("content://sms/inbox"),
null, "address = " + senderAddress, null, null);
my cursorsender.movetofirst() is returning false.

Related

Retrieve SMS sent after a specific time

I have a background service that listens to changes to Android SMS content provider. I store the last recorded id of the message that my Service recorded and using it, I want to find the number of messages that were sent when the service was Stopped/Killed.
So when the service is started again and another message is sent, I want to use the timestamp of the last recorded message. I use the below code to do this. My problem is that the cursor always returns 1 record irrespective of how many messages are sent when the service was killed before its restarted.
String COLUMN_NAME_DATE = "date"
String[] projection = new String[]{COLUMN_NAME_ID, COLUMN_NAME_DATE};
Cursor newMessagesCursor = mContext.getContentResolver().query(
Uri.parse(SMSManager.SMS_URI_SENT),
projection,
COLUMN_NAME_DATE + "> ? ",
new String[]{String.valueOf(lastMessageTimeStamp)}, null);
int newMessagesCount = (null != newMessagesCursor) ? newMessagesCursor.getCount() : 0;
Any pointers would be helpful. Let me know if there is any alternative approach as well.
Edit: Adding code to retrieve the lastMessageTimeStamp.
Cursor messageCursor = mContext.getContentResolver().query(
Uri.parse(SMSManager.SMS_URI_SENT),
projection,
COLUMN_NAME_ID + "= ? ",
new String[]{ lastMessageId}, null);
if(null != messageCursor && messageCursor.moveToFirst()) {
Long lastMessageTimeStamp = Long.parseLong(messageCursor.getString(messageCursor.getColumnIndex(COLUMN_NAME_DATE)));
}
Complete Source code for this class is located at:
https://github.com/midhunhk/message-counter/blob/unicorn/messageCounter/src/main/java/com/ae/apps/messagecounter/observers/SMSObserver.java
I was able to solve this issue by using a different approach.
It seems like when we query the table SMSManager.SMS_URI_SENT as in the question, it is only returning 1 row which is the last message that was sent.
Cursor newMessagesCursor = context.getContentResolver().query(
Uri.parse(SMSManager.SMS_URI_ALL),
MessagesTableConstants.SMS_TABLE_PROJECTION,
"person is null and " + MessagesTableConstants.COLUMN_NAME_DATE + "> ? ",
new String[]{String.valueOf(lastMessageTimeStamp)}, null);
I query for SMSManager.SMS_URI_ALL and check for person column as null which represents messages that are sent.

How to delete particular inbox message in Android version 5.0 lollipop or in Kitkat?

I am making to delete particular sms of phone number task. when I am testing in motog or Android version 5.0's mobile. I can't delete particular number's sms. My code snippet is below.
public void deleteSMS(Context context,String number) {
try {
Log.d("","Deleting SMS from inbox");
Uri uriSms = Uri.parse("content://sms/inbox");
Cursor c = context.getContentResolver().query(uriSms,
new String[] { "_id", "thread_id", "address",
"person", "date", "body" }, "address = '"+number+"'", null, null);
if (c != null && c.moveToFirst()) {
do {
long id = c.getLong(0);
long threadId = c.getLong(1);
String address = c.getString(2);
String body = c.getString(5);
Toast.makeText(getApplicationContext(),"SMS with id: " + threadId +" Number:- " +address,Toast.LENGTH_LONG).show();
Log.d("", "SMS with id: " + threadId +" Number:- " +address);
if ( address.equals(number)) {
Log.d("", "Deleting SMS with id: " + threadId);
context.getContentResolver().delete(
Uri.parse("content://sms/" + id), null, null);
}
} while (c.moveToNext());
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(),"Could not delete SMS from inbox ",Toast.LENGTH_LONG).show();
Log.e("", "Could not delete SMS from inbox: " + e.getMessage());
}
}
After 4.4 you are not allowed to delete any sms messages from inbox unless your app is the "default sms app"
Beginning with Android 4.4, the system settings allow users to select a "default SMS app." Once selected, only the default SMS app is able to write to the SMS Provider and only the default SMS app receives the SMS_DELIVER_ACTION broadcast when the user receives an SMS or the WAP_PUSH_DELIVER_ACTION broadcast when the user receives an MMS. The default SMS app is responsible for writing details to the SMS Provider when it receives or sends a new message.
Other apps that are not selected as the default SMS app can only read
the SMS Provider...
You can see More info here
just mentioned the important part below:
if your app is designed to behave as the default SMS app, then while your app is not selected as the default, it's important that you understand the limitations placed upon your app and disable features as appropriate. Although the system writes sent SMS messages to the SMS Provider while your app is not the default SMS app, it does not write sent MMS messages and your app is not able to write to the SMS Provider for other operations, such as to mark messages as draft, mark them as read, delete them, etc.

Unable to delete SMS programmatically

I have tried variations on deleting all or some messages from the SMS, including but not limited to thread id, id, where clause, etc.
Android manifest reflects read and write permissions.
I tried with variations on the SMS content, from inbox etc.
Nothing seems to delete the record.
Here is the last iteration:
Cursor c = getApplicationContext().getContentResolver().query(Uri.parse("content://sms/"), null, null, null,null);
try {
while (c.moveToNext()) {
int Id = c.getInt(0);
String pid = c.getString(0);
// String uri = "content://sms/conversations/" + threadId;
String strUriAll = "content://sms/" + pid;//SMS_ALL
Log.d("URI is ", strUriAll);
getApplicationContext().getContentResolver().delete(Uri.parse(strUriAll), null, null);
// getApplicationContext().getContentResolver().delete(Uri.parse(strUriAll), null, null);
}
}catch(Exception e){
Log.e(this.toString(),"Error deleting sms",e);
}finally {
c.close();
}
Android 4.4 Kitkat introduced changes in SMS handling where now only the default SMS app is allowed write access to the SMS database - all other apps silently fail when attempting to write.

query the latest SMS messages group by contact

Like the Message app, the first Activity displays the lastest SMS grouped by person and the count number.
Example I had exchanged 50 texts with Chris, 60 with Aline, 40 with Ray ...
The app displays something like this
Chris (50) Hey how are you lately ?
Aline (60) Let's catch up
Ray (40) Here is my number
Ethan (1) I wrote a solution
I'm trying to do the same . I query all the SMS then I sort them.
At least it's O(n) efficient .
Cursor cur = this.getContentResolver().query(Uri.parse("content://sms"), null, null, null, null);
int count = cur.getCount();
// iterate all the SMS
for (int i=1 ; i<=count ; i++){
// processing
....
cur.moveToNext();
}
Is there a way to query the latest messages (received or sent no draft type) grouped by person and get the number of SMS from a person ?
I suppose there are multiple queries.
I think this will do what you want:
Cursor cur = this.getContentResolver().query(Uri.parse( "content://mms-sms/conversations?simple=true"), null, null, null, "normalized_date desc" );
The returned cursor will contain the latest message in every conversation and the amount of messages in each conversation.

How to detect that i have received an email/gmail on my Android Device?

Is there a way to detect or an event being triggered when i receive a gmail/email on my android device.
Basically i would like my application to read the email notification when i receive it.
Could you kindly tell me if there is way to do this ?
You need to register a content observer (not broadcast receiver)
contentResolver.registerContentObserver(Uri.parse("content://gmail-ls"), true, gmailObserver);
gmailObserver is your own ContentObserver object.
ContentObserver.onChange is going to be called every time something changes in Gmail.
Here you get all conversations like so:
Cursor conversations = _contentResolver.query(Uri.parse("content://gmail-ls/conversations/" + YourEmailAddress), null, null, null, null);
And the actual conversation messages will be:
Cursor messages = _contentResolver.query(Uri.parse("content://gmail-ls/conversations/" + YourEmailAddress + "/" + String.valueOf(conversationId) + "/messages"), null, null, null, null);

Categories

Resources