#Android application interface development

Chapter 3 Study

#### Part 4 ####

# # # # Notification and Widget# # # #

Notification and Widget, why do they talk together? Because they are so similar, they even have the same way of customizing the interface, which is probably not in many books


First you have to go through this website, there are very few official and elegant introductions in Chinese, this is one


Ok, done, I’m assuming you only looked at the pictures, Great! enough

This is the elements of a default Notification, a picture on the left, two lines of text in the middle, one large and one small, a time on the right (usually showing the system time when the Notification occurred), and a small icon

When I get a new tool, I always like to create it directly from the template in Android Studio, because there are all kinds of methods in it, so it’s easy to install

First is this interface. According to the description in the first link of this article, our Notification has been significantly changed after Android 4.1. By default, it is a small bar on the top. It’s a big screen like the one below, so you can display more content, maybe without even entering the main app.

Here, More text means to display More text after the drop downCopy the code

What about this one? It drops down to show an extra imageCopy the code

This gives me a List, very good, very powerful List, for the moment we do not use, after all we are new, take your time, we choose None, there is no gorgeous drop-down effect again…

Ahem, next screen

Finish. It's too long. Just take the main methodCopy the code

And then defines a NotificationCompat Builder instance, only one parameter, is the Context Context, who call the notification, this Context is who; And then there's a whole bunch of. XXXXX, all set up for this builder, what a killer shotCopy the code
  • SetContentTitle (CharSequence) : Sets the title
  • SetContentText (CharSequence) : Sets the content
  • SetSubText (CharSequence) : Sets a small line of text below the content
  • SetTicker (CharSequence) : Sets the text message to be displayed at the top when receiving a notification
  • SetWhen (long) : Sets the notification time, typically system.currentTimemillis () when the notification is received.
  • SetSmallIcon (int) : Sets the small icon in the lower right corner that will also be displayed at the top when notifications are received
  • SetLargeIcon (Bitmap) : Sets the large icon on the left
  • SetAutoCancel (Boolean) : Should the Notification be cancelled after the user clicks on the Notification panel (default: no cancellation)
  • SetDefaults (int) : The simplest way to add sound, flashing, and vibrating effects to notifications, using defaults properties that can combine multiple properties, notification. DEFAULT_VIBRATE(adds default vibrate reminders); Notification.DEFAULT_SOUND(add default sound reminders); Notification.DEFAULT_LIGHTS(add default tricolor lights) notification. DEFAULT_ALL(add default tricolor lights)
  • SetVibrate (long[]) : Sets the vibrate mode, for example:
  • SetVibrate (new long [] {0300500700}); Delay 0ms, then Vibrate 300ms, delay 500ms, then Vibrate 700ms. More on the use of Vibrate later!
  • SetLights (int arGB, int onMs, int offMs) : setLights(int arGB, int onMs, int offMs) : setLights(int arGB, int onMs, int offMs) : setLights(int arGB, int onMs, int offMs) : setLights(int arGB, int onMs, int offMs) : setLights(int arGB, int onMs, int offMs) In addition, you need to set flags to notification. FLAG_SHOW_LIGHTS for Notification.
  • SetSound (Uri) : Sets the ringtone for receiving notifications. SetSound (uri.parse (“file:///sdcard/xx/xx.mp3”)) // get custom ringtones .setSound(uri.withappendedPath (audio-media.internal_content_uri, “5”)) // Gets the ringtone in the Android multimedia library
  • SetOngoing (Boolean) : Set to true, meaning it is an ongoing notification. They are usually used to indicate a background task in which the user is actively engaged (such as playing music) or is waiting in some way and therefore occupying the device (such as a file download, synchronous operation, active network connection)
  • SetProgress (int,int, Boolean) : setProgress(int,int, Boolean) : setProgress(int,int, Boolean) : setProgress(int,int, Boolean) : setProgress(int,int, Boolean) : setProgress(int,int, Boolean) : setProgress(int,int, Boolean) : setProgress(int,int, Boolean) : Call setProgress(Max, progress, false) to set the notification, initiate the notification to update the progress when the progress is updated, and remove the progress bar when the download is complete by calling setProgress(0, 0, false). If it is an indeterminate (continuous activity) progress bar, this indicates that the activity is continuing when the progress cannot be accurately known, so call setProgress(0, 0, true). At the end of the operation, call setProgress(0, 0, false) and update the notification to remove the indicator bar
  • PendingIntent (PendingIntent) setContentIntent(PendingIntent) : Different from Intent Intent, PendingIntent can set the number of times it is executed. It is mainly used for remote service communication, alarm, notification, initiator, and SMS. For example, start an Activity with Pending: getActivity(Context, int, Intent, int). You can also start a Service or Broadcast PendingIntent with a bit identifier (fourth parameter) : FLAG_NO_CREATE Indicates that if the PendingIntent described does not exist, no PendingIntent is created. Instead, return NULL FLAG_CANCEL_CURRENT to indicate that the PendingIntent already exists, then cancel it and create a new PendingIntent. This helps keep the data current. Communication scenarios that can be used for instant messaging FLAG_UPDATE_CURRENT indicates the updated PendingIntent

Too many Settings, that is to say, if you set one, there will be more information on the notification, according to their own requirements, I choose to simplify

final NotificationCompat.Builder builder = new NotificationCompat.Builder(context)

        notify(context, builder.build());
Copy the code

Finally how to solve the API level is automatically generated, he would call the NotificationManager. Notify () method of startup notification

Start up! Start notifications in the Activity (or anywhere else)

Copy the code

The first parameter is left out, the second one is passed a string to the title and text, and the last one is just an int = =, the official default is there, and it will be displayed in the lower right corner of the notification bar

About custom notification interface

I’ll start by defining an XML layout without going into details

Then add RemoteView to the Notification method before a long list of.setxxxx

Widget and Notification both require RemoteView to manage the layout file. Fill in the constructor parameter with: 1. Full package name; 2. The following three layout file ids represent the controls in RemoteView, which can be manually modified based on external parameters

Even so, we don’t want to use notify anymore, we want to turn notifications into a foreground service! (After the service is started, the Context is the service and the service initiates this Notification)

    Service.startForeground(1, builder.build());
Copy the code

Yes, it’s started, I hope I can understand, haha…

– the –