Is there a way to figure out a contents URI for SMS data for a specific android device?
For example, I was able to locate the DB containing all messages following this.
SMS DB is located at ./dbdata/databases/com.android.providers.telephony/mmssms.db
However, I want to know the contents URI rather than the actual location (e.g. contents://sms). Is this possible? If not, how do I gather information about stored SMS messages from different devices that have different and unknown content URIs for SMS?
For example, these are the contents URI for some vendors:
LG: content://com.lge.messageprovider/msg/inbox
SAMSUNG: content://com.sec.mms.provider/message
SAMSUNG Galaxy A: content://com.btb.sec.mms.provider/message
General: content://sms/inbox
Why don't you use uri parser of Contentresolver, especially that you know content string.
Try this (in connection with Cursor):
Cursor cursor = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);
cursor.moveTofirst();
//read all messages in your inbox
do
{
String message = "";
//read all data from all available columns for each message
for (int i = 0; i < cursor.getColumnCount(); i++)
{
message += " " + cursor.getComulnName(i) + ": " + cursor.getString(i);
//here do wathever you want with your message string
}
}
while(cursor.moveToNext());
cursor.close();
dont forget do add READ_SMS permission to your AndroidManifest.xml
Related
I want to delete messages of particular contact number programmatically in Android Lollipop and Marshmallow.
I am using the following code to read and delete the messages... but this code does not actually delete messages:
public List<Sms> getAllSms() {
try {
List<Sms> lstSms = new ArrayList<Sms>();
Sms objSms = new Sms();
Uri uriSms = Uri.parse("content://sms/");
Cursor c = getActivity().getContentResolver().query(uriSms, new String[]{"_id", "thread_id", "address", "person", "date", "body"}, null, null, null);
getActivity().startManagingCursor(c);
int totalSMS = c.getCount();
if (c.moveToFirst()) {
for (int i = 0; i < totalSMS; i++) {
long id = c.getLong(0);
long threadId = c.getLong(1);
String address = c.getString(2);
String body = c.getString(5);
String date = c.getString(3);
Log.e("log>>>",
"0--->" + c.getString(0) + "1---->" + c.getString(1)
+ "2---->" + c.getString(2) + "3--->"
+ c.getString(3) + "4----->" + c.getString(4)
+ "5---->" + c.getString(5));
Log.e("log>>>", "date" + c.getString(0));
if (address.equals("1234567890")) {
// mLogger.logInfo("Deleting SMS with id: " + threadId);
getContext().getContentResolver().delete(
Uri.parse("content://sms/" + id), null, null);
Log.e("log>>>", "Delete success.........");
}
c.moveToNext();
}
} else {
throw new RuntimeException("You have no SMS ");
}
c.close();
return lstSms;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
Manifest permissions:
<uses-permission android:name="android.permission.WRITE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
Please help me regarding to delete the messages.
OR
suggest me a way to do the following:
If I send message in background programatically in both the above Android version then it should not save messages in sent folder or anywhere in the device.
i want to delete message of particular contact number programmatically in android lollipop and marshmallow
You will need to write a complete SMS client.
You cannot implement an app that only deletes messages on API Level 19 and higher. The user will not make your app be the default SMS app on the device. If your app is not the default SMS app, then "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." See this official blog post for more.
Tell me the way that if i send message in background programatically in both the above android version. then it should not save in sent folder or anywhere in the device
The decision of what happens with sent SMS messages is made between Android, the user's default SMS client, and possibly the user. You do not get a vote.
I am building an sms app to send and receive sms'.I am able to send and receive the sms'. I want the app to show all the messages received by the user, even those that are received by the user before the app install and the messages should be grouped by the sender, i.e. All the messages sent by one sender can be seen together but don't have any idea how to do this.
Can anyone help me on this?
You can accomplish what you want with this code, don't forget to add android.permission.READ_SMS permission on the manifest:
Cursor cursor = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);
if (cursor.moveToFirst()) // Check if there is SMS'
do {
String smsData = "";
for(int i = 0; i < cursor.getColumnCount(); i++) {
smsData += "\n" + cursor.getColumnName(i) + ":" + cursor.getString(i);
}
// TODO Do what you want with smsData
} while (cursor.moveToNext());
else
// There's no SMS to show
Despite of that, there is no guarantee that this would work in all Android devices as this ContentProvider is not officially supported by the Android SDK and Google does not recommed to use it as you can read here.
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.
I am looking for code to get the total sent-sms count in android mobiles. If I use content://sms/sent/ for querying the database I get the sms count of the available sent sms in the mobile, but I want the total sms that is sent from buying the mobile.
Retrieve All SMS messages in the Android
The URI (content://sms/) will used to retrive the all SMS messages, also we can split up by the message type as well as.
Uri allMessage = Uri.parse("content://sms/");
ContentResolver cr = getContentResolver();
Cursor c = cr.query(allMessage, null, null, null, null);
while (c.moveToNext()) {
String row = c.getString(1);
}
you can get the all inbox/outbox here
change your URI
use this one: content://sms/
I am trying to create app, that will get the text from SMS, and use it in textview. So something like this, message is recived, i check if it is message I want, then i extract text, save it to string, and then show this string in textview. Any suggestions from where should i start, any examples plese ??
You can start here for handling received SMS.
First I would listen for SMS incoming, and on incoming SMS show a notification. Then if the user opens your app, update your display using this to get the data you want:
Uri allMessage = Uri.parse("content://sms/inbox");
ContentResolver cr = getContentResolver();
Cursor c = cr.query(allMessage, null, null, null, null);
//shows one message
c.moveToNext();
//uncomment to cycle thru ALL messages... This will take AWHILE
//while (c.moveToNext()) {
for(int i = 0; i != c.getColumnCount(); i++){
String columnName = c.getColumnName(i);
String columnValue = c.getString(i);
Log.v(TAG, "Col: " + columnName);
Log.v(TAG, "Val: " + columnValue);
}
//}
Play around with it a little. It Should have all of the data you need (distinguish SMSs by timestamp)