Android uses notification to implement a common notification bar (a) _android

Source: Internet
Author: User

Notification is a message that is displayed outside of your application's regular interface. When the app lets the system send a message, the message is first displayed as a chart in the notification bar. To see the details of the message you need to go to the notification drawer (notificationdrawer) to view it. (notificationdrawer) are system-level controlled, you can view them anytime, not limited to the app.

The design of notification:

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

Notification interface elements in the notification drawer notification there are two ways to display, depending on your Android version and Notificationdrawer status.

Two ways to display notification:

(1) Normal view

This style is the standard display method 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's a detailed overview of the common View implementation:

In the Touton view, notification is the highest 64DP, even if you create a wide view style of notification, in the case of not expanded in the normal size display. Below is an ordinary notification.

The Blue indicator box represents the following meaning:

1. Title

2. Large icon

3. Notification Content

4. Notification data

5. Small icon

The release time of 6.Notification.

You can set a definite time by calling Setwhen ().

The default is when the system receives the notification.

Below we are the results of our presentation:


This time in the general view of the basis of adding a 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 users to click Notification to enter an activity for more viewing or subsequent operations. A notification can provide a variety of actions, and you should also have the user click a notification to always have a corresponding response action, usually open an activity. You can also add a button in the notification that responds to the Click event, such as delaying the alarm, or replying to a short message immediately.

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

For example, if you want to start an activity when you click on 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.

First step: activity_main.xml in Layout (Set Trigger button only):

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
 "http://schemas.android.com/" Apk/res/android "
 xmlns:tools=" Http://schemas.android.com/tools "
 android:id=" @+id/activity_main
 " Android:layout_width= "Match_parent"
 android:layout_height= "match_parent"
 tools:context= " Com.example.administrator.day12.MainActivity ">
 <button
  android:text= Display notification"
  Android:layout_ Width= "Match_parent"
  android:layout_height= "wrap_content" android:id= "@+id/button"
  android:o nclick= "Show1"/>
</LinearLayout>

Step Two: jump page activity_content.xml in layout (set display text only):

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
 xmlns:tools=" Http://schemas.android.com/tools "
 android:id=" @+id/activity_content
 " Android:layout_width= "Match_parent"
 android:layout_height= "match_parent"
 tools:context= " Com.example.administrator.day12.ContentActivity ">
 <textview
  android:layout_width=" Match_parent "
  android:layout_height= "match_parent"
  android:gravity= "center"
  android:textsize= "30SP"
  Android:text= "10 wins ten defeats"/>
</LinearLayout>

Step Three: Java (main interface button click event) Implementation Code Mainactivity.java:

Import android.app.Notification;
Import Android.app.NotificationManager;
Import android.app.PendingIntent;
Import Android.content.Context;
Import android.content.Intent;
Import Android.graphics.BitmapFactory;
Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;
Import Android.support.v7.app.NotificationCompat;
Import Android.view.View;
Import Android.widget.RemoteViews;
 public class Mainactivity extends Appcompatactivity {private static final int no_1 =0x1;
  @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
 Setcontentview (R.layout.activity_main);
  public void Show1 (View v) {notificationcompat.builder Builder = new Notificationcompat.builder (this);
  Builder.setsmallicon (R.mipmap.guojia);
  Builder.setcontenttitle ("Guo Jia");
  Builder.setcontenttext ("Let's play Yuan Shao");
  Set Notification.default_all (all services are enabled by default (breathing lights, ringtones, etc.) builder.setdefaults (Notification.default_all); Call the Notificationcompat.builder setcontentintent ()To add pendingintent Intent Intent = new Intent (this, contentactivity.class); Intent.putextra ("info", "Guo Jia sent you a ruse!")
  ");
  pendingintent pi = pendingintent.getactivity (this, 0, intent, pendingintent.flag_update_current);
  Builder.setcontentintent (PI);
  Get Notification Notification n = builder.build (); Get notification object by NotificationCompat.Builder.build () Notificationmanager manager = (Notificationmanager)
  Getsystemservice (Context.notification_service);
 Then call Notificationmanager.notify () to the system to transfer manager.notify (No_1, N);  }
}

Fourth Step: Java (jump activity) Functional code implementation Contentactivity.java (only toast):

public class Contentactivity extends Appcompatactivity {
 @Override
 protected void OnCreate (Bundle Savedinstancestate) {
  super.oncreate (savedinstancestate);
  Setcontentview (r.layout.activity_content);
  Get the toast content toast.maketext by getting the Putextra set in Mainactivity
  (this, getintent (). Getstringextra ("info"), Toast.length_ Short). Show ();
 }

Demo effect of the code on these, we comb down the realization of the idea:

(1) Trigger Click event via button

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

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

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

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.