Android: programmatically get position of the widget on desktop relative to other widgets

会有一股神秘感。 提交于 2019-12-22 09:57:56

问题


On Android the user can place a widget on the desktop and then to move it by long touch and moving the finger while still holding.

Is it possible to programmatically get the position on the screen where the widget was moved by long touch?

I need my desktop widget to know if it's near the edge of the screen of the device. Depending on whether it's on the top of the desktop or at the bottom different layouts for the widget will be chosen.

I would expect that this position is not given in pixels, but as pair of 0-based indexes. E.g. if the device can display 4x7 cells on the desktop, the widget in the bottom-right corner should have coords (3, 6). Also it should be somehow possible to ask the device how many cells fit into the screen.

Or am I misunderstanding something?


回答1:


On Android 2.1 and later, with some select home screens, you can find out where an app widget resides when it is clicked via getSourceBounds() -- this value is attached to any Intent you spawn via a PendingIntent via setOnClickPendingIntent().

However:

  • this only works on Android 2.1 and newer
  • not all home screens might do this, as this is part of the Launcher code IIRC
  • the coordinates are in pixels IIRC
  • there is no way to interrogate the home screen to find out this information any other way, since there is no API to interact with the home screen

Hence, I think your stated goal ("Depending on whether it's on the top of the desktop or at the bottom different layouts for the widget will be chosen") is impossible, I think.



来源:https://stackoverflow.com/questions/5324602/android-programmatically-get-position-of-the-widget-on-desktop-relative-to-othe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!