0

I want to detect when a message receive log or Toast the number and message text.for this I use SMS_RECEIVED broadcast in manifest but does not work.I tried it on lolliphp version emulator and nothing happend. when I debug the broadcast receiver does not work.this is my codes.

manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="clicksource.ir.cameratest">
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.SEND_SMS"/>
    <uses-permission android:name="android.permission.READ_SMS"/>
    <uses-permission android:name="android.permission.RECEIVE_MMS"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name=".SmsReceiver" android:enabled="true">
            <intent-filter android:priority="0">
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
        </receiver>
    </application>

</manifest>


public class SmsReceiver extends BroadcastReceiver {


    @Override
    public void onReceive(Context context, Intent intent) {
      //  if (intent.getAction().equals(SMS_RECEIVED)) {
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                // get sms objects
                Object[] pdus = (Object[]) bundle.get("pdus");
                if (pdus.length == 0) {
                    return;
                }
                // large message might be broken into many
                SmsMessage[] messages = new SmsMessage[pdus.length];
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < pdus.length; i++) {
                    messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                    sb.append(messages[i].getMessageBody());
                }
                String sender = messages[0].getOriginatingAddress();
                String message = sb.toString();
                Toast.makeText(context, message, Toast.LENGTH_SHORT).show();

            }
        }
   // }
}
john
  • 345
  • 1
  • 4
  • 20
  • 1
    You're missing the `RECEIVE_SMS` permission. Also, note that you will need to launch `MainActivity` at least once after installation for your Receiver to work. – Mike M. Jan 28 '19 at 19:20
  • 1
    thank you man ....its work now.it was my bad – john Jan 28 '19 at 19:24

0 Answers0