android开发分享android 震动和提示音的实现代码

android的消息提示(震动和提示音),实现代码如下所示: public class vibratorutil { protected audiomanager

android的消息提示(震动和提示音),实现代码如下所示:

  public class vibratorutil {    protected audiomanager audiomanager;    protected vibrator vibrator;    private ringtone ringtone;    private static final int min_time_out = 4000; //时间间隔    long lastnotificationtime;    public vibratorutil() {      audiomanager = (audiomanager) myapp.getcontext().getsystemservice(context.audio_service); //此方法是由context调用的      vibrator = (vibrator) myapp.getcontext().getsystemservice(context.vibrator_service); //同上    }    /**     * 开启手机震动和播放系统提示铃声     */    public void vibrateandplaytone() {      if (system.currenttimemillis() - lastnotificationtime < min_time_out) {        return;      }      try {        lastnotificationtime = system.currenttimemillis();        if (audiomanager.getringermode() == audiomanager.ringer_mode_silent) {          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(myapp.getcontext(), notificationuri);          if (ringtone == null) {            return;          }        }        if (!ringtone.isplaying()) {          ringtone.play();          //判断手机品牌          string vendor = build.manufacturer;          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();      }    }  }

总结

以上所述是小编给大家介绍的android 震动和提示音的实现代码,希望对大家有所帮助

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/addevelopment/899763.html

(0)
上一篇 2021年10月23日
下一篇 2021年10月23日

精彩推荐