Advanced Notification and notification skills

Source: Internet
Author: User

Advanced Notification and notification skills

  • Observe the Notification class and you will find many attributes that we have never used. Let's take a look at the sound attribute. It can play a piece of audio when the notification is sent, so that it can better inform the user of the arrival of the notification. Sound is a Uri object. Therefore, you must first obtain the URI of the audio file when specifying the audio file. For example, if there is a Basic_tone.ogg audio file under the Mobile Phone/system/media/audio/ringtones directory, you can specify it in the Code as follows:
Uri soundUri = Uri. fromFile (new File ("/system/media/audio/ringtones/Basic_tone.ogg"); notification. sound = soundUri;
  • In addition to allowing audio playback, we can also vibrate the mobile phone when the notification arrives.VibrateThis attribute. It is a long integer array.Used to set the duration of the mobile phone's static and vibration, in milliseconds. 0 indicates the duration of the mobile phone, 1 indicates the duration of the mobile phone, 2 indicates the duration of the mobile phone, and so on. Therefore, if you want your phone to vibrate for 1 second immediately when the notification arrives, then wait for 1 second, and then vibrate for 1 second, the code can be written as follows:
Long [] vibrates = {0, 1000,100 0, 1000}; notification. vibrate = vibrates; however, you still need to declare permissions to control the mobile phone vibration. Therefore, we have to edit the AndroidManifest. xml file and add the following statement: <manifest xmlns:android="http://schemas.android.com/apk/res/android"     package="com.example.notificationtest"     android:versionCode="1"     android:versionName="1.0" >     ……     <uses-permission android:name="android.permission.VIBRATE" />     …… </manifest>
  • The current mobile phone basically has a front LED light. When there is a phone call or unread text message, and the mobile phone is in the screen lock status, the LED light will flash continuously and remind the user to check it. We canUse ledARGB, ledOnMS,LedOffMS and flags attributes to achieve this effect.LedARGB is used to control the color of LED lights, generallyThree colors are available: Red, green, and blue. LedOnMS is used to specify the duration of the LED light, in milliseconds. LedOffMS is used to specify the darkness duration of the LED lamp, also in milliseconds. Flags can be used to specify the actions of a notification.This includes displaying LED lights.Therefore, when the notification arrives, if you want to achieve the flashing effect of the LED light with green light, you can write it as follows:
Notification. ledARGB = Color. GREEN; notification. ledOnMS = 1000; notification. ledOffMS = 1000; notification. flags = Notification. FLAG_SHOW_LIGHTS; of course, if you do not want to perform so many complicated settings, you can also directly use the default notification effect. It will decide what ringtones to play and how to vibrate based on the current mobile phone environment, statement: notification. defaults = Notification. DEFAULT_ALL; note that the above advanced skills must be run on the mobile phone to see the effect. The simulator cannot show vibration, LED flashing, and other functions.

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.