I knew this question has been asked but I couldn't solve it. I want to show simple AlertDialog on Android's Service. I can show it nicely on MainActivity but I had a problem on Service , Here is my code:
CustomMainActivity.java:
public void popupDialogMain()
{
final Context context = getApplicationContext();
Handler h1 = new Handler(context.getMainLooper());
h1.post(new Runnable() {
@Override
public void run() {
if (mBXmpp)
mBXmppService.popupDialogMain2();
}
});
}
XmppService.java:
public static void popupDialogMain2()
{
AlertDialog.Builder builder = new AlertDialog.Builder(CustomMainActivity.this)
.setMessage("Look at this dialog!")
.setCancelable(true)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//do things
}
});
AlertDialog alert = builder.create();
builder.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alert.show();
}
I get an error on this line: AlertDialog.Builder builder = new AlertDialog.Builder(CustomMainActivity.this)
Android Manifest: I added this permission:
android.permission.SYSTEM_ALERT_WINDOW
And I get this error: not an enclosing class: CustomMainActivity
Any suggestion to solve it?