我们可以在 Android 中的短信到达收件箱之前删除它吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1741628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:34:15  来源:igfitidea点击:

Can we delete an SMS in Android before it reaches the inbox?

androidsms

提问by umakant

I am deleting an SMS from the inbox but I want to know: How can I delete it before it reaches the inbox?

我正在从收件箱中删除一条短信,但我想知道:如何在它到达收件箱之前将其删除?

回答by Christopher Orr

Yes.

是的。

Despite some negative reactions to this question, there are legitimate uses for SMS interception. For example: automating phone number verification, services which are provisioned via SMS (though generally this should be done with data SMS), or for applications which otherwise improve the user experience by processing specially-formatted messages in order to show them in a nice Android-specific UI.

尽管对这个问题有一些负面反应,但短信拦截还是有合法用途的。例如:自动电话号码验证、通过 SMS 提供的服务(尽管通常这应该通过数据 SMS 完成),或者用于通过处理特殊格式的消息来改善用户体验以在漂亮的 Android 中显示它们的应用程序- 特定的用户界面。

As of Android 1.6, incoming SMS message broadcasts (android.provider.Telephony.SMS_RECEIVED) are delivered as an "ordered broadcast" — meaning that you can tell the system which components should receive the broadcast first.

从 Android 1.6 开始,传入的 SMS 消息广播 ( android.provider.Telephony.SMS_RECEIVED) 作为“有序广播”传送——这意味着您可以告诉系统哪些组件应该首先接收广播。

If you define an android:priorityattribute on your SMS-listening <intent-filter>, you will then receive the notification before the native SMS application.

如果您android:priority在 SMS-listening 上定义一个属性<intent-filter>,那么您将在本机 SMS 应用程序之前收到通知。

At this point, you can cancelthe broadcast, preventing it from being propagated to other apps.

此时,您可以取消广播,防止其传播到其他应用程序。



Update (October 2013):When Android 4.4 arrives, it will make changes to the SMS APIs which may affect an app's ability to influence SMS delivery.
Check out this Android Developers blog post for some more info:
http://android-developers.blogspot.com/2013/10/getting-your-sms-apps-ready-for-kitkat.html

更新(2013 年 10 月):当 Android 4.4 到来时,它将对 SMS API 进行更改,这可能会影响应用程序影响 SMS 传送的能力。
查看此 Android 开发人员博客文章了解更多信息:http:
//android-developers.blogspot.com/2013/10/getting-your-sms-apps-ready-for-kitkat.html

回答by Sunil Kumar Sahoo

Step-1:Create your custom broadcast receiver to receive sms. write the logic to abort the broadst so that the message will not be available to your inbox

步骤 1:创建您的自定义广播接收器以接收短信。编写中止最广泛的逻辑,以便您的收件箱无法使用该消息

public class SMSReceiver extends BroadcastReceiver
{
    public void onReceive(Context context, Intent intent)
    {
     if(conditionMatches){
     abortBroadcast();
     }
    }
}

Step-2Register broadcast receiver in AndoridManifest and put android:priority value a large number

步骤 2在 AndoridManifest 中注册广播接收器并将 android:priority 值设置为大数

<receiver android:name=".SMSReceiver" >
            <intent-filter android:priority="1000">
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>

Thats It

就是这样

How does the above code work

上面的代码是如何工作的

As SMS receiving broad cast is an ordered broadcast the receiver with high priority will receive first so your application receive first and after receiving you are aborting broadcast. So no other application can receive it. Hence the sms will not exist in inbox

由于 SMS 接收广播是有序广播,因此具有高优先级的接收器将首先接收,因此您的应用程序首先接收,接收后您将中止广播。所以没有其他应用程序可以接收它。因此收件箱中将不存在短信

回答by Subba

The below("android:priority" and abortBroadcast()) solution works as long as Android Messaging application as default(I meant stock Android Messaging application). If user installs "GoSMSPro" or "HandcentSMS", these applications still show messages in inbox, I believe this due to "android:priority". I don't see any better way to fix the above issue, if third party messaging applications installed on the phone.

下面的("android:priority" 和 abortBroadcast()) 解决方案只要 Android Messaging 应用程序作为默认值(我的意思是股票 Android Messaging 应用程序)就可以工作。如果用户安装“GoSMSPro”或“HandcentSMS”,这些应用程序仍会在收件箱中显示消息,我相信这是由于“android:priority”。如果手机上安装了第三方消息传递应用程序,我看不出有什么更好的方法可以解决上述问题。

回答by taran mahal

/**
 * Check priority
 * @param activity
 */
public static void receiverPriority(Activity activity){

    Intent smsRecvIntent = new Intent("android.provider.Telephony.SMS_RECEIVED");
    List<ResolveInfo> infos =  activity.getPackageManager().queryBroadcastReceivers(smsRecvIntent, 0);
    for (ResolveInfo info : infos) {
        System.out.println("Receiver: " + info.activityInfo.name + ", priority=" + info.priority);
    }
}

Check priority and set higher priority (in your manifest) than other receivers.

检查优先级并设置比其他接收器更高的优先级(在您的清单中)。

回答by Pir Fahim Shah

If you have a scenario like this and you want to delete or ignore the message related to this contact number "+44xxxxx" etc, then use this code in SMS Broadcast receiver

如果您有这样的场景,并且您想删除或忽略与此联系电话“+44xxxxx”等相关的消息,请在 SMS Broadcast 接收器中使用此代码

 if(sender.equalsIgnoreCase("+44xxxxxx")
   this.abortBroadCast();

You also have to set it the high priority.

您还必须将其设置为高优先级。