Following source will give you a movable overlay. (Like Chat head service provided by Facebook).
But in here there's no any other activities append to this and if some one need to do something like that please comment I'll provide the source for that.
For beginning create a service and include following code. So this will run as a service in you application.
package com.chinthaka.testing; import android.app.Service; import android.content.Intent; import android.graphics.PixelFormat; import android.os.IBinder; import android.view.Gravity; import android.view.MotionEvent; import android.view.View; import android.view.WindowManager; import android.widget.ImageView; public class ChatHeadService extends Service { private WindowManager windowManager; private ImageView chatHead; WindowManager.LayoutParams params; @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); 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.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); } }
Don't forget to add this as a service in to your AndroidManifest.xml file. And also always remember to give the permission to access SYSTEM_ALERT_WINDOW in your AndroidManifest.xml.
To do these steps add following source to your application.
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> ...... ...... ...... <service android:enabled="true" android:name="com.chinthaka.testing.ChatHeadService"> </service>
Start Service in activity by calling following :
startService(new Intent(this, ChatHeadService.class));
No comments:
Post a Comment