[Practical] How To Get The Android Status Bar and the android Status Bar

Source: Internet
Author: User

[Practical] How To Get The Android Status Bar and the android Status Bar

private static int getStatusHeight(Context context){        int statusHeight = 0;        Rect localRect = new Rect();        ((Activity) context).getWindow().getDecorView().getWindowVisibleDisplayFrame(localRect);        statusHeight = localRect.top;        if (0 == statusHeight){            Class<?> localClass;            try {                localClass = Class.forName("com.android.internal.R$dimen");                Object localObject = localClass.newInstance();                int i5 = Integer.parseInt(localClass.getField("status_bar_height").get(localObject).toString());                statusHeight = context.getResources().getDimensionPixelSize(i5);            } catch (Exception e) {                e.printStackTrace();            }         }        return statusHeight;    }


[Practical] How to obtain the Android Status Bar


How does Android obtain the system height, title bar, and Status Bar Height?

GetWindow (). getDecorView (). getWindowVisibleDisplayFrame (rect); // obtain the entire view. Note that if you want to set the title style, this must appear after the title style; otherwise, an error occurs: int top = rect. top; // The height of the status bar, so rect. height, rect. width is the width of the system height. View v = getWindow (). findViewById (Window. ID_ANDROID_CONTENT); // obtain the Root View int top2 = v. getTop (); // the total height of the title bar of the status bar, so the height of the title bar is top2-topint width = v. getWidth (); // The width of the view, which is always the largest int height = v. getHeight (); // view height, excluding the status bar and title bar. If you only want to obtain the screen size, you can use Display display = getWindowManager (). getdefadisplay display ();

Steps for status bar notification in android

Private icationicationmanager = (NotificationManager) getSystemService (NOTIFICATION_SERVICE); // This attribute will be used later to wake up notifications

Directly write a method for you, which is the step and the parameter meaning:
TickerText text when a notification is prompted
ContentTitle: The title displayed when you drag the status bar
ContentText: The content displayed when you drag the status bar
Id is an identifier and may contain multiple notifications.
ResId image resource to notify the previous image
Private void showNotification (String tickerText, String contentTitle,
String contentText, int id, int resId ){
// Create a notification object. The image resource is in front of it, and the following parameter is the current time, indicating that the notification will be received immediately.
Notification notification = new Notification (resId,
TickerText, System. currentTimeMillis ());
// PendingIntent indicates the content executed by intent after you click the notification.
PendingIntent contentIntent = PendingIntent. getActivity (this, 0,
GetIntent (), 0 );
// Notification display content
Notification. setLatestEventInfo (this, contentTitle, contentText,
ContentIntent );

// Notification. defaults = defaults; it is set to vibrate, except for vibrate, but must be called before wakeup.

// Wake up
Icationicationmanager. notify (id, notification );

}

Do not ask!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.