Android震动与提示音实现代码
本文实例为大家分享了android消息提示的具体代码,供大家参考,具体内容如下
protectedAudioManageraudioManager; protectedVibratorvibrator; audioManager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);//此方法是由Context调用的 vibrator=(Vibrator)getSystemService(Context.VIBRATOR_SERVICE);//同上 /** *vibrateandplaytone */ publicvoidvibrateAndPlayTone(EMMessagemessage){ if(System.currentTimeMillis()-lastNotifiyTime<1000){ //receivednewmessageswithin2seconds,skipplayringtone return; } try{ lastNotifiyTime=System.currentTimeMillis(); //checkifinsilentmode if(audioManager.getRingerMode()==AudioManager.RINGER_MODE_SILENT) { Log.e("TAG","已经调成静音"); return; } long[]pattern=newlong[]{0,180,80,120}; vibrator.vibrate(pattern,-1);//震动 if(ringtone==null){ UrinotificationUri=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); ringtone=RingtoneManager.getRingtone(appContext,notificationUri); if(ringtone==null){ Log.d(TAG,"cantfindringtoneat:"+notificationUri.getPath()); return; } } if(!ringtone.isPlaying()){ //Stringvendor=Build.MANUFACTURER; ringtone.play(); //forsamsungS3,wemeetabugthatthephonewill //continueringtonewithoutstop //soaddbelowspecialhandlertostopitafter3sif //needed if(vendor!=null&&vendor.toLowerCase().contains("samsung")){ ThreadctlThread=newThread(){ publicvoidrun(){ try{ Thread.sleep(3000); if(ringtone.isPlaying()){ ringtone.stop(); } }catch(Exceptione){ } } }; ctlThread.run(); } }catch(Exceptione){ e.printStackTrace(); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。