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) BaseApp.getAppContext().getSystemService(Context.AUDIO_SERVICE); //此方法是由Context调用的
vibrator = (Vibrator) BaseApp.getAppContext().getSystemService(Context.VIBRATOR_SERVICE); //同上
}
/**
* 开启手机震动和播放系统提示铃声
*/
public void vibrateAndPlayTone() {
if (System.currentTimeMillis() - lastNotificationTime < MIN_TIME_OUT) { //间隔少于4s
return;
}
try {
lastNotificationTime = System.currentTimeMillis();
//声音开启,振动开启:RINGER_MODE_NORMAL
//声音关闭,振动关闭:RINGER_MODE_SILENT
//声音关闭,振动开启:RINGER_MODE_VIBRATE
if (audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT) {
return;
}
long[] pattern = new long[]{0, 1000, 1000, 1000}; // 停止0s 开启1s 停止1s 开启1s
//第二个参数表示使用pattern第几个参数作为震动时间重复震动,如果是-1就震动一次 0一直震动
vibrator.vibrate(pattern, -1);
if (ringtone == null) {
//TYPE_NOTIFICATION:通知的声音的类型。 TYPE_ALARM:闹钟的声音类型。
Uri notificationUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
ringtone = RingtoneManager.getRingtone(BaseApp.getAppContext(), notificationUri);
if (ringtone == null) {
return;
}
}
if (!ringtone.isPlaying()) {
ringtone.play();
//判断手机品牌
String vendor = Build.MANUFACTURER;
Log.d("vendor", "vibrateAndPlayTone: "+vendor);
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();
}
}
}