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实现倒计时
推荐阅读
-
Android震动与提示音实现代码
-
SpringBoot与Quartz集成实现分布式定时任务集群的代码实例
-
Android实现带有删除按钮的EditText示例代码
-
Android AsyncTask实现机制详细介绍及实例代码
-
Android程序开发之Fragment实现底部导航栏实例代码
-
Android应用中实现选择本地文件与目录的实例分享
-
Android悬浮对话框(即点即关对话框)实现代码
-
Android实现H5与Native交互的两种方式
-
Android中ViewPager实现滑动指示条及与Fragment的配合
-
React-Native Android 与 IOS App使用一份代码实现方法