Android--响铃、震动、状态栏提示

目标:希望在特定情况下,通过响铃、震动、状态栏提示提醒用户


一、响铃

AudioManager能够调整响铃模式,音量等与音频相关的。

http://blog.sina.com.cn/s/blog_5418969101012yde.html

响铃实际靠MediaPlayer播放特定Uri下的音乐

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. // 使用来电铃声的铃声路径  
  2. Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);   
  3. // 如果为空,才构造,不为空,说明之前有构造过  
  4. if(mMediaPlayer == null)  
  5.     mMediaPlayer = new MediaPlayer();  
  6. mMediaPlayer.setDataSource(context, uri);  
  7. mMediaPlayer.setLooping(true); //循环播放  
  8. mMediaPlayer.prepare();  
  9. mMediaPlayer.start();  

二、震动

vibrator

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. vibrator = (Vibrator)mContext.getSystemService(mContext.VIBRATOR_SERVICE);  
  2. // 等待3秒,震动3秒,从第0个索引开始,一直循环  
  3. vibrator.vibrate(new long[]{30003000}, 0);  

三、状态栏提示

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. m_Manager = (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);  
  2. //m_Manager.cancel(1023); //取消  
  3. m_PendingIntent = PendingIntent.getActivity(this0new Intent(this, MainActivity.class), 0);  
  4. m_Notification = new Notification();  
  5. m_Notification.icon = R.drawable.flyheart; //设置图标  
  6. m_Notification.tickerText = "未下拉提示栏时的浮动内容";  
  7. m_Notification.setLatestEventInfo(mContext, "提示标题""提示正文", m_PendingIntent);  
  8. m_Manager.notify(1023, m_Notification); // 这个notification 的 id 设为1023  


四、代码

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class RemindService extends Service{  
  2.   
  3.     private MediaPlayer mMediaPlayer = null;  
  4.     private Vibrator vibrator;  
  5.     private Target target; // 关注对象的信息  
  6.   
  7.     // 状态栏提示要用的  
  8.     private NotificationManager m_Manager;  
  9.     private PendingIntent m_PendingIntent;  
  10.     private Notification m_Notification;  
  11.       
  12.     @Override  
  13.     public IBinder onBind(Intent intent) {  
  14.         // TODO Auto-generated method stub  
  15.         return null;  
  16.     }  
  17.     @Override  
  18.     public void onCreate() {  
  19.         // TODO Auto-generated method stub  
  20.         super.onCreate();  
  21.     }  
  22.   
  23.     @Override  
  24.     public void onDestroy() {  
  25.         // TODO Auto-generated method stub  
  26.           
  27.         if(mMediaPlayer != null){  
  28.             mMediaPlayer.stop();  
  29.             // 要释放资源,不然会打开很多个MediaPlayer  
  30.             mMediaPlayer.release();  
  31.         }  
  32.         if(vibrator != null){  
  33.             vibrator.cancel();  
  34.         }  
  35.           
  36.         super.onDestroy();  
  37.   
  38.     }  
  39.   
  40.     /** 
  41.      * 一启动就响铃,震动提醒 
  42.      */  
  43.     @SuppressLint("NewApi")  
  44.     @SuppressWarnings("deprecation")  
  45.     @Override  
  46.     public int onStartCommand(Intent intent, int flags, int startId) {  
  47.         // TODO Auto-generated method stub  
  48.           
  49.         Context mContext = getApplicationContext();  
  50.         if(target == null)  
  51.             target = new Target(mContext);  
  52.           
  53.         if(target.getInfo(Target.setRemindRing).equals("false") == false){  
  54.   
  55.             AudioManager audioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);  
  56.             audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);  
  57.             PlaySound(mContext);  
  58.         }  
  59.           
  60.         if(target.getInfo(Target.setRemindVibrate).equals("false") == false){  
  61.   
  62.             vibrator = (Vibrator)mContext.getSystemService(mContext.VIBRATOR_SERVICE);  
  63.             // 等待3秒,震动3秒,从第0个索引开始,一直循环  
  64.             vibrator.vibrate(new long[]{30003000}, 0);  
  65.         }  
  66.           
  67.         // 无论是否震动、响铃,都有状态栏提示  
  68.         m_Manager = (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);  
  69.         m_Manager.cancel(1023);  
  70.         m_PendingIntent = PendingIntent.getActivity(this0new Intent(this, MainActivity.class), 0);  
  71.         m_Notification = new Notification();  
  72.         m_Notification.icon = R.drawable.flyheart;  
  73.         m_Notification.tickerText = "随时情感助手在呼唤你……";  
  74.         m_Notification.setLatestEventInfo(mContext, "随时情感助手", intent.getExtras().getString("remind_kind"""), m_PendingIntent);  
  75.         m_Manager.notify(1023, m_Notification); // 这个notification 的 id 设为1023  
  76.           
  77.         return super.onStartCommand(intent, flags, startId);  
  78.     }  
  79.   
  80.   
  81.         public  void PlaySound(final Context context) {  
  82.           
  83.             Log.e("ee""正在响铃");  
  84.             // 使用来电铃声的铃声路径  
  85.             Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);   
  86.             // 如果为空,才构造,不为空,说明之前有构造过  
  87.             if(mMediaPlayer == null)  
  88.                 mMediaPlayer = new MediaPlayer();  
  89.             try {  
  90.                 mMediaPlayer.setDataSource(context, uri);  
  91.                 mMediaPlayer.setLooping(true); //循环播放  
  92.                 mMediaPlayer.prepare();  
  93.                 mMediaPlayer.start();  
  94.             } catch (IllegalArgumentException e) {  
  95.                 // TODO Auto-generated catch block  
  96.                 e.printStackTrace();  
  97.             } catch (SecurityException e) {  
  98.                 // TODO Auto-generated catch block  
  99.                 e.printStackTrace();  
  100.             } catch (IllegalStateException e) {  
  101.                 // TODO Auto-generated catch block  
  102.                 e.printStackTrace();  
  103.             } catch (IOException e) {  
  104.                 // TODO Auto-generated catch block  
  105.                 e.printStackTrace();  
  106.             }  
  107.               
  108.         }  
  109.   
  110. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值