2

I have to set reminder in my app. So, Once the reminder time is reached , app has to show one popup window (Even if the app is not running) , just like how WhatsApp shows messages in popup when it is not running

enter image description here

On tapping the button I have to launch my app also. How can I display one popup from background? Is there any samples available? Thanks in advance

dev
  • 1,085
  • 4
  • 19
  • 26
  • This might help you http://stackoverflow.com/questions/25921960/gcm-intentservice-how-to-display-a-pop-up-on-notification-receive – Arshad Feb 29 '16 at 09:54

1 Answers1

2

You can use SYSTEM_ALERT_WINDOW from your BroadcastReceiver to show one dialog window , which will be shown on top of all other apps.

First add the permission

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

in Manifest , then in your onReceiver , Create one AlertDialog as follows

@Override
public void onReceive(final Context context, Intent intent) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context.getApplicationContext());
        LayoutInflater inflater = LayoutInflater.from(context);
        View dialogView = inflater.inflate(R.layout.your_dialog_layout, null);
        builder.setView(dialogView);
        final AlertDialog alert = builder.create();
        alert.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        alert.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        alert.show();
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        Window window = alert.getWindow();
        lp.copyFrom(window.getAttributes());
        //This makes the dialog take up the full width
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);
}
Sarath Kn
  • 2,680
  • 19
  • 24