android - Make an alert dialog popup from a class that is not an activity? -


this question has answer here:

i creating app requires alert dialog pop class not activity. tried creating 1 of alertdialog builder , passed context in, app stops working whenever try show alert. when used same method create alertdialog in main activity worked, if that, not pop @ time want to. there anyway can make alertdialog class not activity? or can access activity reference class can use create alert?

please note class created result of service, , not using constructer directly main activity, cannot pass main activity reference through constructor. have put notification in place of alert, rather have alert notification.

there reason why notifications supported method of notifying user of servce. if want know more why shouldn't use them check out this answer. if doesn't make rethink using dialog in situation here's how show 1 service.

first need build dialog:

alertdialog dialog = new alertdialog.builder(this)                     .settitle("title")                     .setmessage("message")                     .create();  alertdialog.getwindow().settype(windowmanager.layoutparams.type_system_alert); alertdialog.show(); 

then add permission:

<uses-permission android:name="android.permission.system_alert_window" />