![](/img/trans.png)
[英]How to read my own sent sms in the "sms content provider" for Android Studio
[英]Android Studio: Delete sent and recieved sms by particular number
我想知道如何删除特定号码发送和接收的短信。 我的应用程序正在与其他设备的 GSM 模块连接,它发送和接收 SMS 消息。 我想删除这些短信。 下面是我的代码:
@Override
public void onReceive(Context context, Intent intent) {
if (Objects.equals(intent.getAction(), SMS_RECEIVED)) {
String smsSender = "";
String smsBody = "";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
StringBuilder smsBodyBuilder = new StringBuilder();
for (SmsMessage smsMessage : Telephony.Sms.Intents.getMessagesFromIntent(intent)) {
smsSender = smsMessage.getDisplayOriginatingAddress();
smsBodyBuilder.append(smsMessage.getMessageBody());
}
smsBody = smsBodyBuilder.toString();
} else {
Bundle smsBundle = intent.getExtras();
if (smsBundle != null) {
Object[] pdus = (Object[]) smsBundle.get("pdus");
if (pdus == null) {
// Display some error to the user
return;
}
SmsMessage[] messages = new SmsMessage[pdus.length];
StringBuilder smsBodyBuilder = new StringBuilder();
for (int i = 0; i < messages.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
smsBodyBuilder.append(messages[i].getMessageBody());
}
smsBody = smsBodyBuilder.toString();
smsSender = messages[0].getOriginatingAddress();
}
}
if (smsSender != null) {
if (smsSender.equals(serviceProviderNumber) && smsBody.startsWith(serviceProviderSmsCondition)) {
this.pdCanceller.removeCallbacks(this.progressRunnable);
this.message = smsBody;
context.unregisterReceiver(broadcastReceiver);
progressDialog.cancel();
SmsReceiverDialog smsReceiverDialog = new SmsReceiverDialog(this.activity, this.context, this.message);
checkCommand(smsBody, smsReceiverDialog); // call correctly function from list
context.getContentResolver().delete(Uri.parse("content://sms/"), "address=?", new String[]{smsSender});
}
}
}
}
源代码是捕获 SMS 消息。 我对 where 子句有疑问。 删除短信不起作用。
从删除-android-sms-programmatically中归功于“Maksim Dmitriev”的答案
请注意,您无法在使用 Android 4.4 的设备上删除 SMS 消息。
此外,系统现在只允许默认应用程序将消息数据写入提供程序,尽管其他应用程序可以随时读取。
http://developer.android.com/about/versions/kitkat.html
尝试不会抛出异常; 什么都不会被删除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.