简体   繁体   English

阅读Android 4.4中的传入短信

[英]Read Incoming SMS in Android 4.4

As I have read from multiple sources that there are some changes in APIs in Android 4.4 onwards etc. True enough, my code is not working now :( 正如我从多个来源中了解到的那样,Android 4.4及更高版本中的API有所更改,等等。的确,我的代码现在无法正常工作:(

Basically, I want to read incoming SMS and filter possible spams by deleting the spam SMSes. 基本上,我想读取传入的SMS并通过删除垃圾短信来过滤可能的垃圾邮件。 Since deletion could not be done now, I am trying to just prompt that the newly received SMS is potential spam. 由于现在无法删除,因此我仅提示您新接收的SMS可能是垃圾邮件。

Below is my source snippet for non Android 4.4 versions: 以下是我的非Android 4.4版本的源代码片段:

public class Sms extends BroadcastReceiver{

    private static final String ACTION = "android.provider.Telephony.SMS_DELIVER";

     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("Adv")) {

                 abortBroadcast();
             }
     }

} }

My Manifest file: 我的清单文件:

<receiver android:name=".Sms" android:permission="android.permission.BROADCAST_SMS">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_DELIVER" />
            </intent-filter>                        
</receiver>

What must I change to make it work for Android 4.4? 为了使其适用于Android 4.4,我必须更改什么? I just want to read the incoming SMSes without doing abortBroadcast. 我只想阅读传入的SMS,而无需执行abortBroadcast。

Since 4.4 only default SMS app will receive SMS_DELIVER_ACTION broadcasts. 从4.4开始,仅默认的SMS应用程序将接收SMS_DELIVER_ACTION广播。 Other apps must now use SMS_RECEIVED_ACTION broadcasts, this is introduced in API level 19 and can not be aborted. 现在其他应用程序必须使用SMS_RECEIVED_ACTION广播,这是API级别19中引入的,不能中止。

Official page here... 官方页面在这里...

Managed to find it. 设法找到它。 You can refer to this tutorial: 您可以参考本教程:

http://code.tutsplus.com/tutorials/use-text-to-speech-on-android-to-read-out-incoming-messages--cms-22524 http://code.tutsplus.com/tutorials/use-text-to-speech-on-android-to-read-out-incoming-messages--cms-22524

Explains how to read incoming SMSes. 说明如何阅读传入的SMS。 Do note that certain functions like abort broadcast, delete sms will not be working from Android 4.4 onwards unless the user chooses your app as the default messaging app. 请注意,除非用户选择您的应用程序作为默认消息传递应用程序,否则某些功能(如中止广播,删除短信)将无法从Android 4.4开始使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM