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.
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).