Set your own software's ringtone + vibrate in Android

Source: Internet
Author: User

Sometimes some communication software needs these functions, such as receiving text messages, notifications, etc., asking the phone to make a ringtone, or vibrate, or glow to prompt the user to know.

Often mobile phones have default settings, such as the user to turn on the ring + vibration, only the ring does not vibrate, completely mute and so on ...

This is the time to have a rule, at least software settings can not be in conflict with the system

Rules:

1. The software should have its own settings configuration file, to save the reminder rules of its own software

2. Follow the system settings, such as: The system is completely silent, people want to sleep, you software, although the bell is shaking full, you have to shut up quietly.

3. If there is a need to remind, first get the configuration of the system, and then make logical judgments to give what kind of reminder.

Code:

First you need to receive a Notification parameter privatevoid setalarmparams (Notification Notification) {//audiomanager provides access to          Volume and ringer mode control.         Audiomanager volmgr = (audiomanager) mappcontext.getsystemservice (Context.audio_service); Switch (Volmgr.getringermode ()) {//Get system set ringtone mode case audiomanager.ringer_mode_silent://Mute mode, value 0, this time does not vibrate, does not ring no                Tification.sound =null;                Notification.vibrate =null;            Break                Case audiomanager.ringer_mode_vibrate://Vibration mode, the value is 1, this time vibration, not ringing notification.sound =null;                Notification.defaults |= notification.default_vibrate;            Break                Case audiomanager.ringer_mode_normal://General mode, with a value of 2, in two cases: 1_ Bell but not vibrate, 2_ Bell + vibrate Uri ringTone =null;                Get the settings for the software sharedpreferences sp = preferencemanager.getdefaultsharedpreferences (Mappcontext);    if (!sp.contains (Systemutil.key_ring_tone)) {//If no configuration file is generated, then both the ringtone and the vibration                Notification.defaults |= notification.default_vibrate;                Notification.defaults |= Notification.default_sound;                    }else{String ringfile = sp.getstring (Systemutil.key_ring_tone, NULL);                    if (ringfile==null) {//No value, blank, does not play ringtone ringtone=null; }elseif (!                    Textutils.isempty (Ringfile)) {//have ringtones: 1, default 2 custom, all return a Uri ringtone=uri.parse (ringfile);                                        } notification.sound = RingTone;                    Boolean vibrate = Sp.getboolean (systemutil.key_new_mail_vibrate,true);                    if (vibrate ==false) {//If the software settings do not vibrate, then the notification.vibrate =null is not shaken; }else{//otherwise the need for vibration, this time to see how the system is set: no vibration = 0; vibration = 1; Vibrate only in silent mode =2;if (Volmgr.getvibratesetting (audiomanager.vibrate_type_ RINGER) = = Audiomanager.vibrate_setting_off) {//non-vibrating NotiFication.vibrate =null;                            }elseif (volmgr.getvibratesetting (audiomanager.vibrate_type_ringer) = = audiomanager.vibrate_setting_only_silent) {                        Only vibrate notification.vibrate =null when mute;                         }else{//Vibration notification.defaults |= notification.default_vibrate; }}} notification.flags |= notification.flag_show            _lights;//to turn on the light;            Default:break; }    }

  

First, you need to receive a notification parameter
Privatevoid setalarmparams (Notification Notification) {
Audiomanager provides access to volume and ringer mode control.
Audiomanager volmgr = (audiomanager) mappcontext.getsystemservice (Context.audio_service);
Switch (Volmgr.getringermode ()) {//Get system settings for ringtone mode
Case audiomanager.ringer_mode_silent://silent mode with a value of 0, this time does not vibrate, does not ring
Notification.sound =null;
Notification.vibrate =null;
Break
Case audiomanager.ringer_mode_vibrate://Vibration mode with a value of 1, this time shaking, not ringing
Notification.sound =null;
Notification.defaults |= notification.default_vibrate;
Break
Case audiomanager.ringer_mode_normal://General mode with a value of 2, in two cases: 1_ Bell but not vibrate, 2_ Bell + vibrate
Uri RingTone =null;
Get the settings for your software
Sharedpreferences sp = preferencemanager.getdefaultsharedpreferences (Mappcontext);
if (!sp.contains (Systemutil.key_ring_tone)) {//If no configuration file is generated, then both the ringtone and the vibration
Notification.defaults |= notification.default_vibrate;
Notification.defaults |= Notification.default_sound;
}else{
String ringfile = sp.getstring (Systemutil.key_ring_tone, NULL);
if (ringfile==null) {//No value, blank, no ringtone played
Ringtone=null;
}elseif (! Textutils.isempty (Ringfile)) {//have ringtones: 1, default 2 custom, all return a URI
Ringtone=uri.parse (Ringfile);
}
Notification.sound = RingTone;

Boolean vibrate = Sp.getboolean (systemutil.key_new_mail_vibrate,true);
if (vibrate ==false) {//If the software settings do not vibrate, then it will not vibrate.
Notification.vibrate =null;
}else{//Otherwise is needed vibration, this time to see how the system is set: no vibration = 0; vibration = 1; Vibrate only in silent mode = 2;
if (volmgr.getvibratesetting (audiomanager.vibrate_type_ringer) = = Audiomanager.vibrate_setting_off) {
No vibrations.
Notification.vibrate =null;
}elseif (volmgr.getvibratesetting (audiomanager.vibrate_type_ringer) = = audiomanager.vibrate_setting_only_silent) {
Vibrate only when muted
Notification.vibrate =null;
}else{
Vibration
Notification.defaults |= notification.default_vibrate;
}
}
}
Notification.flags |= notification.flag_show_lights;//to turn on the lights
Break
Default
Break
}
}

The concrete implementation is like the code, the annotation is also very clear, wherein sharedpreferences SP = preferencemanager.getdefaultsharedpreferences (Mappcontext); is to obtain configuration information for the software.

Set your own software's ringtone + vibrate in Android

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.