0

Below code is not working. If you see the output, it always return 0 no matter what I try ? Is there anything wrong OR API is deprecated or something ?

Please help

Code :

public static final String ALL = "content://sms"; 
String where = "_id = 2";
ContentValues StatusValues = new ContentValues();
StatusValues.put(TextBasedSmsColumns.READ, 0);
int cursor = getContentResolver().update(Uri.parse(ALL), StatusValues, where, null);
Log.i("COS:int", cursor+"");

where = "_id = '2'";
cursor = getContentResolver().update(Uri.parse(ALL), StatusValues, where, null);
Log.i("COS:int", cursor+"");

where = "_id = ?";
cursor = getContentResolver().update(Uri.parse(ALL), StatusValues, where, new String[]{"2"});
Log.i("COS:int", cursor+"");

where = null;
cursor = getContentResolver().update(Uri.parse(ALL), StatusValues, where, null);
Log.i("COS:int", cursor+"");

Output :

COS:int  0
COS:int  0
COS:int  0
COS:int  0

Shouldn't it return the number of columns changed ?

Bharat
  • 750
  • 1
  • 9
  • 20
  • 1
    If you're running under KitKat or above, your app has to be the default SMS app to have write access to the Provider. Otherwise, inserts, updates, and deletes will fail like that. That is, no crash, necessarily, they just won't do anything. – Mike M. Jul 24 '16 at 05:36
  • I am using 5.1.1 – Bharat Jul 24 '16 at 05:38
  • 1
    Yep, KitKat is 4.4, so you're on a version that disallows non-default apps from writing to the Provider. http://stackoverflow.com/questions/20158998/marking-sms-messages-as-read-unread-or-deleting-messages-not-working-in-kitkat – Mike M. Jul 24 '16 at 05:40
  • @MikeM. Thanks for info. I have to do so much just to change some value in SMS DB ? like registering broadcast receiver etc . .. ? – Bharat Jul 24 '16 at 05:44
  • Well, yeah, pretty much. The thing is, though, if you want your app to act as a user's default SMS app full-time, you need to handle a _bunch_ of stuff, like saving all incoming messages, Notifications, handling MMS, etc. The default is responsible for a lot. There might be another option, however, if you just need to delete stuff occasionally, but it would involve the user temporarily setting your app as the default, then switching back afterwards. – Mike M. Jul 24 '16 at 05:49

0 Answers0