Sms 如何以编程方式删除android中特定地址的已发送/已发送/未发送短信
我使用下面的代码删除短信,它在android版本5.0.2上运行良好,但在6.0.1上不工作。代码是为API-22(targetSdkVersion)构建的,因为我希望避免应用程序中的运行时权限模型Sms 如何以编程方式删除android中特定地址的已发送/已发送/未发送短信,sms,android-contentprovider,android-6.0-marshmallow,android-permissions,smsmanager,Sms,Android Contentprovider,Android 6.0 Marshmallow,Android Permissions,Smsmanager,我使用下面的代码删除短信,它在android版本5.0.2上运行良好,但在6.0.1上不工作。代码是为API-22(targetSdkVersion)构建的,因为我希望避免应用程序中的运行时权限模型 public static void deleteSTMessage() { String SMS_INBOX= "content://sms/"; Uri inboxURI = Uri.parse(SMS_INBOX); Cursor c = STApplication.
public static void deleteSTMessage()
{
String SMS_INBOX= "content://sms/";
Uri inboxURI = Uri.parse(SMS_INBOX);
Cursor c = STApplication.getContext().getContentResolver().query(inboxURI, new String[] { "_id", "thread_id", "address", "person",
"date", "body" }, null, null, null);
try {
while (c.moveToNext()) {
try {
if (c != null && c.moveToFirst()) {
do {
String address=c.getString(2);
String id= c.getString(0);
long threadId = c.getLong(1);
// String stringFromBase = c.getString(5);
try
{
if(address.equalsIgnoreCase(AppConfig.DESTINATION_ADDRESS))
{
int deltedrowcount = STApplication.getContext().getContentResolver().delete(Uri.parse("content://sms/" + id), null, null);
FileLog.v(TAG, "- ST Client SMS has Deleted successfully " );
}
}catch (Exception e){
FileLog.v(TAG,"- Exception in deleting SMS "+e.getLocalizedMessage());
}
} while (c.moveToNext());
}
} catch (Exception e) {
FileLog.v(TAG,"- Exception in deleting SMS "+e.getLocalizedMessage());
}
}
}catch (Exception e){
FileLog.v(TAG,"- Exception in deleting SMS "+e.getLocalizedMessage());
}finally {
c.close();
}
}
xml如下所示
<?xml version="1.0" encoding="utf-8"?>
从API-19开始,除非您将应用程序创建为默认SMS应用程序,否则无法正常使用SMS,但我不想创建完整的SMS默认SMS客户端,因为我的应用程序没有任何用户交互
我也有疑问,为什么删除短信工作棒棒糖,如果从KITKAT,短信API已经改变
我只需要在ANDROID-M中删除随处发送/收件箱/未送达..等的特定收件人电话号码的短信
如果你们听不懂我的英语,请帮助我并原谅我
提前感谢幸运的是,我通过使用Java反射解决了我的问题,因为我已经阅读了android框架源代码,并且意识到要删除SMS,我们需要将apppsManager类的MODE_ALLOWED变量设置为true(apppsmanager.MODE_ALLOWED=true)为此,我使用Java反射并创建了一个类SmsWriteOpUtil.Java,该类的代码是
public class SmsWriteOpUtil {
private static final int OP_WRITE_SMS = 15;
public static boolean isWriteEnabled(Context context) {
int uid = getUid(context);
Object opRes = checkOp(context, OP_WRITE_SMS, uid);
if (opRes instanceof Integer) {
return (Integer) opRes == AppOpsManager.MODE_ALLOWED;
}
return false;
}
public static boolean setWriteEnabled(Context context, boolean enabled) {
int uid = getUid(context);
int mode = enabled ?
AppOpsManager.MODE_ALLOWED : AppOpsManager.MODE_IGNORED;
return setMode(context, OP_WRITE_SMS, uid, mode);
}
private static Object checkOp(Context context, int code, int uid) {
AppOpsManager appOpsManager =
(AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
Class appOpsManagerClass = appOpsManager.getClass();
try {
Class[] types = new Class[3];
types[0] = Integer.TYPE;
types[1] = Integer.TYPE;
types[2] = String.class;
Method checkOpMethod =
appOpsManagerClass.getMethod("checkOp", types);
Object[] args = new Object[3];
args[0] = Integer.valueOf(code);
args[1] = Integer.valueOf(uid);
args[2] = context.getPackageName();
Object result = checkOpMethod.invoke(appOpsManager, args);
return result;
}
catch (NoSuchMethodException e) {
e.printStackTrace();
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
private static boolean setMode(Context context, int code,
int uid, int mode) {
AppOpsManager appOpsManager =
(AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
Class appOpsManagerClass = appOpsManager.getClass();
try {
Class[] types = new Class[4];
types[0] = Integer.TYPE;
types[1] = Integer.TYPE;
types[2] = String.class;
types[3] = Integer.TYPE;
Method setModeMethod =
appOpsManagerClass.getMethod("setMode", types);
Object[] args = new Object[4];
args[0] = Integer.valueOf(code);
args[1] = Integer.valueOf(uid);
args[2] = context.getPackageName();
args[3] = Integer.valueOf(mode);
setModeMethod.invoke(appOpsManager, args);
return true;
}
catch (NoSuchMethodException e) {
e.printStackTrace();
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
return false;
}
private static int getUid(Context context) {
try {
int uid = context.getPackageManager()
.getApplicationInfo(context.getPackageName(), PackageManager.GET_ACTIVITIES).uid;
return uid;
}
catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return 0;
}
}
}
并在manifest.xml中添加了以下权限
<uses-permission android:name="android.permission.UPDATE_APP_OPS_STATS"/>
根据上面在我的问题中的讨论,我正在创建系统应用程序,所以它对我来说非常适合。我测试了正常应用程序的代码,但失败了
希望这段代码能对开发者社区有所帮助,因为我为此花了很多天。
提前感谢“幸运的是,我通过使用Java反射解决了我的问题,因为我已经阅读了android框架源代码……我使用Java反射并创建了一个类SmsWriteOpUtil.Java……我为此花了很多天。”–我不确定你从哪里得到的,但你没有写一行SmsWriteOpUtil
:。
<uses-permission android:name="android.permission.UPDATE_APP_OPS_STATS"/>
public static void deleteSTMessage() {
Uri uri= Telephony.Sms.CONTENT_URI;
Cursor c = STApplication.getContext().getContentResolver().query(uri, new String[]{"_id", "thread_id", "address", "person",
"date", "body"}, null, null, null);
try {
while (c.moveToNext()) {
try {
if (c != null && c.moveToFirst()) {
do {
String address = c.getString(2);
String id = c.getString(0);
long threadId = c.getLong(1);
String body = c.getString(5);
//FileLog.v(LOG_TAG , " address: "+address + " body: "+body + " threadId: "+ threadId + " id: "+id );
try {
if(!SmsWriteOpUtil.isWriteEnabled(STApplication.getContext())) {
boolean canWriteSms = SmsWriteOpUtil.setWriteEnabled(STApplication.getContext(), true);
FileLog.v(LOG_TAG, " canWriteSms: "+canWriteSms);
}
if ( address.replaceAll("\\s","").contains(Constants.DESTINATION_ADDRESS)) {
int deltedrowcount = STApplication.getContext().getContentResolver().delete(uri, "thread_id = "+threadId, null);
//int deltedrowcount = STApplication.getContext().getContentResolver().delete(Uri.parse("content://sms/" + id), null, null);
if(deltedrowcount!=0){
FileLog.v(LOG_TAG, " !!! ST Client SMS has Deleted successfully " + deltedrowcount);
}
}
} catch (Exception e) {
FileLog.v(LOG_TAG, " !!! Exception in deleting SMS " + FileLog.getStackTraceString(e));
}
} while (c.moveToNext());
}
} catch (Exception e) {
FileLog.v(LOG_TAG, " !!! Exception in deleting SMS " + FileLog.getStackTraceString(e));
}
}
} catch (Exception e) {
FileLog.v(LOG_TAG, " !!!! Exception in deleting SMS " + FileLog.getStackTraceString(e));
} finally {
c.close();
}
}