0

I need to display the dialog on every 5 minutes in the Activity1. So sending broadcast every 5 mins from Thread T and register the receiver in Activity1. So every 5 mins Activity1 receive the broadcast and shows the dialog. It's fine. But if i goes to Activity2 from Activity 1 and after 5 mins if i come back to Activity 1, the dialog is not getting display. Because broadcast register to Activity1 not with the Activity2. When Thread T send the broadcast, Activity2 will not receive that, because it's not register with that. Is that any other possible solutions are there to solve this problem.

krish
  • 213
  • 1
  • 10

2 Answers2

1

Probably you don't need to use any separated thread for pushing dialog in every 5 minutes. In Android you have several mechanism for scheduling tasks. Maybe this will help : Scheduling recurring task in Android

Community
  • 1
  • 1
0

If you only want to display the dialog when Activity1 is the current activity, register/unregister your receiver in onCreate() and onStop() respectively:

@Override
public void onCreate(Bundle savedInstanceState) {
    ...
    yourReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            //Your implementation
        }
    ...
    registerReceiver(yourReceiver, new IntentFilter(YOUR_ACTION_STRING);
}
@Override
public void onStop() {
    ...
    unregisterReceiver(yourReceiver);
    ...
}

If you want your receiver to always be ready for a signal, declare it in your AndroidManifest, and create a custom class for it:

<receiver android:name="your.package.name.YOUR_RECEIVER_CLASSNAME" >
    <intent-filter>
        <action android:name="your.action.string" />
    </intent-filter>
</receiver>
nstosic
  • 2,584
  • 1
  • 17
  • 21