Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PointerService extends Service {
- private ImageView cursor;
- private CountDownTimer timer;
- private WindowManager.LayoutParams params;
- public void Update(final int x, final int y) {
- Log.d("PointerService", "Updating cursor position. X = " + Integer.toString(x) +
- "nY = " + Integer.toString(y));
- //Packet received => timer reset
- cursor.setVisibility(View.VISIBLE);
- cursor.setX(x);
- cursor.setY(y);
- cursor.postInvalidate();
- timer.cancel();
- timer.start();
- }
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- @Override
- public void onCreate() {
- super.onCreate();
- Log.d("PointerService", "Creating service");
- cursor = new ImageView(this);
- cursor.setImageResource(R.drawable.ic_cursor);
- cursor.setVisibility(View.INVISIBLE);
- params = new WindowManager.LayoutParams(
- 64, 64,
- WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
- WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
- WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
- WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
- WindowManager.LayoutParams.FLAG_SECURE,
- PixelFormat.TRANSPARENT);
- params.setTitle("Cursor");
- params.gravity = Gravity.TOP | Gravity.LEFT;
- WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
- wm.addView(cursor, params);
- timer = new CountDownTimer(5000, 5000) {
- @Override public void onTick(long l) {}
- @Override public void onFinish() {
- cursor.setVisibility(View.INVISIBLE);
- cursor.postInvalidate();
- }
- };
- timer.start();
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- Log.d("CursorService", "Service destroyed");
- Singleton.getInstance().pointerService = null;
- if(cursor != null) {
- ((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(cursor);
- cursor = null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement