Android Development of Notification notification usage detailed _android

Source: Internet
Author: User
Tags constant sqlite database

This example describes the notification notification usage of Android development. Share to everyone for your reference, specific as follows:

Depending on the life cycle of the activity, the OnStop function (such as pressing the Home key) is executed when the activity is not displayed, so you put the notification in the notification bar in the OnStop function (except for the exit key), and then the display Remove the notification from the notice bar. Alternatively, the notification bar icon will be displayed as long as the program is running.

The following is a brief introduction to some constants, fields, and methods in the notification class:

Constant:

Default_all uses all default values, such as sound, vibration, splash screen, etc.
Default_lights Use the default flash hint
Default_sounds Use the default prompt sound
Default_vibrate Use default phone vibration

"description": Add the phone vibration, be sure to add permissions in the Manifest.xml:

<uses-permission android:name= "Android.permission.VIBRATE"/>

The above effect constants can be superimposed, that is, by

Notification.defaults =default_sound| Default_vibrate; 

Notification.defaults |= Default_sound (preferably tested on the real machine, not on the vibration effect simulator)

Set flag bit

Flag_auto_cancel the notification can be cleared by the purge button of the status bar
Flag_no_clear the notification can be cleared by the purge button of the status bar
Flag_ongoing_event notifications are placed in a running
Flag_insistent whether the music is playing all the time, knowing the user's response

Commonly-asked Fields:

Contentintent sets the Pendingintent object and sends the intent when clicked
Defaults Add a default effect
Flags set flag bits, such as flag_no_clear, etc.
Icon setting icons
Sound Set Sound
Tickertext the text displayed in the status bar
When the timestamp is sent for this notification

Notificationmanager Common Method Introduction:

public void Cancelall () removes all notifications (only for notification under the current context)
public void cancel (int id) removes the notification marked as ID (only for all notification under the current context)
public void Notify (String tag, int id, Notification Notification) adds a notification to the status bar, labeled Tag, labeled as ID
The public void notify (int id, Notification Notification) joins the notification in the status bar, marked as ID

Package com.ljq.activity;
Import android.app.Activity;
Import android.app.Notification;
Import Android.app.NotificationManager;
Import android.app.PendingIntent;
Import android.content.Intent;
Import Android.graphics.Color;
Import Android.os.Bundle; The public class Mainactivity extends activity {/** called the ' when the ' is the ' The activity ' is a./@Override public void created
Ate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main);
Clearnotification (); @Override protected void OnStop () {shownotification (); Super.onstop ();} @Override protected void OnStart () {Clearnoti
Fication ();
Super.onstart (); /** * Display notification in status bar/private void shownotification () {//Create a Notificationmanager reference Notificationmanager notificationmanage 
R = (Notificationmanager) this.getsystemservice (Android.content.Context.NOTIFICATION_SERVICE); Defines the various properties of Notification Notification Notification =new Notification (R.drawable.icon, "steering system", System.currenttimemillis ( 
)); Flag_auto_CANCEL the notification can be cleared by the purge button of the status bar//flag_no_clear the notification cannot be cleared by the purge button of the status bar//flag_ongoing_event notification is placed on the running//flag_insistent whether it is ongoing, For example, music has been playing, know the user response notification.flags |= notification.flag_ongoing_event; Place this notice in the "ongoing", the "Running" group, in the notification bar notification.flags |= notification.flag_no_clear; 
Indicates that after clicking on the "clear notice" in the notification bar, this notice is not cleared, often with flag_ongoing_event to use Notification.flags |= notification.flag_show_lights; Default_all uses all default values, such as sound, vibration, splash screen, and so on//default_lights use the default flash hint//default_sounds use the default hint sound//default_vibrate use the default phone vibration, add <uses-permission android:name= "Android.permission.VIBRATE"/> Permissions notification.defaults = Notification.default_ 
LIGHTS; Superposition effect constant//notification.defaults=notification.default_lights|
Notification.default_sound; 
Notification.ledargb = Color.Blue; NOTIFICATION.LEDONMS = 5000; Flash time, milliseconds//Set notification event message Charsequence Contenttitle = "steering system title"; Notice Bar title Charsequence ContentText = "supervision system content"; Notification bar content Intent notificationintent =new Intent (Mainactivity.this, Mainactivity.class); Click on the notification to jump to the activity PendinginTent contentitent = pendingintent.getactivity (this, 0, notificationintent, 0); 
Notification.setlatesteventinfo (This, Contenttitle, ContentText, contentitent); 
Pass the notification to Notificationmanager notificationmanager.notify (0, notification); }//delete notification private void Clearnotification () {//After startup delete the notification we defined Notificationmanager Notificationmanager = (notificationmana 
GER) this. Getsystemservice (Notification_service); 
Notificationmanager.cancel (0);

 }
}

For more information on Android-related content readers can view the site topics: "The basic components of Android Usage Summary", "Android View Overview", "Android Resource Operation tips Summary", "Android File Operation skills Summary", " Android Operation SQLite Database skills Summary, "Android operation JSON format Data Skills summary", "Android Database Operation skills Summary", "Android programming development of SD card Operation Summary", "Android Development introduction and Advanced Course", A summary of the operational skills of Android programming and the summary of the usage of Android controls

I hope this article will help you with the Android program.

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.