目标:希望在特定情况下,通过响铃、震动、状态栏提示提醒用户
一、响铃
AudioManager能够调整响铃模式,音量等与音频相关的。
http://blog.sina.com.cn/s/blog_5418969101012yde.html
响铃实际靠MediaPlayer播放特定Uri下的音乐
- // 使用来电铃声的铃声路径
- Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
- // 如果为空,才构造,不为空,说明之前有构造过
- if(mMediaPlayer == null)
- mMediaPlayer = new MediaPlayer();
- mMediaPlayer.setDataSource(context, uri);
- mMediaPlayer.setLooping(true); //循环播放
- mMediaPlayer.prepare();
- mMediaPlayer.start();
二、震动
vibrator
- vibrator = (Vibrator)mContext.getSystemService(mContext.VIBRATOR_SERVICE);
- // 等待3秒,震动3秒,从第0个索引开始,一直循环
- vibrator.vibrate(new long[]{3000, 3000}, 0);
三、状态栏提示
- m_Manager = (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);
- //m_Manager.cancel(1023); //取消
- m_PendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);
- m_Notification = new Notification();
- m_Notification.icon = R.drawable.flyheart; //设置图标
- m_Notification.tickerText = "未下拉提示栏时的浮动内容";
- m_Notification.setLatestEventInfo(mContext, "提示标题", "提示正文", m_PendingIntent);
- m_Manager.notify(1023, m_Notification); // 这个notification 的 id 设为1023
四、代码
- public class RemindService extends Service{
- private MediaPlayer mMediaPlayer = null;
- private Vibrator vibrator;
- private Target target; // 关注对象的信息
- // 状态栏提示要用的
- private NotificationManager m_Manager;
- private PendingIntent m_PendingIntent;
- private Notification m_Notification;
- @Override
- public IBinder onBind(Intent intent) {
- // TODO Auto-generated method stub
- return null;
- }
- @Override
- public void onCreate() {
- // TODO Auto-generated method stub
- super.onCreate();
- }
- @Override
- public void onDestroy() {
- // TODO Auto-generated method stub
- if(mMediaPlayer != null){
- mMediaPlayer.stop();
- // 要释放资源,不然会打开很多个MediaPlayer
- mMediaPlayer.release();
- }
- if(vibrator != null){
- vibrator.cancel();
- }
- super.onDestroy();
- }
- /**
- * 一启动就响铃,震动提醒
- */
- @SuppressLint("NewApi")
- @SuppressWarnings("deprecation")
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- // TODO Auto-generated method stub
- Context mContext = getApplicationContext();
- if(target == null)
- target = new Target(mContext);
- if(target.getInfo(Target.setRemindRing).equals("false") == false){
- AudioManager audioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
- audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
- PlaySound(mContext);
- }
- if(target.getInfo(Target.setRemindVibrate).equals("false") == false){
- vibrator = (Vibrator)mContext.getSystemService(mContext.VIBRATOR_SERVICE);
- // 等待3秒,震动3秒,从第0个索引开始,一直循环
- vibrator.vibrate(new long[]{3000, 3000}, 0);
- }
- // 无论是否震动、响铃,都有状态栏提示
- m_Manager = (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);
- m_Manager.cancel(1023);
- m_PendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);
- m_Notification = new Notification();
- m_Notification.icon = R.drawable.flyheart;
- m_Notification.tickerText = "随时情感助手在呼唤你……";
- m_Notification.setLatestEventInfo(mContext, "随时情感助手", intent.getExtras().getString("remind_kind", ""), m_PendingIntent);
- m_Manager.notify(1023, m_Notification); // 这个notification 的 id 设为1023
- return super.onStartCommand(intent, flags, startId);
- }
- public void PlaySound(final Context context) {
- Log.e("ee", "正在响铃");
- // 使用来电铃声的铃声路径
- Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
- // 如果为空,才构造,不为空,说明之前有构造过
- if(mMediaPlayer == null)
- mMediaPlayer = new MediaPlayer();
- try {
- mMediaPlayer.setDataSource(context, uri);
- mMediaPlayer.setLooping(true); //循环播放
- mMediaPlayer.prepare();
- mMediaPlayer.start();
- } catch (IllegalArgumentException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (SecurityException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IllegalStateException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }