Android gets the scene mode and ringtones to achieve vibration, ringing reminders _android

Source: Internet
Author: User

When we want to remind the user by ringing or shaking (similar to the phone call reminder interface), we need to consider the situation pattern of the phone itself. (There is a oppo test mobile phone is found, even if the tone to mute mode, I can still play out the bell), in order to prevent the "supernatural" event, so in the hint before the situation mode to judge so that it is necessary, specially to the code record.

1, get mobile phone situation mode:

Audiomanager Audiomanager = (audiomanager) getsystemservice (context.audio_service);
int ringermode = Audiomanager.getringermode ();

There are three kinds of ringermode, respectively: Audiomanager.ringer_mode_normal (Bell mode), Audiomanager.ringer_mode_silent (silent mode), Audiomanager.ringer_mode_vibrate (Vibration mode)

2. Get the system ringtone URI:

Gets the URI private URI of the system default ringtone
Getsystemdefultringtoneuri () {return
 Ringtonemanager.getactualdefaultringtoneuri (This,ringtonemanager.type_ringtone);
}

3. Play/stop playing system ringtones

/** * Player System sound */private void Startalarm () {//Some hand opportunity creation failed, causing Mmediaplayer to be empty.
 Mmediaplayer = Mediaplayer.create (this, Getsystemdefultringtoneuri ()); if (Mmediaplayer = null) {//Some phone ringtones will create a failure, if the creation fails, play our own ringtones soundpoolutils.playcallwaitingaudio ();//Your own defined bell-tone playback tool class.
  Concrete implementation see below} else {mmediaplayer.setlooping (true);/set loop try {mmediaplayer.prepare ();
  catch (Exception e) {e.printstacktrace ();
 } mmediaplayer.start (); }/** * Stop playing caller voice/private void Stopalarm () {if (Mmediaplayer!= null) {if (mmediaplayer.isplaying ()) {mmed
   Iaplayer.stop ();
   Mmediaplayer.release ();
  Mmediaplayer = null;
} soundpoolutils.stopcallwaitingaudio (); The implementation code for the//soundpoolutils class/** * Sound Playback tool Class (the software must first invoke the Soundpoolutils.init () method to initialize) * "Special attention" System permissions: Media_content_control, Modify_audio_settings, Read_external_storage * @author Blue Pavilion Order */public class Soundpoolutils {//tag has been initialized private
 static Boolean inited = false;
 private static final int max_streams = 10; PrivateStatic Soundpool Soundpool;

 Voice pool private static Sparseintarray Soundmap = new Sparseintarray ();
 /** * Sound recording ready to complete (usually before recording needs to play) index number * * public static final int record_prepared = 1;
 /** * Message successfully sent an audio prompt index number * * * public static final int msg_send = 2;
 /** * The wait tone in voice-connected * * public static final int call_waiting = 3;
 The ID of the waiting sound that is playing is private static int call_playing_id =-1;

 /** * Toggle the group's beep/public static final int group_switch_tip = 4; /** * * @param context/@SuppressWarnings ("deprecation") public static void init (context context) {if Conte
    XT!= NULL) {if (!inited) {soundpool = new Soundpool (max_streams, audiomanager.stream_music, 0);
    Soundmap.append (record_prepared, soundpool.load (context, R.raw.start_record, 1));
    Soundmap.append (Msg_send, soundpool.load (context, r.raw.msg_send, 1));
    Soundmap.append (call_waiting, soundpool.load (context, r.raw.call_waiting, 1)); Soundmap.append (Group_switch_tip, SOUNDPOOl.load (Context, R.raw.group_switch_tip, 1));
   Inited = true;  /** * Playback Recording preparation tone/public static void Playrecordpreaudio () {Soundpool.play (Soundmap.get (record_prepared)),
 1, 1, 0, 0, 1); /** * Playback message was sent successfully (or a recorded beep)/public static void Playmessagesendaudio () {Soundpool.play (Soundmap.get msg_send
 ), 1, 1, 0, 0, 1); /** * Play the wait tone in the voice connection (this sound will loop for a long time, similar to the sound effect of phone calls)/public static void Playcallwaitingaudio () {call_playing_id = Soun
 Dpool.play (Soundmap.get (call_waiting), 1, 1, 0,-1, 1);
  /** * Stop playing the wait tone/public static void Stopcallwaitingaudio () {soundpool.stop (call_playing_id);
 call_playing_id =-1; /** * Player group Toggle successful beep/public static void Playgroupswitchtipaudio () {Soundpool.play (Soundmap.get group_switch_t
 IP), 1, 1, 0, 0, 1);
 }
}

4, open/close vibration "Don't forget the vibration system permissions: <uses-permission android:name=" Android.permission.VIBRATE "/>"

 /**
  * Open Vibration
  /
 private void Startvibrate () {
  if (vibrator = null) {
  //Get vibration service
   vibrator = ( Vibrator) Getsystemservice (Context.vibrator_service);
  }
  Vibration Mode 1 seconds vibration 1.4 seconds
  long[] pattern = {1000, 1400};
  The vibrations are repeated, starting at 0 of the array (-1 means not repeating)
  vibrator.vibrate (pattern, 0);
 }

 /**
  * Stop vibration */
 private void Stopviberate () {
  if (vibrator!= null) {
   vibrator.cancel ();
  }
 }

5. Create scene mode change broadcast recipient listener

 Scenario mode switching broadcast recipient private Broadcastreceiver Broadcastreceiver = new Broadcastreceiver () {@Override public void onrece
     Ive (context, Intent Intent) {//If it is a broadcast if the story mode switch (AudioManager.RINGER_MODE_CHANGED_ACTION.equals Intent
    . Getaction ())) {Audiomanager am = (audiomanager) getsystemservice (Context.audio_service);
Handleringmode (Am.getringermode ());//do different processing for the story Mode}}; /** * Processing Scenario Mode * * @param ringermode/private void Handleringmode (int ringermode) {stopalarm ();/Turn off ringtones STOPV
   Iberate ()//close shock switch (ringermode) {case audiomanager.ringer_mode_normal://Standard mode (ringtones and vibrations) startalarm ();
   Startvibrate ();
  Break
  Case audiomanager.ringer_mode_silent://Mute mode break;
   Case audiomanager.ringer_mode_vibrate://Vibration Mode "open vibration do not forget the system vibration permission" startvibrate ();
  Break
  Default:break; }/** * Listener scenario Switching (registration scenario mode Change broadcast receiver)/private void ringermodelistening () {Intentfilter intentfilter = new Intent
  Filter (); Intentfilter.addaction (AudiOmanager.ringer_mode_changed_action);
 Registerreceiver (Broadcastreceiver, Intentfilter);
 }

6. Cancellation of registered broadcast receivers at the time of activity extinction

@Override
 protected void OnDestroy () {
  //cancellation of registered broadcast receiver Listener
  unregisterreceiver (broadcastreceiver);
  Super.ondestroy ();
 }


The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.