我怎么能拦截来电短信与特定文字 [英] How Can i INTERCEPT an Incoming SMS With A specific Text

查看:105
本文介绍了我怎么能拦截来电短信与特定文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解一下拦截传入的短信特定关键字EX:你好,这样我就可以读取包含嗨的短信在资讯科技及电讯;阅读味精,和功放后删除;如果该味精不包含任何这样的文本,然后它不会被删除,而不是保存在收件箱中。伙计们,请帮助我,我发现它很难执行此功能。

I want to know about intercepting an incoming sms for a specific key word EX:"Hi", so that I can read that sms containing "Hi" in it & delete it after reading the msg, & if that msg doesn't contain any such text then it wouldn't be deleted and instead saved in the inbox. Guys please help me I am finding it very difficult to perform this functionality.

推荐答案

查找广播接收器,这是依赖于在手机上安装这些应用程序,但你可以给你的应用程序优先级听消息。虽然,通知显示时,会不会是消息中的SMS数据库还没有,所以你需要使用abortBroadcast()停止被通知其他应用程序。见下面的例子:

Look for Broadcast Receiver, this is dependent on the apps installed on the phone but you can give your app priority for listening to messages. Although, when a notification is shown, the message won't be in the SMS Database yet, so you will need to use abortBroadcast() to stop other apps being notified. See example below:

public class MessageReceiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
             Bundle pudsBundle = intent.getExtras();
             Object[] pdus = (Object[]) pudsBundle.get("pdus");
             SmsMessage messages =SmsMessage.createFromPdu((byte[]) pdus[0]);    
             Log.i(TAG,  messages.getMessageBody());
                 if(messages.getMessageBody().contains("Hi")) {
                     abortBroadcast();
                 }
    }

和你需要声明的接收器在清单中,像这样:

And you would need to declare the receiver in the manifest, like so:

 <receiver android:name="com.encima.smsreceiver.MessageReceiver" android:exported="true">
    <intent-filter android:priority="999">
        <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
    </intent-filter>

最后,请确保您有在清单,希望帮助到RECEIVE_SMS权限!

Finally, make sure you have the permission to RECEIVE_SMS in the manifest, hope that helps!

这篇关于我怎么能拦截来电短信与特定文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆