我的Android心得(5)--响铃、震动、状态栏提示
16lz
2021-01-26
目标:希望在特定情况下,通过响铃、震动、状态栏提示提醒用户
一、响铃
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;@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn null;}@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate();}@Overridepublic void onDestroy() {// TODO Auto-generated method stubif(mMediaPlayer != null){mMediaPlayer.stop();// 要释放资源,不然会打开很多个MediaPlayermMediaPlayer.release();}if(vibrator != null){vibrator.cancel();}super.onDestroy();}/** * 一启动就响铃,震动提醒 */@SuppressLint("NewApi")@SuppressWarnings("deprecation")@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {// TODO Auto-generated method stubContext 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 设为1023return 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 blocke.printStackTrace();} catch (SecurityException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalStateException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
更多相关文章
- Android(安卓)启动异常:The connection to adb is down, and a se
- Android半透明提示效果的实现
- Android(安卓)studio 高级控件提示文本框与下拉框
- android:foreground="?attr/selectableItemBackground" 提示找不
- Android之如何解决Listview里面的值出现了2次
- Android(安卓)Studio Emulator 提示 “/dev/kvm is not found”
- eclipse开发Android程序时一些常见问题与错误提示
- 当ListView为空时显示提示语
- Cocos2d-x 新版本中android震动