android提示音资源,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();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值