欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android震动与提示音实现代码

程序员文章站 2024-02-26 21:31:46
本文实例为大家分享了android消息提示的具体代码,供大家参考,具体内容如下 protected audiomanager audiomanager; pr...

本文实例为大家分享了android消息提示的具体代码,供大家参考,具体内容如下

protected audiomanager audiomanager;
 protected vibrator vibrator;

 audiomanager = (audiomanager)getsystemservice(context.audio_service); //此方法是由context调用的
 vibrator = (vibrator)getsystemservice(context.vibrator_service); //同上

 /**
   * vibrate and play tone
   */
  public void vibrateandplaytone(emmessage message) {
   if (system.currenttimemillis() - 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) 
      {
         log.e("tag","已经调成静音");
         return;
      }
      long[] pattern = new long[] { 0, 180, 80, 120 };
      vibrator.vibrate(pattern, -1); //震动

      if (ringtone == null) {
         uri notificationuri = ringtonemanager.getdefaulturi(ringtonemanager.type_notification);

         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 bug that the phone will
          // continue ringtone without stop
          // so add below special handler to stop it after 3s if
          // needed
         if (vendor != null && vendor.tolowercase().contains("samsung")) {
           thread ctlthread = new thread() {
             public void run() {
               try {
                 thread.sleep(3000);
                 if (ringtone.isplaying()) {
                   ringtone.stop();
                 }
               } catch (exception e) {
               }
             }
           };
           ctlthread.run();
       }
      
   } catch (exception e) {
     e.printstacktrace();
   }
 }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇: js实现倒计时

下一篇: