如何在Android中像这样发出类似Facebook

如何在Android中像这样发出类似Facebook

本文介绍了如何在Android中像这样发出类似Facebook Messenger的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现下图所示的通知.

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

通知随时出现.我认为这当然是一个后台服务,等待服务器发出新消息,然后显示此消息.我认为这是使用此自定义UI实施为对话框的活动.我对么?它是服务中的普通startActivity方法吗?以及如何制作过渡动画以使其在显示时从左到右缓慢缩放?

Notification appears any time. I think it's of course a background service waiting for new messages from the server then shows this. What I think this is an activity implemented as dialog with this custom UI. Am I correct? And is it a normal startActivity method from the service? And how do I do the transition animation to make it appears slowly from left to right with zooming when show up?

推荐答案

查看此链接 http://www.piwai.info/chatheads-basics .他提供了有关如何在屏幕上添加它们的信息.

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

技巧是像下面的代码一样向WindowManager添加View

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);
}

别忘了添加权限<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

这篇关于如何在Android中像这样发出类似Facebook Messenger的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 11:09