以下代码做到:
1.系统震动
2.消息提示音
3.系统震动伴随提示音
4.相关权限为Nomal Permission,运行在6.0之上不需要动态申请权限
操作:
1.在Menifest.xml中添加权限:
//添加震动权限
<uses-permission android:name="android.permission.VIBRATE"/>
2.在res文件夹下创建raw文件夹(与layout同级),将下载的音频文件放进去,下面附上苹果各种消息提示音下载链接地址:http://sc.chinaz.com/tag_yinxiao/iphone.html
3.将下列代码考进项目中;
import android.app.Service;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Vibrator;
import com.ebig.pharmacyalliance.R;
import java.io.IOException;
/**
* @author Is-Poson
* @time 2017/9/13 11:05
* @desc 提示音 + 手机震动管理类
*/
public class BeeAndVibrateManager {
private static boolean shouldPlayBeep = true;
/**
* @param context Context实例
* @param milliseconds 震动时长 , 单位毫秒
*/
public static void vibrate(Context context, long milliseconds) {
Vibrator vib = (Vibrator) context.getSystemService(Service.VIBRATOR_SERVICE);
vib.vibrate(milliseconds);
}
/**
* @param context Context实例
* @param pattern 自定义震动模式 。数组中数字的含义依次是[静止时长,震动时长,静止时长,震动时长。。。]单位是毫秒
* @param isRepeat true-> 反复震动,false-> 只震动一次
*/
public static void vibrate(Context context, long[] pattern, boolean isRepeat) {
Vibrator vib = (Vibrator) context.getSystemService(Service.VIBRATOR_SERVICE);
vib.vibrate(pattern, isRepeat ? 1 : -1);
}
public static void playBee(final Context context, PlayerCompleteListener listener) {
AudioManager audioService = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
if (audioService.getRingerMode() != AudioManager.RINGER_MODE_NORMAL) {
shouldPlayBeep = false;//检查当前是否是静音模式
}
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer player) {
player.seekTo(0);
}
});
AssetFileDescriptor file = context.getResources().openRawResourceFd(
R.raw.dingding);
try {
mediaPlayer.setDataSource(file.getFileDescriptor(),
file.getStartOffset(), file.getLength());
file.close();
mediaPlayer.setVolume(0, 1);
mediaPlayer.prepare();
} catch (IOException ioe) {
mediaPlayer = null;
}
if (shouldPlayBeep && mediaPlayer != null) {
mediaPlayer.start();
}
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mp.stop();
listener.onCompletion(mp);
}
});
}
public static void playBeeAndVibrate(final Context context, long milliseconds, PlayerCompleteListener listener) {
//震动
vibrate(context, milliseconds);
//提示音
playBee(context, listener);
}
//MediaPlayer播放完毕监听
public interface PlayerCompleteListener {
void onCompletion(MediaPlayer mp);
}
}