6

I have an app that opens an Alert Dialog when a bluetooth device is connected / disconnected. The alert dialog is triggered by a BroadcastReceiver on connect of a Bluetooth device.

I want to open an alert dialog such that if I open my app (app A) > long press home > go to a different app (app B), bluetooth device is connected -> my alert from app A will be displayed on top of app B.

What's happening now is that I can only see the dialog if I go back to app A

My current code:

    final AlertDialog.Builder dialog = new AlertDialog.Builder(activity,
            AlertDialog.THEME_DEVICE_DEFAULT_DARK);

    ... some setting here

    final AlertDialog alert = dialog.create();
    alert.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
    alert.show();
Adarsh M Pallickal
  • 813
  • 3
  • 16
  • 37
kiirohana
  • 260
  • 1
  • 3
  • 11

2 Answers2

8

this may help you...

    @Override
    public void onReceive(Context context, Intent intent) {
        final WindowManager manager = (WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
        layoutParams.gravity = Gravity.CENTER;
        layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.alpha = 1.0f;
        layoutParams.packageName = context.getPackageName();
        layoutParams.buttonBrightness = 1f;
        layoutParams.windowAnimations = android.R.style.Animation_Dialog;

        final View view = View.inflate(context.getApplicationContext(),R.layout.test_layout, null);
        Button yesButton = (Button) view.findViewById(R.id.yesButton);
        Button noButton = (Button) view.findViewById(R.id.noButton);
        yesButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                manager.removeView(view);
            }
        });
        noButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                manager.removeView(view);
            }
        });
        manager.addView(view, layoutParams);
    }
}
Gopal Gopi
  • 11,101
  • 1
  • 30
  • 43
0

You will need SYSTEM_ALERT_WINDOW permission in your Manifest in order to achieve that.

Lowkey
  • 836
  • 5
  • 12