开发者

Android - Notification bar height from a service

The application I'm working on is a service which runs in the background i开发者_StackOverflow社区n android. The problem is that I need to determine the height of the notification bar for some functionality of the service.

I found a number of solutions for this, for regular activities - a view inside an activity can determine its own height without the notification bar, based on checking its actual view size from its onSizeChanged event, after it is already drawn. However, this is not applicable to a service which has no physical view drawn.

I would really appreciate any ideas on getting the size of the notification bar at a system level, perhaps?

Thanks so much!

Vitaliy


Have an activity tell your service the proper size to use. The status bar height, nor the screen size, is likely to change during the operation of your service.


Maybe you can retrieve an standard notification bar icon and measure it.

You'll have to use a system icon so you have more chances to be present on all Android customizations (Sense UI, MotoBlur, etc...)

Something like:

Drawable phoneCallIcon = getResources().getDrawable(android.R.drawable.stat_sys_phone_call);

int height = phoneCallIcon.getIntrinsicHeight();

Running this from my Nexus One gives 38 pixels height, which is accurate.


A safer approach to this 'hack' would be iterating through some of the standard notification icons.

It would be this way:

int notificationBarResources[] = {
            android.R.drawable.stat_sys_phone_call,
            android.R.drawable.stat_notify_call_mute,
            android.R.drawable.stat_notify_sdcard,
            android.R.drawable.stat_notify_sync,
            android.R.drawable.stat_notify_missed_call,
            android.R.drawable.stat_sys_headset,
            android.R.drawable.stat_sys_warning };
int notificationBarHeight = -1;
    for (int i = 0; i < notificationBarResources.length; i++) {
        try {
            Drawable phoneCallIcon = getResources().getDrawable(
                    android.R.drawable.stat_sys_phone_call);
            if ((notificationBarHeight = phoneCallIcon.getIntrinsicHeight()) != -1) {
                break;
            }
        } catch (Resources.NotFoundException e) {
            // Nothing to do
        }
    }


I have the same problem. My view lays out images in the view constructor. The only time you know the actual view height is on the onMesure method by doing:

   protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    widthView = MeasureSpec.getSize(widthMeasureSpec);
    heightView = MeasureSpec.getSize(heightMeasureSpec);
   }

However I can't lay out the images inside this method (animation of the images will keep calling this method).

To know the actual height of the view I would need to do:

screenHeight - statusBarHeight

so I could use this value on the constructor of the view.

I can't believe it has to be so complex to provide a solution for such a simple requirement.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜