3

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!

Randomly Named User
  • 1,889
  • 7
  • 27
  • 47

3 Answers3

3

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

Semyon Danilov
  • 1,753
  • 1
  • 17
  • 37
  • he can use context in onClickListener because his contex is 'final' – Dr Glass Jul 23 '13 at 07:58
  • LOL no. Is it final or not, but it's only a parameter of method, but not a member of class. – Semyon Danilov Jul 23 '13 at 07:59
  • there is no need for context to be a member o class dude... onClickListener just need to know that Context will not change, so he use 'final' it not looks well but it is correct – Dr Glass Jul 23 '13 at 08:15
  • Do You know about scopes? Method onClick in his unnamed child of onClickListener do not have access to the onReceive's scope. SO, he can't access context that is parameter of onReceive. – Semyon Danilov Jul 23 '13 at 08:18
3

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

Community
  • 1
  • 1
Pradeep
  • 126
  • 3
0

use getBaseContext() instead of getApplicationContext()

example BroadcastReceiver::

 @Override
     public void onCreate() 
     {        
         Toast.makeText(this, "SmsSenderService()", Toast.LENGTH_LONG).show();

         sendBroadcastReceiver = new BroadcastReceiver()
         {
                public void onReceive(Context arg0, Intent intent)
               {
                 switch (getResultCode())
                 {
                  case Activity.RESULT_OK:                  

                    Toast.makeText(getBaseContext(), "SMS Sent", Toast.LENGTH_SHORT).show();        

                    break;

                }
            }
        };
Mohammod Hossain
  • 4,134
  • 2
  • 26
  • 37
  • Oh dear god, you are trying to fire getBaseContext method IN THE ONCLICKLISTENER. OnClickListener DO NOT HAVE SUCH METHOD. YOU NEED to push context to your onClickListener OR make Context a field in your Receiver AS I SAID. How many times do I need to repeat that? Do not listen to whose who suggest using getContext or getBaseContext or something else, because they know nothin – Semyon Danilov Jul 23 '13 at 08:08