2.5.8 Notification (Status Bar Notification) Detailed Explanation
Category Android Basic Tutorial
Introduction to This Section:
>
This section introduces the control used to display notification information in the status bar of Android: Notification. I believe most people who learn Android are very familiar with it. Many online tutorials about Notification are based on 2.x, but now most Android devices are basically above 4.x, and even some are above 5.0; their respective Notifications are different! This section explains the Notification based on above 4.x, and the Notification above 5.0 will be explained in the advanced tutorial section of Android 5.0 new features ~
Official documentation on some of Notification:
Design Concept: Notifications in Android 4.4 and Lower
Translation: Notifications
API Documentation: Notification
Visiting the above website may require a ladder ~
1. Interpretation of the Design Document
1) Basic Layout of Notification
>
The composition elements above are as follows:
Icon/Photo: Large icon
Title/Name: Title
Message: Content information
Timestamp: Notification time, the default is the time when the system sends the notification, which can also be set through setWhen()
Secondary Icon: Small icon
Content Text, a text to the left of the small icon
2) Extended Layout
>
In Jelly Bean, you can provide more event details for notifications. You can display the first few lines of the message or a preview of the picture through the extended layout. This way, users can see more content - sometimes they can even see the entire message. Users can open the extended layout by pinch-zoom or double-finger swipe. Android provides two types of extended layouts (text and image) for you to use when developing applications.
Regarding other design-related things, I will not mention them one by one. If you are interested, please check the API documentation provided above and know that this Notification can be various above version 4.x! Most of the time, we pay more attention to how to write code to use this thing. Let's learn about the usage of Notification below!
2. Basic Usage Process of Notification
>
The status notification bar mainly involves two classes: Notification and NotificationManager
Notification: Notification information class, which corresponds to the various attributes of the notification bar
NotificationManager: It is the management class of the status bar notification, responsible for sending notifications, clearing notifications, and other operations.
The basic process of use is as follows:
Step 1. Obtain the NotificationManager object: NotificationManager mNManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Step 2. Create a Builder construction class for the notification bar: Notification.Builder mBuilder = new Notification.Builder(this);
Step 3. Set up the Builder for related settings, such as title, content, icon, action, etc.!
Step 4. Call the build() method of Builder to assign a value to notification
Step 5. Call the notify() method of NotificationManager to send a notification!
PS: In addition, we can also call the cancel() method of NotificationManager to cancel the notification
3. Setting Some Related Methods:
>
Notification.Builder mBuilder = new Notification.Builder(this);
Then call the following related methods for settings: (Official API documentation: Notification.Builder) Commonly used methods are as follows:
setContentTitle (CharSequence): Set the title
setContentText (CharSequence): Set the content
setSubText (CharSequence): Set a small line of text below the content
setTicker (CharSequence): Set the text information displayed at the top when receiving the notification
setWhen (long): Set the notification time, generally set to the System.currentTimeMillis() when receiving the notification
setSmallIcon (int): Set the small icon in the lower right corner. This small icon will also be displayed at the top when receiving the notification
setLargeIcon (Bitmap): Set the large icon on the left
setAutoCancel (boolean): Whether to cancel the notification after the user clicks the Notification panel (default is not canceled)
setDefaults (int): The simplest way to add sound, flash, mNManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); bindView();
}
private void bindView() { btn_show_normal = (Button) findViewById(R.id.btn_show_normal); btn_close_normal = (Button) findViewById(R.id.btn_close_normal); btn_show_normal.setOnClickListener(this); btn_close_normal.setOnClickListener(this); }
@Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_show_normal: // Define a PendingIntent to start an Activity after clicking on the Notification Intent it = new Intent(mContext, OtherActivity.class); PendingIntent pit = PendingIntent.getActivity(mContext, 0, it, 0);
// Set attributes such as image, notification title, send time, and notification method
Notification.Builder mBuilder = new Notification.Builder(this);
mBuilder.setContentTitle("Ye Liangchen") // Title
.setContentText("I have a hundred ways to make you stay uncomfortable~") // Content
.setSubText("—Remember, my name is Ye Liangchen") // A short text under the content
.setTicker("Received a message from Ye Liangchen~") // The text information displayed in the status bar after receiving the message
.setWhen(System.currentTimeMillis()) // Set the notification time
.setSmallIcon(R.mipmap.ic_lol_icon) // Set the small icon
.setLargeIcon(LargeBitmap) // Set the large icon
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE) // Set the default three-color light and vibrator
.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.biaobiao)) // Set custom notification sound
.setAutoCancel(true) // Set to cancel the Notification after clicking
.setContentIntent(pit); // Set PendingIntent
notify1 = mBuilder.build();
mNManager.notify(NOTIFYID_1, notify1);
break;
case R.id.btn_close_normal:
// In addition to canceling the Notification based on the ID, you can also call cancelAll(); to close all notifications generated by the app
mNManager.cancel(NOTIFYID_1); // Cancel the Notification
break;
}
} } ```
The comments are very detailed, so I won't explain them one by one~
5. Download the code sample:
Summary of this section:
>
Well, this section introduced the basic usage of Notification in the 4.x version, which is very simple, right~ Android Notification Bar Notification Integration Comprehensive Learning (A DEMO to Fully Understand It) is quite detailed~ That's it for this section, thank you~
-1.0.1 Latest Android Basic Tutorial Catalog for 2015
-1.1 Background and System Architecture Analysis
-1.2 Development Environment Setup
-1.2.1 Developing Android APPs with Eclipse + ADT + SDK
-1.2.2 Developing Android APPs with Android Studio
-1.3 Solving SDK Update Issues
-1.4 Genymotion Emulator Installation
-1.5.1 Git Tutorial for Local Repository Basic Operations
-1.5.2 Git for Using GitHub to Set Up a Remote Repository
-1.6 How to Play with 9 (Nine Sister) Images
-1.7 Interface Prototype Design
-1.8 Project-Related Analysis (Various Files, Resource Access)
-1.9 Android Program Signing and Packaging
-1.11 Decompiling APK to Obtain Code & Resources
-2.1 The Concept of View and ViewGroup
-2.2.1 LinearLayout (Linear Layout)
-[2.2.2 RelativeLayout (Relative Layout)](android-tutorial-relativelayout
2.5.4 Basic Usage of AutoCompleteTextView (Auto-Complete Text Box)
2.5.8 Detailed Explanation of Notification (Status Bar Notification)
2.6.4 Simple Usage of DrawerLayout (Official Side-Slide Menu)
3.6 Responding to System Setting Events (Configuration Class)
4.4.2 Further Exploration of ContentProvider - Document Provider
5.2.1 Detailed Explanation of Fragment Example - Implementation of Bottom Navigation Bar (Method 1)
5.2.2 Detailed Explanation of Fragment Example - Implementation of Bottom Navigation Bar (Method 2)
5.2.3 Detailed Explanation of Fragment Example - Implementation of Bottom Navigation Bar (Method 3)
5.2.4 Detailed Explanation of Fragment Example - Bottom Navigation Bar + ViewPager Page Swiping
6.2 Data Storage and Access - Saving User Preferences with SharedPreferences
[6.3.1 An Introduction to SQLite Database](android-tutorial-
8.3.7 Paint API - Xfermode and PorterDuff Explained (Part Four)
8.3.8 Paint API - Xfermode and PorterDuff Explained (Part Five)
8.3.14 Paint API - Several Enumeration/Constant Values and ShadowLayer Shadow Effect
8.3.17 Detailed Explanation of Canvas API (Part 2) - A Collection of Clipping Methods
8.3.18 Detailed Explanation of Canvas API (Part 3) - Matrix and drawBitmapMesh
8.4.3 Android Animation Collection - Property Animation - First Encounter
8.4.4 Android Animation Collection - Property Animation - Another Encounter
11.0 "The Latest 2015 Android Basic Tutorial" Concludes with Fireworks~
12.2 DrySister Girl Watching App (First Edition) - 2. Parsing Backend Data
12.4 DrySister Girl Watching App (First Edition) - 4. Adding Data Caching (Integrating SQLite)
[12.5 DrySister Girl Watching App (First Edition) - 5. Code Review, Adjustment, and Writing of Logging Classes](android