简体   繁体   中英

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 :(

Basically, I want to read incoming SMS and filter possible spams by deleting the spam SMSes. Since deletion could not be done now, I am trying to just prompt that the newly received SMS is potential spam.

Below is my source snippet for non Android 4.4 versions:

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? I just want to read the incoming SMSes without doing abortBroadcast.

Since 4.4 only default SMS app will receive SMS_DELIVER_ACTION broadcasts. Other apps must now use SMS_RECEIVED_ACTION broadcasts, this is introduced in API level 19 and can not be aborted.

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

Explains how to read incoming SMSes. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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