android音乐播放器(2)
16lz
2021-01-24
自定义Notify发送通知,并进行更新,显示播放进度;自定义menu菜单,设置响应事件
- //更新通知
- privatevoidupdateNotify(){
- Runnabler=newRunnable(){
- @Override
- publicvoidrun(){
- if(isStop){
- return;
- }
- SongInfosongInfo=mSongInfos.get(mCurrentIndex);
- //设置专辑信息
- mNotify.contentView.setTextViewText(R.id.notify_album_name,mAlbumName);
- mNotify.contentView.setTextViewText(R.id.notify_song_name,songInfo.getSongName());
- //获取歌曲长度
- Stringduration=CommonUtil.convertTime(songInfo.getDuration());
- //获取当前播放时间
- StringcurrentTime=CommonUtil.convertTime(mMediaPlayer.getCurrentPosition()/1000);
- mNotify.contentView.setTextViewText(R.id.notify_current_time,currentTime);
- mNotify.contentView.setTextViewText(R.id.notify_duration,duration);
- mManager.notify(R.layout.play_notify,mNotify);
- mHandler.postDelayed(this,1000);
- }
- };
- mHandler.postDelayed(r,1000);
- }
- //通知
- publicvoidshowNotify(){
- mNotify=newNotification(R.drawable.no_cd,"正在播放",System.currentTimeMillis());
- //
- mNotify.flags|=Notification.FLAG_ONGOING_EVENT;
- //布局,getPackage()上下文方法
- mNotify.contentView=newRemoteViews(getPackageName(),R.layout.play_notify);
- //通知的图片不用实时刷新,否则在31秒处会出现异常
- mNotify.contentView.setImageViewBitmap(R.id.notify_album_icon,mAlbumBitmap);
- //
- Intentintent=newIntent(this,PlayActivity1.class);
- mNotify.contentIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
- mManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
- //发送通知
- startForeground(R.layout.play_notify,mNotify);
- }
- //自定义菜单
- @Override
- publicbooleanonCreateOptionsMenu(Menumenu){
- Viewparent=findViewById(R.id.home);
- Log.d("mPopupWindow",parent.toString());
- if(mPopupWindow.isShowing()){
- mPopupWindow.dismiss();
- }else{
- mPopupWindow.showAtLocation(parent,Gravity.BOTTOM,0,0);
- }
- returnfalse;
- }
- @Override
- publicbooleanonKeyDown(intkeyCode,KeyEventevent){
- if(keyCode==KeyEvent.KEYCODE_BACK){
- if(mPopupWindow.isShowing()){
- mPopupWindow.dismiss();
- returntrue;
- }
- }
- returnsuper.onKeyDown(keyCode,event);
- }
本文出自 “android” 博客,谢绝转载!
更多相关文章
- Android学习07-----事件处理(2)单选按钮与下拉列表框级联菜单
- Android判断某一应用是否正在运行与判断某一Service是否正在运行
- 多条未接来电只显示来电数,同时拨号应用通知小圆点一直显示
- Android真机调试不打印日志解决
- android JiaoZiVideoPlayer问题整理解析
- React Native开发error calling appregistry.runapplication的解
- Android:Eclipse如何设置格式化Java代码
- Android(安卓)4.1正式发布 十大新特性
- android自定义Spinner下拉菜单(下拉列表框)样式