Search code examples
androidsmsbroadcastreceiver

BroadcastReceiver automatically reads SMS in Android


I used BroadcastReceiver to show received sms's content in Toast. It's working fine. Shows Content in Toast. But it also Shows the message in Dialog box like thing. enter image description here

And also the message goes to read state. Any way to avoid this. My BroadcastReceiver is

public class Receiver extends BroadcastReceiver {

    public static final String action = "android.provider.Telephony.SMS_RECEIVED";

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        if (intent.getAction().equals(action)){
            Bundle bundle = intent.getExtras();
            if (bundle != null){
                Object[] pdus = (Object[]) bundle.get("pdus");
                SmsMessage[] messages = new SmsMessage[pdus.length];
                for (int i = 0; i < pdus.length; i++){
                    messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                }
                for (SmsMessage message : messages){

                    String strMessageFrom = message.getDisplayOriginatingAddress();
                    String strMessageBody = message.getDisplayMessageBody();

                    Toast.makeText(context, "From : " +strMessageFrom+"\nBody : "+strMessageBody, Toast.LENGTH_LONG).show();
                }
            }
        }
    }

AndroidManifest.xml is

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.RECEIVE_SMS"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >

        <intent-filter>>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver
        android:name="com.realtech.sms_db.Receiver"
        android:enabled="true"
        android:exported="true"
        android:permission="android.permission.BROADCAST_SMS">
        <intent-filter
            android:priority="1000">
            <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
        </intent-filter>
    </receiver>
</application>


Solution

  • As I understood, the issue is 'why it is going in read state'?. There is nothing wrong with the implementation. The incoming message is getting marked as read is not because of the coed, it is because of device notification settings "auto preview". That is why you are able to see the window. To disable "auto preview" follow the steps :-

    Step 1: Go to messages folder Step 2: Select Menu (3 dots). Step 3: Select "Settings" Step 4: Scroll down to " Notification Settings" Step 5: Select " Notification Settings" Step 6: Uncheck the box near to "Preview message".

    Hope it helps you.