android - Using context inside class extending BroadcastReceiver -
when alarm in android goes off, want create alertdialog. also, want create notification, depending on option user clicks in radio buttons on dialog. problem arises when try use context
or getapplicationcontext()
.
this code:
public void onreceive(final context context, intent intent) { final charsequence[] items = {" i'm taking dose now! "," remind again in ten minutes. "," ignore 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 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 switch case, exact error get:
the method getapplicationcontext() undefined type new dialoginterface.onclicklistener(){}
any suggestions of how go forward?
edit:
till now, these i've tried:
public void onreceive(final context context, intent intent) { ctx = context; final charsequence[] items = {" i'm taking dose now! "," remind again in ten minutes. "," ignore 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 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()
, checked. doesn't work.
also, when comment out problematic areas , run verify dialog box turns up, exception:
07-23 13:26:21.316: e/androidruntime(1756): java.lang.runtimeexception: unable start receiver com.dosemanager.ui.dosealarmreceiever: android.view.windowmanager$badtokenexception: unable add window -- token null not application
please help!
hm, have context - it's onreceive()
's parameter. don't need use getapplicationcontext()
.
edit: can't use context in switch case, because context defined in receiver class , trying use in onclicklistener
class. suggest this:
public class %your_receiver_class% { private context context; public onreceive(context context, ...) { this.context = context; } }
now can use context everywhere
Comments
Post a Comment