protectedAudiomanager Audiomanager;protectedvibrator Vibrator; Audiomanager=(Audiomanager) Getsystemservice (Context.audio_service); //This method is called by the context vibrator=(Vibrator) Getsystemservice (Context.vibrator_service); //Ibid ./*** Vibrate and play tone*/ Public voidvibrateandplaytone (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", "has been muted"); return; } Long[] pattern =New Long[] {0, 180, 80, 120 }; Vibrator.vibrate (Pattern,-1); //Vibration 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 bugs that the phone would//continue ringtone without stop//So add below special handler-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 (); } }
Android message alert (vibrate and beep)