Android Vibration and cue sound implementation code _android

Source: Internet
Author: User

This example for you to share the Android message prompts the specific code for your reference, the details are as follows

protected Audiomanager Audiomanager;

 protected vibrator Vibrator; Audiomanager = (Audiomanager) getsystemservice (Context.audio_service); This method is vibrator = (vibrator) getsystemservice (Context.vibrator_service) called by the context; Ditto/** * Vibrate and play tone */public void Vibrateandplaytone (Emmessage message) {if (System.currenttim 
    Emillis ()-Lastnotifiytime < 1000) {//received new messages within 2 seconds, skip play ringtone return;
      
      try {lastnotifiytime = System.currenttimemillis (); Check if in silent mode if (audiomanager.getringermode () = = audiomanager.ringer_mode_silent) {Lo
         G.E ("TAG", "has been adjusted to mute");
      Return
      } long[] pattern = new Long[] {0, 180, 80, 120}; Vibrator.vibrate (Pattern,-1); Shake if (ringtone = = null) {Uri Notificationuri = Ringtonemanager.getdefaulturi (ringtonemanager.type_noti

         fication); Ringtone = Ringtonemanager.getringtone (appcontext, Notificationuri);
           if (ringtone = null) {LOG.D (TAG, "Cant find ringtone at:" + notificationuri.getpath ());
         Return
          
         } if (!ringtone.isplaying ()) {//string vendor = Build.manufacturer;
          Ringtone.play ();  For Samsung S3, we meet a bugs that the phone'll//continue ringtone without stop//SO add below Special handler to stop it after 3s if//needed if (vendor!= null && Vendor.tolowercase (). Co
                 Ntains ("Samsung")) {thread ctlthread = new Thread () {public void run () {try {
                 Thread.Sleep (3000);
                 if (ringtone.isplaying ()) {ringtone.stop ();
           The catch (Exception e) {}}};
       Ctlthread.run (); } catch (Exception e) {e.printstacktrAce ();
 }
 }

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.