I would like to check every so often if exists a new version of my app, and if it´s, show a message to user. I use Firebase, connecting and comparing version from remote config with current version of app. This isn´t the problem, my problem is how to show the dialog at any time, in any activity.
I have a BaseActivity, where I have methods to connect with firebase and to show the message when it answers. Furthermore, I have a method that executes every hour this update checking:
private void checkUpdate() {
handlerCheckUpdate.postDelayed(() -> {
getConfigFromFirebase(this);
checkUpdate();
}, 3600000);
}
And finally I have Activity1 and Activity2 that extends of BaseActivity. In my Activity1 I start the recursive checkUpdate method.
The problem is that, if user is in Activity2 currently, when the message shows, it do it in Activity1 and not in Activity2. What is the best solution to do this?.
Thank you very much!
Finally, I´ve found a solution. I use AlarmManager and BroadcastReceiver, and I´ve removed the recursive method checkUpdate and set an alarm that repeat every hour, and does the action getConfigFromFirebase(this):
Create broadcast receiver with the action of alarm:
public class CheckUpdateAppAlarmReceiver extends BroadcastReceiver {
public static CheckUpdateReceiverListener checkUpdateReceiverListener;
public static Boolean dialogShown = false;
@Override
public void onReceive(Context context, Intent intent) {
//Alarm!!
if (checkUpdateReceiverListener != null) {
checkUpdateReceiverListener.onCheckUpdateListenerChanged();
}
}
public interface CheckUpdateReceiverListener {
void onCheckUpdateListenerChanged();
}
}
In BaseActivity, add create and cancel alarm methods:
public void createAlarmCheckUpdate() {
try {
long firstAlarmTime = new Date().getTime();
Intent intentAlarmCheckUpdate = new Intent(this, CheckUpdateAppAlarmReceiver.class);
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 2000,
intentAlarmCheckUpdate,
PendingIntent.FLAG_UPDATE_CURRENT);
alarmMgr.setInexactRepeating(AlarmManager.RTC, firstAlarmTime,
AlarmManager.INTERVAL_HOUR, alarmIntent);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void cancelAlarmCheckUpdate(Context context) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, CheckUpdateAppAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 2000, intent, PendingIntent.FLAG_NO_CREATE);
if (pendingIntent != null) {
alarmManager.cancel(pendingIntent);
}
}
Finally, implements the broadcastReceiver in BaseActivity, and override its method
public class BaseActivity extends AppCompatActivity implements CheckUpdateAppAlarmReceiver.CheckUpdateReceiverListener {
...
@Override
protected void onResume() {
super.onResume();
CheckUpdateAppAlarmReceiver.checkUpdateReceiverListener = this;
}
...
@Override
public void onCheckUpdateListenerChanged() {
if (this instanceof Activity1 || this instanceof Activity2) {
getConfigFromFirebase(this);
}
}
Use create and cancel alarm methods where necessary.