programing

What APIs are used to draw over other apps (like Facebook's Chat Heads)?

css3 2023. 9. 27. 18:03

What APIs are used to draw over other apps (like Facebook's Chat Heads)?

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

This one:

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.

Constant Value: "android.permission.SYSTEM_ALERT_WINDOW"

//EDIT: The full code here:

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.

As a rule, Android activities are full screen, conceptually dedicated UIs that take all the interaction. There are a few exceptions to this. For a start, there are popup dialogs that don't fill the screen. Another is the Android toast, which is a non-interactive popup - you can't touch it, and if you try it'll go to whatever's underneath.

본인만의 특별한 UI도 가능합니다.보기를 에 직접 추가할 수 있습니다.WindowManager, 유형 플래그 지정채팅 헤드는 TYPE_PHONE을 사용할 것입니다.몇 가지 유사한 유형이 있지만 목적은 동일합니다. 상위 응용 프로그램이 분명히 존재하지 않고 다른 모든 것 위에 나타날 수 있는 특수 목적 오버레이입니다.

That only gets you so far, though, because of problems with interaction. At first, your overlay will absorb all the interaction, so not only does the head get events, but you block interaction to everything underneath.

LayoutParams를 사용하여 이 동작을 구성합니다.FLAG_NOT_TOUCH_MODAL디스플레이 영역 밖의 이벤트는 기본 UI로 이동합니다.이제 작동은 되겠지만 뒤로 가기/메뉴 버튼이 앱으로 연결되지 않거나 키보드가 없는 등의 다른 나쁜 일이 발생할 수 있습니다.당신이 필요로 하는 것을 해결하기 위해서는FLAG_NOT_FOCUSABLE.

You get a side effect from the non-focusable bit too, which is no nice interactions with your overlay any more, e.g. button presses. You can get some basic touch events though, which you can always do maths on, and that's probably enough for Chat Heads. Just be aware that it leaves you on your own in plenty of areas, like UI animation.

선택적 상호작용 소비 허용을 포함한 세부사항에 대한 좋은 개요는 이 StackOverflow 스레드에서 확인할 수 있습니다.특히 답변 링크 중 하나가 결국 여러분을 여기로 데려다 줄 것이고, 이것은 좋은 예시 프로젝트입니다.ICS는 이 방법을 약간 바꿨지만 스레드는 이를 설명합니다.

이것은 모두 공개 API 같은 것이지만, 당연히 해야 할 주류적인 일은 아닌 것 같습니다.문서에는 특수한 시스템 앱 동작에 대한 언급이 흩어져 있으며, 그럴만한 이유가 있습니다. 만약 모두가 그렇게 했다면 어떻게 되었을까요?

샘솟는 머리들은 대화 머리들의 봄을 기반으로 하는 행동을 상자 밖으로 보여줍니다.대화 헤드를 클릭하면 대화 헤드와 조각이 열릴 수 있도록 정의하기만 하면 됩니다.최소화되면 채팅 헤드가 무너지고 드래그하면 손가락을 따라갑니다.

이 프로젝트에는 내장된 모든 기능을 보여주는 데모 앱이 포함되어 있습니다.사용하려면 그라들 의존성에 이것을 추가해야 합니다.

compile 'com.flipkart.springyheads:library:0.9.6'

언급URL : https://stackoverflow.com/questions/15975988/what-apis-are-used-to-draw-over-other-apps-like-facebooks-chat-heads