Marking Sms Messages as Read/Unread or Deleting Messages Not Working in Kitkat

Marking SMS messages as read/unread or deleting messages not working in KitKat

With Android 4.4, several things have changed with regard to SMS. Among them is the fact that only the app that is registered as the default SMS app has write access to the provider.

Check here for a short blurb on changes to SMS.

Check this link for a more in depth look. This one explains what criteria your app needs to meet to be the default messaging app.

And here's the official fun stuff.

So, if your app is not the default messaging app, that would be why the specified functionalities have stopped working.


A possible workaround for the default Provider restriction can be found in the answer here.

Android KitKat (API 19) - How to write messages in SMS Content Provider, without sending them, from Non-Default App?

The SmsWriteOpUtils class uses reflection to access methods of the AppOpsManager Service in order to enable/disable a non-default SMS app's write access to the SMS Provider in API Level 19 (KitKat). Once set, an app's access mode will be retained until it is reset, or the app is uninstalled.

Enabling an app's write access allows that app all of the standard methods of interaction with the SMS Provider, including insert() and delete().

Please note that this class does no API Level check, and that the WRITE_SMS permission is still required.

import android.app.AppOpsManager;
import android.content.Context;
import android.content.pm.PackageManager;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public final class SmsWriteOpUtils {
private static final int WRITE_OP_CODE = 15;

public static boolean isWriteEnabled(Context context) {
int result = checkOp(context);
return result == AppOpsManager.MODE_ALLOWED;
}

public static boolean setWriteEnabled(Context context, boolean enabled) {
int mode = enabled ? AppOpsManager.MODE_ALLOWED : AppOpsManager.MODE_IGNORED;
return setMode(context, mode);
}

private static int checkOp(Context context) {
try {
Method checkOpMethod = AppOpsManager.class.getMethod("checkOp",
Integer.TYPE,
Integer.TYPE,
String.class);

AppOpsManager appOpsManager =
(AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
int uid = context.getApplicationInfo().uid;
String packageName = context.getPackageName();

return checkOpMethod.invoke(appOpsManager, WRITE_OP_CODE, uid, packageName);
}
catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
e.printStackTrace();
}
return -1;
}

private static boolean setMode(Context context, int mode) {
try {
Method setModeMethod = AppOpsManager.class.getMethod("setMode",
Integer.TYPE,
Integer.TYPE,
String.class,
Integer.TYPE);

AppOpsManager appOpsManager =
(AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
int uid = context.getApplicationInfo().uid;
String packageName = context.getPackageName();

setModeMethod.invoke(appOpsManager, WRITE_OP_CODE, uid, packageName, mode);

return true;
}
catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
e.printStackTrace();
}
return false;
}
}

Example usage:

boolean canWriteSms;

if(!SmsWriteOpUtils.isWriteEnabled(getApplicationContext())) {
canWriteSms = SmsWriteOpUtils.setWriteEnabled(getApplicationContext(), true);
}
...

NB: For regular user apps, this works only on API Level 19 (KitKat). The hole was patched in later versions.

Android delete sms messages in sent box

Starting with KitKat, any app with the SEND_SMS permission is able to send messages with the standard SmsManager methods, and the system will handle writing the messages to the Provider automatically. As the default app is the only one with write access to the Provider, it is the only one that can delete messages, so any non-default app won't be able to delete those messages written automatically.* If you don't want them to be written, your app should be set as the default SMS app. The default app is responsible for writing its own outgoing messages, and it can opt not to do so.


* A possible workaround for the write access restriction in Android 4.4 (KitKat) can be found in the answer here.

Hi , I need to delete sms from inbox in Android 4.4.4

in 4.4 and upper version of android you should make your app as default sms app to be able to write in sms database. to do so you can use link bellow to find out how you can make your app default you can see :
http://android-developers.blogspot.com/2013/10/getting-your-sms-apps-ready-for-kitkat.html

in this link you will understand how you can bring a message to your user to mark your app as default sms app.

How do I force keywords to automatically capitalize in the VIM editor?

Try:

:help abbreviations

For example:

:iab begin BEGIN

Now whenever you type "begin", it will be replaced by "BEGIN".



Related Topics



Leave a reply



Submit