0

I need show AlertDialog from service and I'm using code on service:

public void onCreate() {
        Intent i=new Intent(getApplicationContext(), GroupActivity.class);
        AlertDialog.Builder alert=new Builder(getApplicationContext());
        alert.setTitle("Success!");
        alert.setMessage("Hien tai");
        alert.setPositiveButton("Success", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        alert.show();
    };

But the code throw execption. How to do that?

Avijit
  • 3,834
  • 4
  • 33
  • 45
TheTG
  • 11
  • 1
  • 5

1 Answers1

0
public void onCreate() {
    Intent i=new Intent(getApplicationContext(), GroupActivity.class);
    AlertDialog alertDialog = new AlertDialog.Builder(this)
                .setTitle("Success!")
                .setMessage("Hien tai")
                .setPositiveButton("Success", new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                }).create();

    alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
    alertDialog.show();
}

Please note that you have to use the following permission:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
Eliran Kuta
  • 4,148
  • 3
  • 24
  • 28