Android Learning Notice (Notification)

Source: Internet
Author: User
Tags deprecated

Wirelessly creating a notification requires the use of Notificationmanager to manage notifications. Can be obtained by invoking the Getssystemservice () method of the context. The Getssystemservice () method receives a parameter, which is a string that specifies which service to use. Context.notification_service is the specified notification service. This method returns an object that you want to cast.

Notificationmanager manager = (Notificationmanager) getsystemservice (Notification_service);
    • Create a Notification object

1. Android API level 11 or less

New"This is ticker text", System.currenttimemillis ());

Notification (int icon, charsequence tickertext, long when) is written in Androvan file This constructor is deprecated in API level 11.  Use Notification.Builder instead.

This means that the creation of notification on Android API level 11 is invalid.

2. Higher than Android API level 11 below API level 16

Built with Notification.builder, you can use GetNotification () to get notification objects. However, the document says getnotification() This method is deprecated in API level 16. Use build() instead.

That is, the GetNotification () method obtains the notification object above the Android API level 16 is invalid.

3. Higher than Android API level 16

Using Notification.builder to build, call Builder's build () to get the object.

    • Notification Setting the layout

Notification.builder There are many ways to build the layout of notifications. Here's a piece of code:

 Packagecom.example.notificationtest;ImportJava.io.File;Importandroid.app.Activity;Importandroid.app.Notification;ImportAndroid.app.NotificationManager;Importandroid.app.PendingIntent;Importandroid.content.Intent;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button; Public classMainactivityextendsActivityImplementsonclicklistener{PrivateButton Sendnotice; Private intnum; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Num= 0;        Setcontentview (R.layout.activity_main); Sendnotice=(Button) Findviewbyid (R.id.send_notice); Sendnotice.setonclicklistener ( This); } @Override Public voidOnClick (View v) {//TODO auto-generated Method Stub        Switch(V.getid ()) { Caser.id.send_notice:intent Intent=NewIntent (mainactivity. This, Notificationactivity.class); Intent.putextra ("id", + +num); //Jump Intent First parameter context second parameter document explanation private request code for the sender//The third parameter intent the fourth parameter, flagspendingintent pi = pendingintent.getactivity ( This, 0, intent, pendingintent.flag_update_current); //Invoke BeepUri Sounduri = Uri.fromfile (NewFile ("/system/media/audio/ringtones/01_piano.ogg")); //call vibrate is a long array No. 0 represents a few seconds after shaking the 1th number shaking how long the 2nd number waits and so on            Long[] vibrates = {0,100}; Notificationmanager Manager=(Notificationmanager) Getsystemservice (Notification_service); Notification Notification=NewNotification.builder ( This). SetDefaults (Notification.default_all)//notify the default sound vibration breathing light. Setticker ("This is Ticker")//text displayed in the status bar. Setcontenttitle ("This is title")//set the first line of notifications in the drop-down bar. Setcontenttext ("This is Text")//set the second row of notifications in the drop-down bar. Setsubtext ("This is Subtext")//set the third line of the notification in the drop-down bar. Setsmallicon (R.drawable.ic_launcher)//set Small icons for the status bar//. Setlargeicon (b)//set the large icon in the drop-down bar. Setcontentintent (PI)//jump to the intent. Setsound (Sounduri)//Set notification sounds. Setvibrate (Vibrates)//Set the vibration effect. build ();//Build NotificationManager.notify (num,notification);//display notification The first parameter represents an ID value             Break; default:             Break; }    }}

Vibrations need to be added in the Androidmanifest.xml declaration:

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

When we enter into another activity by notification, we need to remove the notification from the status bar. You can call the Cancel () method of Notificationmanager in the activity that is started, receiving 1 parameters, the ID value of the notification.

    • The Pendingintent.getactivity () method is the value of the fourth parameter:

Flag_update_current: If pendingintent already exists, keep it and replace only its extra data. Will update the previous pendingintent message, for example, you push message 1, and in the intent in which a value of "ABC", before the message is not clicked, continue to push the second message, and in the intent in which putextra a value "CBA "Well, at this time, if you click on message 1 or message 2, you will find that the information read from both of them is" CBA ", that is, the second one replaces the first. Intent.

Flag_cancel_current: If pendingintent already exists, then the current pendingintent will be canceled, and then generate a new pendingintent is still the above steps, it will be found, click on the message 1 o'clock, No response, the second one can be clicked.

Flag_one_shot:pendingintent can only be used once. After invoking the instance method, send (), it will be automatically cancel, and calling the Send () method again will fail.

Flag_no_create: If Pendingintent does not exist, it is simpler when returning null. 

Learn the general usage of Android notifications here.

Android Learning Notice (Notification)

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.