哪些 API 用于绘制其他应用程序(如 Facebook 的聊天头)? [英] What APIs are used to draw over other apps (like Facebook's Chat Heads)?

查看:38
本文介绍了哪些 API 用于绘制其他应用程序(如 Facebook 的聊天头)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Facebook 如何在 Android 上创建聊天头?在所有其他视图之上创建浮动视图的 API 是什么?

How does Facebook create the Chat Heads on Android? What is the API to create the floating views on top of all other views?

推荐答案

这个:

允许应用程序使用类型打开窗口TYPE_SYSTEM_ALERT,显示在所有其他应用程序之上.很少有应用程序应该使用此权限;这些窗口旨在用于与用户进行系统级交互.

Allows an application to open windows using the type TYPE_SYSTEM_ALERT, shown on top of all other applications. Very few applications should use this permission; these windows are intended for system-level interaction with the user.

常量值:android.permission.SYSTEM_ALERT_WINDOW"

Constant Value: "android.permission.SYSTEM_ALERT_WINDOW"

//完整代码这里:

public class ChatHeadService extends Service {

  private WindowManager windowManager;
  private ImageView chatHead;

  @Override public IBinder onBind(Intent intent) {
    // Not used
    return null;
  }

  @Override public void onCreate() {
    super.onCreate();

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

  @Override
  public void onDestroy() {
    super.onDestroy();
    if (chatHead != null) windowManager.removeView(chatHead);
  }
}

不要忘记以某种方式启动服务:

Don't forget to start the service somehow:

startService(new Intent(context, ChatHeadService.class));

.. 并将此服务添加到您的清单中.

.. And add this service to your Manifest.

这篇关于哪些 API 用于绘制其他应用程序(如 Facebook 的聊天头)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆