Use notification in Android to implement the general notification bar (notification example one)

Source: Internet
Author: User

Notification is a message that is displayed outside of your app's general interface. When the app sends a message to the system, the message is first displayed as a chart in the notification bar. To view the details of the message, you need to go to the notification drawer (notificationdrawer). (notificationdrawer) are system-level controlled and you can view them at any time, without limiting the app.

Notification's design:

As an important part of the Android UI, notification has its own design guidelines.

The notification interface element in the notification drawer is notification in two ways, depending on your Android version and the status of the Notificationdrawer.

Notification two modes of display:

(1) Normal view

This style is the standard display mode of notification drawer.

(2) Wide view

When your notification is expanded, it will show a larger view, which is a new feature after android4.1.

Here we describe in detail the implementation of the normal view:

In the Touton view, notification up to 64DP, even if you create a wide-view-style notification, which is displayed as normal size in the case of non-expansion. The following is an ordinary notification.

The Blue indicator box represents the following meaning:

1. Title

2. Large Icons

3. Notification Content

4. Notification data

5. Small Icons

The release time of 6.Notification.

You can set an explicit time by calling Setwhen (),

The default is the time the system receives the notification.

Below we are the results of our presentation:

This time in the general view based on the addition of the click Page Jump effect, can be understood as adding notification action and behavior:

While this is optional, you should add at least one behavior for your notification: Allow the user to click Notification to enter an activity for more viewing or further action. A notification can provide a variety of actions, and you should also let the user click on a notification to always have a corresponding response action, usually open an activity. You can also add a button that responds to a click event in notification, such as delaying the alarm or immediately replying to a short message.

Inside notification, an action itself is defined in a pendingintent, Pendingintent contains a intent that launches the activity in your app. To connect pendingintent with a gesture, you need to call the appropriate Notificationcompat.builder method.

For example, if you want to start activity when you click Notification Text, you need to call Notificationcompat.builder's Setcontentintent () to add pendingintent. Starting an activity is the most common type of notification action response.

The first step: layout activity_main.xml (Set trigger button only):
1<?xml version= "1.0" encoding= "Utf-8"?>2<LinearLayout3Xmlns:android= "Http://schemas.android.com/apk/res/android"4Xmlns:tools= "Http://schemas.android.com/tools"5Android:id= "@+id/activity_main"6Android:layout_width= "Match_parent"7android:layout_height= "Match_parent"8tools:context= "Com.example.administrator.day12.MainActivity" >9<ButtonTenAndroid:text= "Show Notifications" OneAndroid:layout_width= "Match_parent" Aandroid:layout_height= "Wrap_content" -Android:id= "@+id/button" -android:onclick= "Show1"/> the</LinearLayout>
Step two: in Layout Jump page activity_content.xml(only set display text):
1<?xml version= "1.0" encoding= "Utf-8"?>2<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"3Xmlns:tools= "Http://schemas.android.com/tools"4Android:id= "@+id/activity_content"5Android:layout_width= "Match_parent"6android:layout_height= "Match_parent"7tools:context= "Com.example.administrator.day12.ContentActivity" >8<TextView9Android:layout_width= "Match_parent"Tenandroid:layout_height= "Match_parent" Oneandroid:gravity= "Center" AAndroid:textsize= "30SP" -Android:text= "10 wins ten defeats"/> -</LinearLayout>
Step Three: Java (Main interface button's Click event) Implementation Code Mainactivity.java:
1 Importandroid.app.Notification;2 ImportAndroid.app.NotificationManager;3 Importandroid.app.PendingIntent;4 ImportAndroid.content.Context;5 Importandroid.content.Intent;6 Importandroid.graphics.BitmapFactory;7 Importandroid.support.v7.app.AppCompatActivity;8 ImportAndroid.os.Bundle;9 ImportAndroid.support.v7.app.NotificationCompat;Ten ImportAndroid.view.View; One Importandroid.widget.RemoteViews; A  Public classMainactivityextendsappcompatactivity { -     Private Static Final intNo_1 =0x1 ; - @Override the     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); -     } +      Public  voidShow1 (View v) { -Notificationcompat.builder Builder =NewNotificationcompat.builder ( This); + Builder.setsmallicon (R.mipmap.guojia); ABuilder.setcontenttitle ("Guo Jia"); atBuilder.setcontenttext ("Let's play Yuan Shao.")); -         //set Notification.default_all (Enable all services by default (breathing lights, ringtones, etc.) - builder.setdefaults (notification.default_all); -         //Call Notificationcompat.builder's Setcontentintent () to add pendingintent -Intent Intent =NewIntent ( This, Contentactivity.class); -Intent.putextra ("info", "Guo Jia sent you a trick!") "); inpendingintent pi = pendingintent.getactivity ( This, 0, intent, pendingintent.flag_update_current); - builder.setcontentintent (pi); to         //Get notification +Notification n =builder.build (); -         //get the notification object yourself by NotificationCompat.Builder.build () theNotificationmanager Manager =(Notificationmanager) Getsystemservice (context.notification_service); *         //then call Notificationmanager.notify () to transfer to the system $ manager.notify (no_1, n);Panax Notoginseng     } -}

Fourth Step: Java (post-jump activity) function code implementation Contentactivity.java (toast only):
1  Public classContentactivityextendsappcompatactivity {2 @Override3     protected voidonCreate (Bundle savedinstancestate) {4         Super. OnCreate (savedinstancestate);5 Setcontentview (r.layout.activity_content);6         //get toast content by getting the Putextra set in Mainactivity7Toast.maketext ( This, Getintent (). Getstringextra ("info"), Toast.length_short). Show ();8     }9}

The code that demonstrates the effect of this, we comb the next implementation of the idea:

(1) Trigger Click event via button

(2) Assign some UI information and related actions of notification to Notificationcompat.builder object, and then pass NotificationCompat.Builder.build () To obtain the notification object itself, and then call Notificationmanager.notify () to transmit the notification to the system.

(3) In the second step through the builder's setcontentintent () to add pendingintent, for notification add behavior, that is, the activity of the jump

(4) The effect of setting the performance on the open activity.

Use notification in Android to implement the general notification bar (notification example one)

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.