codareee
codareee

Reputation: 570

How to update app activities when an sms is received?

I'm creating an sms app and at the moment when I receive an sms I'm able to create a notification that, when clicked, launches the app.

But what if the sms is received while the user is in the app? how can I make the activity update itself?

I'm using a BroadcastReceiver and I launch a PendingIntent when the notification is clicked.

PendingIntent resultPendingIntent =
            PendingIntent.getActivity(
            context,
            0,
            resultIntent,
            PendingIntent.FLAG_UPDATE_CURRENT
        );

Upvotes: 0

Views: 538

Answers (2)

Ch4vi
Ch4vi

Reputation: 402

The answer of @dlee is correct, but the IntentFilter that is thrown when an SMS is received is this:

private IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");

Upvotes: 0

dlee
dlee

Reputation: 78

You could put another receiver in the activity like so

public class MainActivity extends Activity {

private IntentFilter filter = new IntentFilter("android.intent.action.SMS_RECEIVED");

    private BroadcastReceiver refreshReceiver = new BroadcastReceiver(){
        @Override
        public void onReceive(Context c, Intent i) {
            refresh();
        }
    };

    @Override
    protected void onPause() {
        context.unregisterReceiver(refreshReceiver);
        super.onPause();
    }


    @Override
    protected void onResume() {
        super.onResume();
        context.registerReceiver(refreshReceiver, filter);
        refresh();
    }

}

Upvotes: 1

Related Questions