0

I'm developing an app that uses SMS to receive and store data into database. Also, I have an activity that shows the result of a query from database. Now I want, if the this special activity is open and a new message arrives some code in sms-receiver restart this activity and if this activity was not open then nothing would happen.

Alireza
  • 31
  • 7

1 Answers1

1

You could register a BroadcastReceiver in your activity which performs the refresh of your activity:

public class YourActivity extends Activity {

    private final BroadcastReceiver myReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
           // refresh UI or finish activity and start again
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        IntentFilter filter = new IntentFilter("ACTION_REFRESH_UI");
        registerReceiver(myReceiver, filter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        unregisterReceiver(myReceiver);
    }
}

Send the broadcast whenever a new message has been stored into the database:

Intent intent = new Intent();
intent.setAction("ACTION_REFRESH_UI");
sendBroadcast(intent);

You could also add a content observer for the database that gets notified about changes and which send the broadcast (see this example for more).

Trinimon
  • 13,839
  • 9
  • 44
  • 60
  • Just in case it really helped I'd appreciate it very much if you up vote and accept my answer ;) – Trinimon Mar 11 '15 at 11:01
  • I like to do so but my reputation is low and I cannot vote. Sorry :( .As soon as I get enough I will do that. – Alireza Mar 11 '15 at 13:11
  • Oh dear, you can up vote starting from 15 points on - didn't new that. Can't you accept the answer? In order to avoid down votes on your questions you should add at least some code that demonstrates that you tried something out. Otherwise some people tend to down vote your question. – Trinimon Mar 11 '15 at 15:10