Analysis of the implementation method of the ANDROID notification bar _android

Source: Internet
Author: User

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

These days have been modifying the source code of Twigee, one of the features to join IS resident notification bar, previously written only in the "notification" group, want to put it in the "running" group but do not know how to put, check the official document, found the method, Add "flag_ongoing_event" to the Notification flags field. At the same time, I also summed up the use of notification. See below for details:

(1), using system-defined notification

Here's how to use sample code:

Create a notificationmanager reference String ns = Context.notification_service;
Notificationmanager Mnotificationmanager = (notificationmanager) getsystemservice (NS); Defines the various attributes of notification int icon = R.drawable.icon; Notification icon Charsequence tickertext = "Hello"; The notification text displayed by the status bar prompts long when = System.currenttimemillis ();
The time that the notification is generated will be displayed in the notification message//with the above attribute to initialize the nofification Notification Notification = new Notification (icon,tickertext,when);
* * Add sound * notification.defaults |=notification.default_sound;
* or use the following methods * Notification.sound = Uri.parse ("File:///sdcard/notification/ringer.mp3");
* Notification.sound = Uri.withappendedpath (Audio.Media.INTERNAL_CONTENT_URI, "6"); * If you want the sound to continue to repeat until the user responds to the notification, you can add "flag_insistent" to the Flags field in the Notification if notification defaults field includes "Default_sound"
property, this property will overwrite the voice defined in the Sound field/* * Add Vibration * notification.defaults |= notification.default_vibrate; * or you can define your own vibrational mode: * long[] vibrate = {0,100,200,300};
0 milliseconds after the vibration, vibration 100 milliseconds after the stop, after 200 milliseconds again vibration 300 milliseconds * notification.vibrate = vibrate; * A long array can beTo define any length desired * If the defaults field of notification includes "Default_vibrate", this property will overwrite the vibration defined in the vibrate field/* * Add LED lights reminder *
Notification.defaults |= notification.default_lights;
* or can own LED reminder mode: * Notification.ledargb = 0xff00ff00; * Notification.ledonms = 300; Light time * notification.ledoffms = 1000;
Extinguished time * notification.flags |= notification.flag_show_lights; * * * More feature attributes * Notification.flags |= flag_auto_cancel; Click this notice on the notification bar to automatically clear this notice * notification.flags |= flag_insistent; Repeat the sound until the user responds to this notification * notification.flags |= flag_ongoing_event; Place this notice in the "ongoing", "Running" group, in the notification bar * Notification.flags |= flag_no_clear; Indicates that the notice is not cleared after clicking on the "purge notice" in the notification bar, *//often used with flag_ongoing_event * notification.number = 1; The number field represents the current amount of events represented by this notification, and it will overwrite the top of the status bar icon *//If you want to use this field, you must start with 1 * notification.iconlevel =; *//Set notification event Message context = Getapplicationcontext (); Context Charsequence Contenttitle = "My Notification"; Notice Bar title Charsequence ContentText = "Hello world!"; Notification bar content Intent notificationintent = new Intent (this,main.class); //Click on the notification to jump activity pendingintent contentintent = pendingintent.getactivity (this,0,notificationintent,0);
Notification.setlatesteventinfo (context, Contenttitle, ContentText, contentintent);

 Pass the notification to Notificationmanager mnotificationmanager.notify (0,notification);

If you want to update a notification, simply call Setlatesteventinfo () again after you set up notification, and then resend the notification again, that is, call Notify () again.

(2), using a custom Notification

To create a custom notification, you can use Remoteviews. To define your own extended message, first initialize a Remoteviews object, pass it to the Notification Contentview field, and pass pendingintent to the Contentintent field. The following sample code is the complete step:

1. Create a custom message layout view.xml

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:layout_width=" fill_parent "android:layout_height=" fill_parent ">
<ImageView Android:id= "@+id/image" android:layout_width= "wrap_content"
android:layout_height= "Fill_parent" Android: layout_marginright= "10DP"/>
<textview android:id= "
@+id/text" android:layout_width= "Wrap_content" android:layout_height= "Fill_parent" android:textcolor= "#000"/>
</LinearLayout>

2, in the program code to use the Remoteviews method to define image and text. and upload the Remoteviews object to the Contentview field.

Remoteviews Contentview = new Remoteviews (Getpackagename (), r.layout.view);
Contentview.setimageviewresource (R.id.image,r.drawable.icon);
Contentview.settextviewtext (R.id.text, "hello,this message being in a custom expanded view");
Notification.contentview = Contentview;

3. Define a intent for the Notification contentintent field (note that the Setlatesteventinfo () method is not required to use a custom view)

Intent notificationintent = new Intent (this,main.class);
Pendingintent contentintent = pendingintent.getactivity (this,0,notificationintent,0);
Notification.contentintent = contentintent;

4, send the notice

Mnotificationmanager.notify (2,notification);

The following are all sample code:

//Create a notificationmanager reference String ns = Context.notification_service;
Notificationmanager Mnotificationmanager = (notificationmanager) getsystemservice (NS); Defines the various attributes of notification int icon = R.drawable.icon; Notification icon Charsequence tickertext = "Hello"; The notification text displayed by the status bar prompts long when = System.currenttimemillis ();
The time that the notification is generated will be displayed in the notification message//with the above attribute to initialize the nofification Notification Notification = new Notification (icon,tickertext,when);
Remoteviews Contentview = new Remoteviews (Getpackagename (), R.layout.view);
Contentview.setimageviewresource (R.id.image, r.drawable.iconempty);
Contentview.settextviewtext (R.id.text, "Hello,this is JC");
Notification.contentview = Contentview;
Intent notificationintent = new Intent (this,main.class);
Pendingintent contentintent = pendingintent.getactivity (this,0,notificationintent,0);
Notification.contentintent = contentintent;

Pass the notification to Notificationmanager mnotificationmanager.notify (0,notification); 

For more information on Android-related content readers can view the site topics: "Android Development Introduction and Advanced Course", "Android debugging techniques and common problems solution summary", "Android Multimedia operating skills Summary (audio, video, recording, etc.)", " Android Basic Components Usage Summary, Android View tips Summary, Android layout layout tips and Android Control usage summary

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

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.