0

In an android application I have to show an internal notification that comes over the top of every screen in my app . I have used WindowManager. But it doesn't seem to work as expected. Sometimes I see the first notification but the second is not shown though when debugged ,step by step execution of the following method is visible. Can't figure out why is this happening. Help!!

    public void addNotification(String message, Bundle bundle) {
    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);

    lp.gravity = Gravity.END | Gravity.BOTTOM;

    lp.x = getResources().getInteger(R.integer.notification_side_margin);
    lp.y = getResources().getInteger(R.integer.notification_side_margin);

    // Initialise container view first in which every notification is to be
    // added.
    if (notificationConatinerView == null) {

        notificationConatinerView = getLayoutInflater().inflate(
                R.layout.custom_notification_main_layout, null);

        notificationContainer = (LinearLayout) notificationConatinerView
                .findViewById(R.id.notification_container);
        notificationContainer.setPadding(
                (int) getResources().getDimension(R.dimen.mini_margin),
                (int) getResources().getDimension(R.dimen.mini_margin),
                (int) getResources().getDimension(R.dimen.mini_margin),
                (int) getResources().getDimension(R.dimen.mini_margin));
        windowManager.addView(notificationConatinerView, lp);

        // Add that container to the window manager
        windowManager.updateViewLayout(notificationConatinerView, lp);
    }

    final View view = getLayoutInflater().inflate(
            R.layout.notification_custom, null);
    view.setTag(bundle);
    view.setId(notificationContainer.getChildCount());
    view.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Bundle bundle = (Bundle) v.getTag();
            setClickForNotifications(bundle);
        }
    });
    final ImageView crossImage = (ImageView) view
            .findViewById(R.id.notification_cross);
    crossImage.setTag(view);
    TextView messageTextView = (TextView) view
            .findViewById(R.id.message_text);
    messageTextView.setText(message);

    crossImage.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (notificationContainer.getChildCount() == 1) {
                showNotificationExitingView(v);
            } else {
                if (!isFinishing())
                    notificationContainer.removeView((View) crossImage
                            .getTag());

            }
        }
    });
    LinearLayout.LayoutParams childLP = new LinearLayout.LayoutParams(
            (int) getResources().getDimension(
                    R.dimen.notification_view_width),
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    if (notificationContainer.getChildCount() == getResources().getInteger(
            R.integer.no_of_notifications)) {
        notificationContainer.removeViewAt(notificationContainer
                .getChildCount() - 1);
    }

    notificationContainer.addView(view, 0, childLP);
    notificationContainer.updateViewLayout(view, childLP);
    Handler mHandler = new Handler();
    mHandler.postDelayed(new Runnable() {

        @Override
        public void run() {
            if (notificationContainer.getChildCount() == 1) {
                showNotificationExitingView(view);
            } else {
                if (!isFinishing())
                    notificationContainer.removeView(view);

            }
        }
    }, 10000);

    try {
        windowManager.updateViewLayout(notificationConatinerView, lp);
    } catch (Exception e) {
        windowManager.addView(notificationConatinerView, lp);
        windowManager.updateViewLayout(notificationConatinerView, lp);
    }

    if (notificationContainer.getChildCount() == 1) {
        showNotificationEnteringAnimation();
    }

}

Thanks!!

user2781627
  • 455
  • 5
  • 17
  • https://stackoverflow.com/questions/7569937/unable-to-add-window-android-view-viewrootw44da9bc0-permission-denied-for-t#answer-34061521 Please follow this link you need a permission just. – adil khan Dec 01 '18 at 12:04

1 Answers1

-1

use

windowManager = (WindowManager) getApplicationContext().getSystemService(WINDOW_SERVICE);

instead of

windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
Hana Bzh
  • 2,212
  • 3
  • 18
  • 38