Prompt & amp; dialog box ---- Notification in android, android prompt

Source: Internet
Author: User

Android prompt & dialog box ---- Notification, android prompt

Notification (status bar Notification)

1. Notification is used to display notifications in the status bar. The Notification is different on different devices.

Ii. basic layout of Notification

Element composition:

Icon/Photo: Big Icon Tiltle/Name: Title Message: Content Message Timestamp: notification time. The default time is the system time. You can also set the secondary Icon through setWhen ().

3. Basic Procedure of using Notification

The status Notification bar mainly involves two types: Notification and icationicationmanager.

Notification: Notification Information class, which corresponds to the attributes of the Notification bar.

Icationicationmanager: Manages status bar notifications, and is responsible for sending notifications and clear notifications.

Step 1: Obtain the icationicationmanager object icationicationmanager manager = (icationicationmanager) getSystemService (icationication_service );

Step 2: Create a Builder constructor class for the Notification bar, which is Notification. Builder builder = new Notification. Builder (this );

Step 3: configure the Builder, such as the title, content, and Icon action.

Step 4: Call the build () method of Builder to assign values to notification

Step 5: Call icationicationmanager's notify () method to send a notification

In addition, we can call icationicationmanager's cancel () method to cancel the notification.

4. Set related methods

Notification. Builder mBuilder = new Notification. Builder (this );

Then call the following related methods to set up. The common methods are as follows:

  • SetContentTitle(CharSequence): Set the title
  • SetContentText(CharSequence): Set content
  • SetSubText(CharSequence): Set the text of a small line below the content
  • SetTicker(CharSequence): Set the text displayed at the top of the notification.
  • SetWhen(Long): Set the notification time. Generally, it is set to System. currentTimeMillis () when the notification is received ()
  • SetSmallIcon(Int): Set the small icon in the lower-right corner. This small icon is displayed at the top of the page when the notification is received.
  • SetLargeIcon(Bitmap): Set the big icon on the left
  • SetAutoCancel(Boolean): Indicates whether to cancel the Notification after you click "Notification" on the panel (disabled by default)
  • SetDefaults(Int): the simplest way to add sound, flashing, and vibration effects to a notification. You can combine multiple attributes by using the default (defaults) attribute,
    Notification. DEFAULT_VIBRATE(Add the default vibration Reminder );
    Notification. DEFAULT_SOUND(Add the default sound reminder );
    Notification. DEFAULT_LIGHTS(Add the default three-color light Reminder)
    Notification. DEFAULT_ALL(Add all the above three reminders by default)
  • SetVibrate(Long []): sets the vibration mode, for example:
    SetVibrate (new long [] {0,300,500,700}); latencies of 0 ms, 300 ms of vibration, 500 ms of delay, and 700 ms of vibration. The Vibrate usage will be explained later!
  • SetLights(Int argb, int onMs, int offMs): set a three-color light. The parameters are: light color, light duration, and dark time. Not all colors are available. This is related to the device, some mobile phones do not have three-color lights. In addition, you need to set flags to Notification for Notification. FLAG_SHOW_LIGHTS only supports three-color light reminder!
  • SetSound(Uri): Set the ringtone when the notification is received. You can use the system or set it yourself. The example is as follows:
    . SetDefaults (Notification. DEFAULT_SOUND) // get the default ringtone
    . SetSound (Uri. parse ("file: // sdcard/xx/xxw.") // get custom ringtones
    . SetSound (Uri. withAppendedPath (Audio. Media. INTERNAL_CONTENT_URI, "5") // gets the ringtones in the Android multi-Media library.

  • SetOngoing(Boolean): If it is set to true, it indicates that it is 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, performing synchronization, and actively connecting to the network)

  • SetProgress(Int, int, boolean): Set the notification parameters with progress bars to the maximum value of the progress bar, the current progress, and whether the progress is uncertain. If the progress bar is determined, call setProgress (max, progress, false) to set the notification. When the update progress is reached, the notification update SS is initiated here, And the progress bar is removed after the download is complete. You can call setProgress (0, 0, false. If it is an uncertain progress bar (continuous activity), this indicates that the activity is ongoing when the processing progress cannot be accurately known. Therefore, setProgress (0, 0, true) is called. At the end of the operation, call setProgress (0, 0, false) and update the notification to remove the indicator bar.
  • SetContentIntent(PendingIntent): PendingIntent is slightly different from Intent. It can be used to set the number of executions. It is mainly used for remote service communication, alarm, notification, initiator, and SMS. In general, it is rarely used. For example, if you start Activity: getActivity (Context, int, Intent, int) through Pending, you can also start the bit identifier of Service or BroadcastPendingIntent (fourth parameter ):
    FLAG_ONE_SHOTThe returned PendingIntent can be executed only once, and is automatically canceled after execution.
    FLAG_NO_CREATEIt indicates that if the PendingIntent described does not exist, the corresponding PendingIntent is not created, but NULL is returned.
    FLAG_CANCEL_CURRENTIf the PendingIntent already exists, cancel the former and create a new PendingIntent. This facilitates data persistence to be up-to-date and can be used in instant communication scenarios.
    FLAG_UPDATE_CURRENTIndicates the updated PendingIntent.
    Example:

    // Jump to ActivityIntent intent = new Intent (context, XXX. class); PendingIntent pendingIntent = PendingIntent. getActivity (context, 0, intent, 0); mBuilder. setContentIntent (pendingIntent)
  • SetPriority(Int): set priority:

    Priority User
    MAX Important and Urgent Notification: It is urgent or urgent to notify the user of this incident.
    HIGH High priority is used for important communication content, such as short messages or chats, which are of interest to users.
    DEFAULT The default priority is used for notifications without special priority classification.
    LOW Low-priority notifications can be sent to users but are not urgent events.
    MIN It is used for background messages (such as weather or location information ). A notification with the lowest priority will only display icons in the status bar. The content can only be seen in the user drop-down notification drawer.
    Corresponding property: Notification. PRIORITY_HIGH ..

5. Basic instances

Package com. example. test3; import android. app. activity; import android. app. notification; import android. app. icationicationmanager; import android. app. pendingIntent; import android. content. intent; import android. graphics. bitmapFactory; import android. OS. bundle; import android. view. view; import android. widget. button; public class MainActivity extends Activity implements View. onClickListener {private Button btn1; Private Button btn2; // two related classes: private icationicationmanager manager; private Notification notification; private static final int policyid_1 = 1; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); btn1 = (Button) findViewById (R. id. btn1); btn2 = (Button) findViewById (R. id. btn2); btn1.setOnClickListener (this); btn2.s EtOnClickListener (this); manager = (icationicationmanager) getSystemService (icationication_service);} @ Override public void onClick (View view) {switch (view. getId () {case R. id. btn1: {// define a PendingIntent. Click Intent to start a new Intent intent = new Intent (MainActivity. this, OtherActivity. class); PendingIntent pit = PendingIntent. getActivity (MainActivity. this, 0, intent, 0); // you can specify the image text Notification mode. buil Der builder = new Notification. Builder (MainActivity. this); builder. setContentTitle ("ye liangchen") // Title. setContentText ("I have one hundred ways to keep you stuck ~ ") // Content. setSubText (" -- remember me ye liangchen ") // a short text under the content. setTicker (" received the message sent by Ye liangchen ~ ") // The text displayed in the status bar after receiving the message. setWhen (System. currentTimeMillis () // set the notification time. setSmallIcon (R. mipmap. ic_account) // set the small icon. setLargeIcon (BitmapFactory. decodeResource (getResources (), R. mipmap. ic_launcher )). setDefaults (Notification. DEFAULT_LIGHTS | Notification. DEFAULT_VIBRATE) // sets the default three-color lamp and vibrator. setAutoCancel (true) // click it to cancel Notification. setContentIntent (pit); notification = builder. build (); manager. Y (NOTIFYID_1, notification); break;} case R. id. btn2: {manager. cancel (policyid_1); break ;}}}}

 

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.