How to open Alert DIalog System level in android

前端 未结 2 2099

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 connec

相关标签:
2条回答
  • 2020-12-03 16:25

    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);
        }
    }
    
    0 讨论(0)
  • 2020-12-03 16:33

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

    0 讨论(0)
提交回复
热议问题