0

I am making an app where it has this alarm features. What i want is that when the alarm fires, a dialog shows up (in or outside the application). I am new to android. I tried reading some sources about it and i am very much confused.

btw, im already done on how to set up the alarm.

I dont have a problem setting up the alarms and etc. my problem is making a dialog when the alarm fires.

Can you help me?

Razgriz
  • 7,179
  • 17
  • 78
  • 150
Ruby
  • 43
  • 1
  • 2
  • 10
  • 2
    See http://stackoverflow.com/questions/4459058/alarm-manager-example – scrayne Nov 07 '15 at 05:56
  • i already know how to set up the alarm, the broadcastreceiver and etc. what my problem is that i cant show a dialog when the alarm fires. – Ruby Nov 07 '15 at 06:00

4 Answers4

0

When your alarm fire than call activity you have to make transparent activity, and make dialog layout what you want. You can use AlarmManager and Broadcast Receiver for create Alarm.

Mohit Suthar
  • 8,725
  • 10
  • 37
  • 67
  • how to do that transparent activity and dialog?. i can create alarm . – Ruby Nov 07 '15 at 07:05
  • try to use this style http://stackoverflow.com/questions/2176922/how-to-create-transparent-activity-in-android and create dialog layout in activity – Mohit Suthar Nov 07 '15 at 07:07
0

Replace the Toast in the following code with a Dialog (http://developer.android.com/guide/topics/ui/dialogs.html)

public void onReceive(Context context, Intent intent) 
{   
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
    wl.acquire();

    // Put here YOUR code.
    Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For example

    wl.release();
}
scrayne
  • 701
  • 7
  • 18
0

Here is code for building a dialog:

AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
        builder1.setMessage("Write your message here.");
        builder1.setCancelable(true);
        builder1.setPositiveButton("Yes",
                new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
            }
        });
        builder1.setNegativeButton("No",
                new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
            }
        });

        AlertDialog alert11 = builder1.create();
        alert11.show();
scrayne
  • 701
  • 7
  • 18
0

I'm not sure if this is possible. Where are you trying to make/show your Dialog? Are you doing this on the BroadcastReceiver?

If that's the case then it probably won't work and you might want to change from using Dialogs to Notifications. This happens because Dialogs need an Activity.

This is the error I get when I try to shoot/create a Dialog from the BroadcastReceiver in the onReceive() method:

java.lang.RuntimeException: Unable to start receiver com.voxnet.autohop.helpers.AlarmReceiver: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?

João Martins
  • 706
  • 1
  • 8
  • 20