Delete incoming SMS in android marshmallow 6.0? [duplicate] - android

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.

Related

Delete message from inbox or sent programmatically in Android lollipop and marshmallow

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.

How to display all the sms' the user received and group them

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.

Mark incoming SMS as Read in Default SMS Application

Sorry if i did not come up with a suitable title.
I want to know that is it possible to mark SMS as read in the default SMS Application through my Application?
i.e. i am developing my own application in which i am storing sms that are not from the contact list but the problem is that when ever sms received it received on my app and also on the default sms app so i want that when ever sms received then i can mark the sms as read(of the default SMS App) so that there is no need for go to default sms app and mark SMS as read.
Can i do that from my Application?
try this code
private void markMessageASRead(Context context, String numb, String body) {
Uri uri = Uri.parse("content://sms/inbox");
Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
try{
while (cursor.moveToNext()) {
if ((cursor.getString(cursor.getColumnIndex("address")).equals(number)) && (cursor.getInt(cursor.getColumnIndex("read")) == 0)) {
if (cursor.getString(cursor.getColumnIndex("body")).startsWith(body)) {
String SmsMessageId = cursor.getString(cursor.getColumnIndex("_id"));
ContentValues values = new ContentValues();
values.put("read", true);
context.getContentResolver().update(Uri.parse("content://sms/inbox"), values, "_id=" + SmsMessageId, null);
return;
}
}
}
}catch(Exception e)
{
Log.e("Mark Read", "Error in Read: "+e.toString());
}
}

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.

Categories

Resources