Last active
December 18, 2015 11:59
-
-
Save ayushhgoyal/5779757 to your computer and use it in GitHub Desktop.
Facebook chat heads in android application.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// I forked this code from- http://www.piwai.info/chatheads-basics/ | |
// Chat head is displayed from a service. | |
// uses a permission: <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> | |
// This is a service so it needs to be declared in manifest: <service android:name=".ChatHeadService" /> | |
// Whenever you need to display the chat head, just start the service | |
// example: startService(new Intent(context, ChatHeadService.class)); | |
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.ic_launcher); | |
final 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); | |
chatHead.setOnLongClickListener(new OnLongClickListener() { | |
@Override | |
public boolean onLongClick(View v) { | |
windowManager.removeView(chatHead); | |
return true; | |
} | |
}); | |
// | |
// chatHead.setOnTouchListener(new View.OnTouchListener() { | |
// private int initialX; | |
// private int initialY; | |
// private float initialTouchX; | |
// private float initialTouchY; | |
// | |
// @Override | |
// public boolean onTouch(View v, MotionEvent event) { | |
// switch (event.getAction()) { | |
// case MotionEvent.ACTION_DOWN: | |
// initialX = params.x; | |
// initialY = params.y; | |
// initialTouchX = event.getRawX(); | |
// initialTouchY = event.getRawY(); | |
// return true; | |
// case MotionEvent.ACTION_UP: | |
// return true; | |
// case MotionEvent.ACTION_MOVE: | |
// params.x = initialX | |
// + (int) (event.getRawX() - initialTouchX); | |
// params.y = initialY | |
// + (int) (event.getRawY() - initialTouchY); | |
// windowManager.updateViewLayout(chatHead, params); | |
// return true; | |
// | |
// } | |
// return false; | |
// } | |
// }); | |
} | |
@Override | |
public void onDestroy() { | |
super.onDestroy(); | |
if (chatHead != null) | |
windowManager.removeView(chatHead); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment