Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open Alert DIalog System level in android

Tags:

android

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();
like image 665
kiirohana Avatar asked Jan 17 '14 07:01

kiirohana


1 Answers

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);
    }
}
like image 79
Gopal Gopi Avatar answered Oct 04 '22 02:10

Gopal Gopi