Android Notification for Sound and vibration

Source: Internet
Author: User

When we send a notification to the Android system, we often need to alert the user through vibration or sound. How to set the sound and vibrate for notification. The general ideas are:
-Androidnotification system default sound and vibration
-Set a custom sound and vibration for androidnotification
-Use vibrator and soundpool to generate sound and vibration

The use of vibration requires attention to add permissions:

<uses-permission android:name="android.permission.VIBRATE"/>
Use the system default sound and vibration

1. Set notification

//使用默认的声音notif.defaults |= Notification.DEFAULT_SOUND;//使用默认的震动notif.defaults |= Notification.DEFAULT_VIBRATE;//使用默认的声音、振动、闪光notif.defaults = Notification.DEFAULT_ALL;

2. Set Notificationcompat.builder

Notificationcompat.builder setdefaults (int defaults)

//使用默认的声音、振动、闪光new Notification.Builder(context).setDefaults(Notification.DEFAULT_ALL);//使用默认的震动和声音new Notification.Builder(context).setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE)
Set a custom sound and vibration for notification vibrate

Androidnotification Vibration is actually called the Vibrator vibrate (long[] pattern, int repeat) This method, the passed parameter is a long[].

Long[] Parameter Introduction

The first parameter of the array indicates the delay vibration time
The second parameter indicates the duration of the vibration
The third parameter indicates the sleep time after shaking
The fourth parameter also indicates the duration of the vibration.
The fifth parameter also indicates a positive to sleep time
And so on

//start without a delay//vibrate for  milliseconds//Sleep for 1000 millisecondslong[] pattern = { Span class= "Hljs-number" >0, 100, 1000};//start without a delay//Each element then alternates between vibrate, sleep, vibrate, sleep...long[] pattern1 = {0 , 100, 1000, 300, 200, 100, 500, 200, Span class= "Hljs-number" >100};              

Setting a custom vibration mode for notification

//为Notification设置notification.vibrate = pattern;//为Builder设置NotificationCompat.Builder.setVibrate (pattern)
Sound

Notification sound parameters, the request type is URI. For the specification of URIs, refer to Http://www.ietf.org/rfc/rfc2396.txt.

1. Get the URI
From Rawuri Sound=uri. Parse ("android.resource://" + getpackagename () +"/" + R. Raw. Notificationsound);Oruri Sound=uri. Parse (Contentresolver. Scheme_android_resource +"://" + getpackagename () +"/raw/notificationsound");Oruri Sound=uri. Parse (contentresolver. Scheme_android_resource + "://" + getpackagename () + "/" +r. Raw. Notificationsound)//from Ringtone manager uri sound= Ringtonemanager. Getdefaulturi (Ringtonemanager. type_notification);//URI Sound=uri from file Uri Sound=uri. FromFile (New file ("/sdcard/sound.mp3")). Parse (new File ("/sdcard/sound.mp3"). toString ()));//From Contentresolver
2. Set the URI
notification.sound =Uri sound;NotificationCompat.Builder.setSound(Uri sound)
Self call vibrate and sound play using vibrate

Http://stackoverflow.com/questions/13950338/how-to-make-an-android-device-vibrate

Play sound

Android Notification for Sound and vibration

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.