Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using context inside class extending BroadcastReceiver

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!

like image 792
Randomly Named User Avatar asked Jul 07 '26 01:07

Randomly Named User


2 Answers

Hm, you already have your context - it's onReceive()'s parameter. You don't need to use getApplicationContext().

EDIT: You can't use context in switch case, because context is defined in Receiver class and you are trying to use it in onClickListener class. I suggest this:

  public class %YOUR_RECEIVER_CLASS% {
       private Context context;
       public onReceive(Context context, ...) {
            this.context = context;
       }
  }

NOW you can use context everywhere

like image 128
Semyon Danilov Avatar answered Jul 09 '26 17:07

Semyon Danilov


You are trying to start an alert dialog from a broadcast receiver, which is not allowed. Check this out:

show an alert dialog in broadcast receiver after a system reboot

like image 33
Pradeep Avatar answered Jul 09 '26 16:07

Pradeep



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!