How to make a Facebook Messenger-like notification like this in Android

后端 未结 1 414
孤街浪徒
孤街浪徒 2020-12-07 07:51

I want to implement notifications like the one in the following image.

Notification appears any time. I think it\'s of course a background service waiting for new m

相关标签:
1条回答
  • 2020-12-07 08:29

    Check out this link http://www.piwai.info/chatheads-basics. He provides information about how to add them on your screen.

    The trick is to add a View to the WindowManager like following code

    private WindowManager windowManager;
    private ImageView chatHead;
    
    public void addView()
    {
      windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    
      chatHead = new ImageView(this);
      chatHead.setImageResource(R.drawable.android_head);
    
      WindowManager.LayoutParams params = new WindowManager.LayoutParams(
        WindowManager.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.TYPE_PHONE,
        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
        PixelFormat.TRANSLUCENT);
    
      params.gravity = Gravity.TOP | Gravity.LEFT;
      params.x = 0;
      params.y = 100;
    
      windowManager.addView(chatHead, params);
    }
    

    Don't forget to add the permission <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

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