Notification use and pendingintent.getactivity ()

Source: Internet
Author: User



 Public voidsendnotification (Context ctx,string message) {//get the Notification ManagerString NS =context.notification_servers; Notificationmanager nm=(Notification) ctx.getsystemservice (NS); //Create notification object includes icon, text, the time to send                    inticon =R.drawable.robot; Charquence Tickertext= "Hello"; Longwhen = System.currenttimemills ();//Return the system current time in milliseconds since 1970Notification Notification=NewNotification (Icon,tickertext,when); //set Contextview by using SetlastesteventinfoIntent Intent =NewIntent (Intent.action_view); Intent.setdata (Uri.parse ("Http://www.google.com")); Pendingintent Pi= Pendingintent.getactivity (ctx,0,intent,0);//explain it belowNotification.setlastesteventinfo (CTX, "title", "text", PI); //Send notificationNm.notify (1,notification);//notification join status bar, marked as id=1}//above all what to use Notification

The personal level is limited, the exposure to the temporary is so much, so when I finished the system will be a supplement to this

The use of Notification requires the above four steps.

Simply put, pendingintent.getactivity () is the intent that is about to happen.

The official pendingintent.getactivity (Ctx,o,intent,o) document explains this:

Public static Pendingintent getactivity (context context, int requestcode, Intent Intent, int flags)SINCE:API Level 1

Retrieve a pendingintent that would start a new activity like calling Context.startActivity(Intent) . Note that the activity would be started outside of the context of the existing activity, so you must use the Intent.FLAG_ACTIVITY_NEW_TASK launch FL AG in the Intent.

Parameters
Context The Context in which this pendingintent should start the activity.
Requestcode Private request code for the sender (currently not used). I'm not using it now.
Intent Intent of the activity to being launched.
Flags May is,,,, or any of the "as supported by" to FLAG_ONE_SHOT FLAG_NO_CREATE FLAG_CANCEL_CURRENT FLAG_UPDATE_CURRENT Intent.fillIn() control which unspecified parts of the inte NT that can is supplied when the actual send happens.
Returns
    • Returns an existing or new pendingintent matching the given parameters. May return NULL if have FLAG_NO_CREATE been supplied.

A few excerpts from a person's blog are just better understood than official documents:

Below to talk about notification, this notification generally used in the phone, sms, mail, alarm clock, the status bar on the phone will appear a small icon, prompting the user to deal with this alert, then the hand from the top of the slide status bar can be expanded and processed this alert. I found this feature particularly useful, so I'll talk about it according to my understanding. Excerpt from the Help document: The notification class represents a persistent notification that will be submitted to the user using Notificationmanager. Added Notification.builder to make it easier to build notifications. Notification is a way for your application to run alert users without opening or in the background. It is an invisible program component (broadcast Receiver,service and inactive activity) that warns the user of the best way to take care of the events that occur.

First, distinguish between the following status bar and status bars :

1, the status bar is the mobile phone screen at the top of a section of the shape of the area;

In the status bar has a lot of information: such as USB connection icon, phone signal icon, battery level icon, time icon and so on;

2, the status bar is the hand from the state bar sliding down the view can be telescopic;

There are generally two classes in the status bar (using the flag_ tag):

(1) the procedure in progress;

(2) is a notification event;

Presumably to describe the creation of a notification message that is transmitted:

1, a status bar icon;

2, in the Stretched Status bar window displays the information with a large title, a small title, an icon, and has to handle the Click event: such as invoking the program's entry class;

3, flashing, LED, or vibration;

The quick steps to create a notification can be divided into the following four stages:

First step : Get Notificationmanager object by Getsystemservice () method;

The second step : To set some properties of notification such as: content, icon, title, the corresponding notification action to deal with, and so on;

The third step : Execute a notification by the Notify () method of the Notificationmanager object;

Fourth Step : Cancel a notificatioin with the Cancel () method of the Notificationmanager object;

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

Constant:

Default_all use all default values, such as sound, vibration, splash screen, etc.

Default_lights using default Flash Hints

Default_sounds using default cue Sound

Default_vibrate using default phone vibrate

" description": Join the phone shake, be sure to add permissions in Manifest.xml:

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

The above effect constants can be superimposed, i.e. by

Mnotifaction.defaults =default_sound | Default_vibrate;

or mnotifaction.defaults |=default_sound (preferably tested on a real machine, not on the vibration effect simulator)

Set flag bit

Flag_auto_cancel This notification can be cleared by the purge button of the status bar

flag_no_clear This notification can be cleared by the purge button of the status bar

flag_ongoing_event Notifications are placed in the running

Whether the flag_insistent is always on, such as music always playing, know user response

Characters commonly used segment:

contentintent Set Pendingintent object, send the intent when clicked

Defaults adding default effects

Flags set flag bits, such as flag_no_clear, etc.

icon Settings icons

Sound setting sounds

tickertext text that appears in the status bar

When to send this notification timestamp

Notification.build Construction Notification Method Description:

void Setlatesteventinfo (Context context, charsequencecontenttitle,charsequence contenttext,pendingintent Contentintent)

Features: Displays the Notification property in the stretch status bar and sends the Pendingintent object when clicked

Parameters: Context Environment

Contenttitle big title in status bar

ContentText a small title in the status bar

Contentintent will send Pendingintent object when clicked

Note: If you add an icon to the notification, you must use this method to display the icon in the status bar and status bars.

Finally, let's talk about the common methods of the Notificationmanager class:

Get the object by getting the system service:

Notificationmanager Mnotificationmanager = (notificationmanager) getsystemservice (Context.notification_service);

Notificationmanager Common Methods 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 voidnotify (String tag, int id, Notification Notification) will be notified to join the status bar, labeled Tag, labeled as ID

public void notify(int id, Notification Notification) will be notified to join the status bar, labeled as ID

Http://www.cnblogs.com/babynight/archive/2012/08/22/Notification_NotificationManage_PendingIntent.html

Notification use and Pendingintent.getactivity () (RPM)

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.