I want to display an alert dialog from inside an intent service.
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
This throws the following exception
Unable to add window — token null is not for an application
I have tried IntentService.this and getApplicationContext() as well. Between i dont want to do it using an activity. I just want to show a simple alert dialog with a little text.
Only if you set your alertDialog type to
TYPE_SYSTEM_ALERT
it will be displayed from an intent service.add these after your code:
But, it have a cost:
Need
Activity
for displayAlertDialog
, because we can't displayDialog
from anyService
Solution.
Create
Activity
as Dialog Theme and start thatActivity
fromService
.Just need to register you
Activity
inmenifest.xml
like as belowor
MyDialog.java
Please visit
https://github.com/selmantayyar/Custom-SMS-Popup
it will surly help you!!
or what you can do is register anActivity in menifest.xml as follows
or
and work around it
The problem is because of Context. You can't use this as Context in Intent Service. So need to pass a Context variable of your Intent Service to your Alert Dialog. Like,