When an alarm in android goes off, I want to create an AlertDialog. Also, I want to create a notification, depending on which option the user clicks in the radio buttons on the dialog.
The problem arises when I try to use context
or getApplicationContext()
.
This is my code:
public void onReceive(final Context context, Intent intent)
{
final CharSequence[] items = {" I'm taking the dose now! "," Remind again in ten minutes. "," Ignore for now. "};
String doseName = intent.getStringExtra("doseName");
Toast.makeText(context, "Take medicine: " + doseName, Toast.LENGTH_LONG).show();
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("It's time for your medicine.");
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
switch(item)
{
case 0:
Toast.makeText(context, "Good.", Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(context, "Reminder set in ten minutes.", Toast.LENGTH_SHORT).show();
break;
case 2:
Intent service1 = new Intent(context, DoseAlarmService.class);
service1.putExtra("doseName", doseName);
context.startService(service1);
break;
}
}
});
levelDialog = builder.create();
levelDialog.show();
}
I've tried using getApplicationContext
instead of context
inside the switch case, but this is the exact error i get:
The method getApplicationContext() is undefined for the type new DialogInterface.OnClickListener(){}
Any suggestions of how to go forward?
EDIT:
Till now, these are what i've tried:
public void onReceive(final Context context, Intent intent)
{
ctx = context;
final CharSequence[] items = {" I'm taking the dose now! "," Remind again in ten minutes. "," Ignore for now. "};
String doseName = intent.getStringExtra("doseName");
Toast.makeText(ctx, "Take medicine: " + doseName, Toast.LENGTH_LONG).show();
AlertDialog.Builder builder = new AlertDialog.Builder(ctx.getApplicationContext());
builder.setTitle("It's time for your medicine.");
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
switch(item)
{
case 0:
Toast.makeText(ctx, "Good.", Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(ctx, "Reminder set in ten minutes.", Toast.LENGTH_SHORT).show();
break;
case 2:
Intent service1 = new Intent(ctx.getApplicationContext(), DoseAlarmService.class);
service1.putExtra("doseName", doseName);
ctx.startService(service1);
break;
}
}
});
levelDialog = builder.create();
levelDialog.show();
}
Also, instead of using ctx
, i'v directly used context.getApplicationContext()
and checked. It doesn't work.
Also, when I comment out all the problematic areas and just run to verify that the dialog box turns up, I get this exception:
07-23 13:26:21.316: E/AndroidRuntime(1756): java.lang.RuntimeException: Unable to start receiver com.dosemanager.ui.DoseAlarmReceiever: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
Please help!