There are two kinds of hints about messages: One is toast, the other is notification. The former is relatively short-lived, while the latter remains relatively long.
And our usual mobile phone applications such as NetEase, bar paste and so on have a lot of push message, is to achieve with notification.
Notification is the notification displayed in the phone status bar-the phone status bar is located above the phone screen. Programs generally send notification notifications via the Notificationmanager service
Notification some of the methods that we can use next:
SetDefaults (): Set notification led, music, vibration and so on.
Setautocancel (): After setting the click Notification, the status bar automatically deletes the notification.
Setcontenttitle (): Sets the title of the notification
Setcontenttext (): Set the content of the notification
Setticker (): Set Alert information for notifications
Setsmallicon (): Sets the icon for the notification (note that the third one is the upper case of I, not the lowercase of L)
Steps to send notification:
1. Call the Getsystemservice (Notification_service) method to get the system's NOTIFICATION Manager service
2. Create a notification object from the constructor.
3. Set various properties for notification.
4, send notification through Notificationmanager.
Here, we need to be aware of adding several permissions to the Androidmanifest.xml file:
<!--add permission to operate the Flash--><uses-permission android:name= "Android.permission.FLASHLIGHT"/><!--Add Action Vibrator permissions-- ><uses-permission android:name= "Android.permission.VIBRATE"/>
Next, we explain it through specific code.
Main.xml:
<span style= "FONT-SIZE:14PX;" ><?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/ Res/android "android:orientation=" horizontal "android:layout_width=" Fill_parent "android:layout_height=" Fill_ Parent "android:gravity=" Center_horizontal "><buttonandroid:layout_width=" Wrap_content "Android:layout_ height= "wrap_content" android:text= "Send Notification" android:onclick= "send"/></linearlayout></span >
a button is set here and a notification is sent.
Then, let's look at the Notificationtest.java code:
<span style= "FONT-SIZE:14PX;" >package Cn.notificationtest.com;import Cn.notificationtest.com.r;import Android.app.activity;import Android.app.notification;import Android.app.notificationmanager;import Android.app.pendingintent;import Android.content.intent;import Android.net.uri;import Android.os.bundle;import Android.view.View;public class Notificationtest extends activity{static final int notification_id = 0x123; Notificationmanager nm; @Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate) ; Setcontentview (r.layout.main);//Get the system's Notificationmanager service NM = (Notificationmanager) getsystemservice ( Notification_service);} Defines an event-handling method for the Click event of a button that sends a notification public void send (View source) {//Create a intentintent intent = new intent that initiates other activity Notificationtest.this, Otheractivity.class); pendingintent pi = pendingintent.getactivity (notificationtest.this, 0, intent, 0); Notification notify = new Notification.builder (this)//set to turn on the notification, the notification automatically disappears. Setautocancel (TRUE)//settings displayed in the status barNotification message. Setticker ("NetEase News")//Set the icon for the notification. Setsmallicon (r.drawable.notify)//sets the title of the notification content. Setcontenttitle ("This is the headline")// Sets the notification content. Setcontenttext ("This is the content of the news: *************")///set to use the system default sound, Default LED light//. SetDefaults (notification.default_sound/ / | notification.default_lights)//Set custom sounds for notifications. Setsound (Uri.parse ("android.resource://cn.notificationtest.com/" + r.raw.msg). Setwhen (System.currenttimemillis ())//Set notification to start the program's Intent.setcontentintent (PI). GetNotification ();// Send notification nm.notify (notification_id, notify);}} </span>in this Java file, we create a notification object from the constructor. Then set various properties for notification. Finally send notification via Notificationmanager.
(one thing to note here is that we define the sounds, icons, etc. that are created by the individual)
With the Java code above, we created a intent object that can be switched to another activity interface via this notification: otheractivity
<span style= "FONT-SIZE:14PX;" >/** * */package cn.notificationtest.com;import cn.notificationtest.com.r;import Android.app.Activity;import Android.os.bundle;public class Otheractivity extends activity{@Overridepublic void OnCreate (Bundle savedinstancestate {super.oncreate (savedinstancestate);//Set the activity to display the page Setcontentview (R.layout.other);}} </span>
As shown below:
Android Development Series (24): Features and usage of notification