Sam
Sam

Reputation: 105

Pass SMS from BroadcastReceiver to Activity

I am creating an SMS app. I can send messages fine, however I cannot get it to receive. I have successfully implemented the functionality to allow the app to be selected as the default SMS application on the device.

The problem I have is that I cannot pass the SMS from the BroadcastReceiver to the Activity that displays messages. I am aware of the ability to use intent.putExtra() for the message and then startActivity(), but what happens if that activity has already been started when the message is received? I do not want to restart the activity every time a new message is received.

Upvotes: 2

Views: 1179

Answers (1)

user213493
user213493

Reputation: 892

There are few ways to skin that cat, one way is to have a receiver inside the Activity something like this

    void onResume(){
        super.onResume();
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.provider.Telephony.SMS_RECEIVED");
        registerReceiver(mSmsReceiver, filter);
    }

    void onPause(){
        super.onPause();
        unregisterReceiver(mSmsReceiver);
    }

    private BroadcastReceiver mSmsReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
       //Do you stufff
    }
};

Upvotes: 3

Related Questions