Asked  6 Months ago    Answers:  5   Viewed   33 times

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

 Answers

80

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.

Tuesday, June 1, 2021
 
PeanutsMcgee
answered 6 Months ago
51
08-23 15:38:21.021: E/AndroidRuntime(4200): Caused by: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@418c0b68 -- permission denied for this window type

Problem: Permission missing in maifest file.

Solution: Use following permission in AndroidManifest.

android.permission.SYSTEM_ALERT_WINDOW
Saturday, July 31, 2021
 
Kasun Sandaruwan
answered 4 Months ago
52

No, this can't be done. A user can only interact with whatever app is currently in the foreground.

Sunday, August 29, 2021
 
Rahul
answered 3 Months ago
14

You need to pass uri to the intent Here 100005727832736 is the user id of the person who you want to message to

Uri uri = Uri.parse("fb-messenger://user/100005727832736");

Here is my sample code

Uri uri = Uri.parse("fb-messenger://user/100005727832736");

Intent toMessenger= new Intent(Intent.ACTION_VIEW, uri);
try {
        startActivity(toMessenger);
    } 
catch (android.content.ActivityNotFoundException ex) 
    {
        Toast.makeText(context, "Please Install Facebook Messenger",    Toast.LENGTH_LONG).show();
}
}

This is what worked for me and i haven not tested this for some time now.

Tuesday, August 31, 2021
 
joostvandriel
answered 3 Months ago
80

You should look at Android System Overlay which pretty much draws a view on the root on Android Window Manager.

See: Creating a system overlay window (always on top)

You need "android.permission.SYSTEM_ALERT_WINDOW" permission in your manifest.

See: http://developer.android.com/reference/android/Manifest.permission.html#SYSTEM_ALERT_WINDOW

You might also like to look at Window manger

Tuesday, September 21, 2021
 
Pierre Schambacher
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share