Android development Notification

Source: Internet
Author: User

The Android system has a notification bar, which is useful. Notifications are an important part of the interaction between Android and users. For example, new emails, short messages, and missed calls are displayed in the notification bar. We drag down the notification bar and click the notification information to display the specific notification content. Cool features.

We can also define the notification style, icons, notification sound, LED color, and flashing frequency.

Let's take a look at the example

1: package com. halzhang. android. notification;
   2:  
   3: import android.app.Activity;
   4: import android.app.Notification;
   5: import android.app.NotificationManager;
   6: import android.app.PendingIntent;
   7: import android.content.Intent;
   8: import android.os.Bundle;
   9: import android.view.View;
  10: import android.widget.Button;
  11: /**
12: * notification
13: * @ author Zhang hanguo
  14:  */
  15: public class NotificationDemo extends Activity {
  16:     /** Called when the activity is first created. */
17: // notification id
  18:     private static final int NOTICE_ID = 1222;
  19:     private Button notify;
  20:     private Button cancel;
  21:     @Override
  22:     public void onCreate(Bundle savedInstanceState) {
  23:         super.onCreate(savedInstanceState);
  24:         setContentView(R.layout.main);
  25:         notify = (Button) findViewById(R.id.noti);
  26:         cancel = (Button) findViewById(R.id.cancel);
  27:         notify.setOnClickListener(new View.OnClickListener() {
  28:             
  29:             @Override
  30:             public void onClick(View v) {
  31:                 notityMe();
  32:             }
  33:         });
  34:         
  35:         cancel.setOnClickListener(new View.OnClickListener() {
  36:             
  37:             @Override
  38:             public void onClick(View v) {
  39:                 final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
40: // notification of Cancellation
  41:                 manager.cancel(NOTICE_ID);
  42:             }
  43:         });
  44:     }
  45:     
  46:     private void notityMe(){
47: // get notification manager. Notification is a system service.
  48:         final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
49: // initialize the notification object p1: Notification icon p2: Notification status bar display prompt p3: Notification display time
50: Notification notification = new Notification (R. drawable. icon, "Notification test", System. currentTimeMillis ());
51: // The Intent after the notification is clicked.
  52:         PendingIntent intent = PendingIntent.getActivity(this, 0, new Intent(this, NotificationDemo.class), 0);
53: // set Notification Information
54: notification. setLatestEventInfo (this, "notification title", "notification Information Content", intent );
55: // notification
  56:         manager.notify(NOTICE_ID, notification);
  57:     }
  58: }

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.