0

I have one MainActivity.java and one SmsReceiver.java I want to start the MainActivity when any message is recieved in phone.

MainActivity.java -->

public class MainActivity extends AppCompatActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String sms = getIntent().getStringExtra("message");
    }
}

SmsReceiver.java -->

public class SmsReceiver extends BroadcastReceiver{

    String str = "";

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        SmsMessage msgs = null;
        if (bundle != null) {
            Object[] pdus = (Object[]) bundle.get("pdus");
            for (int i = 0; i < pdus.length; i++) {
                msgs = SmsMessage.createFromPdu((byte[]) pdus[i]);
                str += msgs.getMessageBody().toString();
            }
        sendToMain(context,intent);
        }
    }

    public void sendToMain(Context context,Intent intent){
        Toast.makeText(context,"Message received", Toast.LENGTH_SHORT).show();
        Intent i = new Intent(context,MainActivity.class);
        i.putExtra("message",str);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|   Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(i);
    }
}

and AndroidManifest.xml contains -->

    <receiver android:name=".SmsReceiver"
                android:exported="true">
        <intent-filter>
            <action android:name=
                "android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>

and permissions are also given -->

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

But when any new Message is received, nothing happens...

Please suggest if any thing is wrong here.

MNOPM
  • 23
  • 5
  • try adding android:exported="true" in your reciever element in manifest – Shubham Shukla Jan 22 '17 at 00:38
  • Make sure you actually have the `RECEIVE_SMS` permission. http://stackoverflow.com/questions/32635704/android-permission-doesnt-work-even-if-i-have-declared-it Also note that the keys for your extra don't match - `"message"`, `"Message"`. – Mike M. Jan 22 '17 at 00:54
  • After adding exported="true" it is working but only when the app is minimised. When the app is closed it is not reading the messages. – MNOPM Jan 22 '17 at 01:46
  • Also the string which i am sending from SmsReceiver.java is not going to the MainActivity.java ---> – MNOPM Jan 22 '17 at 01:48
  • The Receiver was already exported by default. I don't really think that explicitly declaring it exported actually fixed anything. You must've changed something else. As for the `String` extra not making it to `MainActivity`, remember I noted that the keys you're using to put and get that extra don't match - `"message"`, `"Message"`. They must be exact matches, and are case-sensitive. – Mike M. Jan 22 '17 at 02:06
  • Yes. I have fixed that..but still the string didn't worked – MNOPM Jan 22 '17 at 02:41
  • If however you're "closing" your app is actually forcibly stopping it, then the app is being put back into the _stopped_ state, and your Receiver won't work again until the app is next run. As for the extra, are you certain you're getting it correctly in the Receiver? Are you checking its value there before you send it to the `Activity`? – Mike M. Jan 22 '17 at 02:44
  • is there any way to solve it...? – MNOPM Jan 22 '17 at 02:47

0 Answers0