Android notification bar implementation

Source: Internet
Author: User

Android notification bar implementation

Package com. example. mynotification; import android. OS. bundle; import android. app. activity; import android. app. notification; import android. app. icationicationmanager; import android. app. pendingIntent; import android. content. intent; import android. support. v4.app. notificationCompat; import android. view. menu; public class MainActivity extends Activity {public PendingIntent getDefalutIntent (int flags) {PendingInten T pendingIntent = PendingIntent. getActivity (this, 1, new Intent (), flags); return pendingIntent ;}@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); icationicationmanager mNotificationManager = (icationicationmanager) getSystemService (icationication_service); NotificationCompat. builder mBuilder = new NotificationCompat. bui Lder (this); mBuilder. setContentTitle ("test title") // set the title of the notification bar. setContentText ("Test content") // set the display content in the notification bar. setContentIntent (getDefalutIntent (Notification. FLAG_AUTO_CANCEL) // set the intent of clicking in the notification bar //. setNumber (number );. setTicker ("") // The notification bar appears in the notification bar for the first time, with animation effect. setWhen (System. currentTimeMillis () // The notification bar time. Generally, the system is used directly. setPriority (Notification. DEFAULT_ALL) // set the priority of the notification bar //. setAutoCancel (true) // The user disappears after clicking the panel. setOngoing (false) // true, set it to an ongoing notification. They are usually used to represent a background task. users actively participate (such as playing music) or are waiting in some way, so they // occupy devices (such as downloading a file and performing synchronization operations, active Network connection ). setDefaults (Notification. DEFAULT_VIBRATE) // The simplest and most consistent way to add sound, flashing, and vibration effects to the notification is to use the default settings of the current user, // use the default attribute, can be combined // Notification. DEFAULT_ALL Notification. DEFAULT_SOUND: Add a sound // requires VIBRATE permission. setSmallIcon (R. drawable. ic_launcher); Notification notification = mBuilder. build (); notification. flags = Notification. FLAG_ONGOING_EVENT; notification. flags = Notification. FLAG_NO_CLEAR; // when you click clear, Intent intent = new Intent (getApplicationContext (), MainActivity is not cleared. class); intent. addCategory (Intent. CATEGORY_LAUNCHER); intent. setFlags (Intent. FLAG_ACTIVITY_NEW_TASK | Intent. FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); PendingIntent pendingIntent = PendingIntent. getActivity (getApplicationContext (), 0, intent, 0); mBuilder. setContentIntent (pendingIntent); micationicationmanager. notify (0, mBuilder. build () ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}
I don't know why my clicks will still be cleared.

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.