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.
Related
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.
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 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.
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
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)