短信删除的总结

删除单条短信:MessageBoxList.this.getContentResolver().delete(
Uri.parse("content://sms/conversations/" + thread),
"date=?", new String[] { bean.getDate1() });
删除单个会话内的所有短信:Uri mUri = Uri.parse("content://sms/conversations/"
+ bean.getThread_id());
SMSListActivity.this.getContentResolver().delete(mUri, null,
null);
取代系统短信应用:String myPackageName = getPackageName();
if (!Telephony.Sms.getDefaultSmsPackage(this).equals(
myPackageName)) {
// App is not default.
// Show the "not currently set as the default SMS app"
// interface

Intent intent = new Intent(
Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME,
myPackageName);
startActivity(intent);
}
//Android 4.4之后删除短信
 
if (!SmsWriteOpUtil.isWriteEnabled(getApplicationContext())) {
SmsWriteOpUtil.setWriteEnabled(
getApplicationContext(), true);
}


cr.delete(Uri.parse("content://sms/"), "_id=" + listdelete.get(i).get_id().trim(), null);// 


//删除短信Utils
package com.lvshandian.menshen.utils;

import android.app.AppOpsManager;
import android.content.Context;
import android.content.pm.PackageManager;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
* Created by zhang on 2016/11/14.
*/

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_SERVICES).uid;

return uid;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return 0;
}
}

}





 
 
 
原文地址:https://www.cnblogs.com/huihuizhang/p/6052124.html