Search code examples
androidsynchronizationandroid-alertdialogalarmmanager

Synchronize Dialog Boxes


I have a broadcast receiver that on boot of the phone runs a pending intent. The pending intent opens a dialog box to alert the user.

I noticed that if there are several dialog boxes who's time has passed, they all fire off at once. Which leads to only the first dialog box displayed. When you click on the 'dismiss' button the it dismisses all the dialog boxes.

Is there a way to for each box to be displayed one at a time and not jam up like this?

I read up on using 'synchronize' but it seems to be only used in networking. Can you let me know what would be the best approach to resolving this issue?

My code:

package com.google.android.gcm.demo.app.Alerts;

import java.io.IOException;

import com.google.android.gcm.demo.app.DemoActivity;
import com.google.android.gcm.demo.app.PreferenceConnector;
import com.google.android.gcm.demo.app.R;
import com.google.android.gcm.demo.app.TabBarExample;
import com.google.android.gcm.demo.app.sqllite.DatabaseSqlite;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Vibrator;

public class AlertDialogActivity extends Activity {

    MediaPlayer player = new MediaPlayer();
    Bundle bundle;
    Vibrator vibrate;
    DatabaseSqlite entry = new DatabaseSqlite(AlertDialogActivity.this);
    int vibrateState;
    int soundState;
    AlertDialog.Builder builder;
    Intent intent;
    int idInteger;
    int alertDismissed = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        bundle = getIntent().getExtras();
        String name = bundle.getString("name");
        String id = bundle.getString("id");

        idInteger = Integer.parseInt(id);

        intent = new Intent(AlertDialogActivity.this, TabBarExample.class);
        sound();
        vibrate();

        entry.open();
        entry.updateAlertDismissedState(idInteger, alertDismissed);
        String text = entry.getMinutesForOneShot(idInteger);
        entry.close();

        String textForDisplay = "In " + text + " Minutes";

        builder = new AlertDialog.Builder(this);
        builder.setMessage(textForDisplay)
                .setTitle(name)
                .setCancelable(false)
                .setNegativeButton("Dismiss",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {

                                soundState = PreferenceConnector.readInteger(
                                        AlertDialogActivity.this,
                                        PreferenceConnector.SOUND_ON_OFF, 0);
                                if (soundState == 0) {

                                    player.pause();
                                    player.release();

                                }

                                vibrateState = PreferenceConnector.readInteger(
                                        AlertDialogActivity.this,
                                        PreferenceConnector.VIBRATE_ON_OFF, 0);

                                if (vibrateState == 0) {

                                    vibrate.cancel();
                                }

                                startActivity(intent);

                            }
                        });

        AlertDialog alert = builder.create();
        alert.show();
    }

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

    }

    private void sound() {

        soundState = PreferenceConnector.readInteger(AlertDialogActivity.this,
                PreferenceConnector.SOUND_ON_OFF, 0);
        if (soundState == 0) {

            // play sound

            AssetFileDescriptor afd = this.getResources().openRawResourceFd(
                    R.raw.alarm_clock);
            try {
                player.setDataSource(afd.getFileDescriptor(),
                        afd.getStartOffset(), afd.getLength());
                afd.close();
                player.setAudioStreamType(AudioManager.STREAM_ALARM);
                player.setLooping(true);
                player.prepare();
                player.start();

            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        } else {
        }

    }

    private void vibrate() {

        vibrateState = PreferenceConnector
                .readInteger(AlertDialogActivity.this,
                        PreferenceConnector.VIBRATE_ON_OFF, 0);

        if (vibrateState == 0) {
            // Get instance of Vibrator from current Context
            vibrate = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

            // Start immediately
            // Vibrate for 200 milliseconds
            // Sleep for 500 milliseconds
            long[] pattern = { 0, 200, 500 };

            // The "0" means to repeat the pattern starting at the beginning
            // CUIDADO: If you start at the wrong index (e.g., 1) then your
            // pattern will be off --
            // You will vibrate for your pause times and pause for your vibrate
            // times !
            vibrate.vibrate(pattern, 0);
        } else {
        }

    }

}

Solution

  • In order to resolve this issue, I compared the current date to when the alert fired off. If the alert passed I, I put it in an arrary list. Then I used a intent to send the list to another activity. In that activity, I used a another for loop to process the dialog boxes one at a time.