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