Simple application of Android notification

Source: Internet
Author: User

Notification is displayed in the phone status bar notification, the phone status bar is located at the top of the phone screen, where the general display of the phone's current network status, battery status, time and so on. The notification lock represents a notification with a global effect, and the program generally sends notification through the Notificationmanager service.


Mainactivity.java

public class Mainactivity extends Activity {final int notification_id=1; Notificationmanager nm; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main); nm = (Notificationmanager) getsystemservice ( Notification_service);} public void Send (view view) {Intent Intent = new Intent (mainactivity.this,nextactivity.class);// The pendingintent inside encapsulates a intent that means the interface to start pendingintent pi = pendingintent.getactivity (mainactivity.this, 0, intent, 0); Builder builder = new Notification.builder (mainactivity.this);//set to open the notification, the notification automatically disappears Notification Notification = Builder.setautocancel (TRUE)//sets the notification prompt information that is displayed in the status bar. Setticker ("new Message")//Set small notification icon. Setsmallicon (R.drawable.ic_launcher)// Sets the caption for the notification content. Setcontenttitle ("title of the content of the notification")//Set the content of the notification. Setcontenttext ("The content of the notification ... ")//set using the system default sound, default LED light. SetDefaults (notification.default_sound| notification.default_lights)//Set notification to start the Intent.setcontentintent (PI). build ();//setup complete, send notification nm.notify (notification_id , notification);} Public VOID Cancel (view view) {//Nm.cancel (NOTIFICATION_ID) According to the ID of the setting;}} 
If you do not want to use the default settings, you can also use the following code

Set Custom Sound Setsound ((Uri.parse ("File:///sdcard/click.mp3"));//Set Custom vibration setvibrate (new long[]{0,50,100,150});



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Simple application of Android 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.