android音樂播放器(2)

自定義Notify發送通知,並進行更新,顯示播放進度;自定義menu菜單,設置響應事件ide

  
  
           
  
  
  1. //更新通知  
  2.  private void updateNotify(){  
  3.   Runnable r=new Runnable() {  
  4.    @Override  
  5.    public void run() {  
  6.     if(isStop){  
  7.      return;  
  8.     }  
  9.     SongInfo songInfo=mSongInfos.get(mCurrentIndex);  
  10.     //設置專輯信息      
  11.     mNotify.contentView.setTextViewText(R.id.notify_album_name, mAlbumName);  
  12.     mNotify.contentView.setTextViewText(R.id.notify_song_name, songInfo.getSongName());  
  13.     //獲取歌曲長度  
  14.     String duration=CommonUtil.convertTime(songInfo.getDuration());  
  15.     //獲取當前播放時間  
  16.     String currentTime=CommonUtil.convertTime(mMediaPlayer.getCurrentPosition()/1000);  
  17.     mNotify.contentView.setTextViewText(R.id.notify_current_time,currentTime);  
  18.     mNotify.contentView.setTextViewText(R.id.notify_duration, duration);  
  19.       
  20.     mManager.notify(R.layout.play_notify,mNotify);  
  21.     mHandler.postDelayed(this, 1000);  
  22.    }  
  23.   };  
  24.   mHandler.postDelayed(r, 1000);  
  25.  }  
  26.  //通知  
  27.  public void showNotify(){  
  28.   mNotify = new Notification(R.drawable.no_cd,"正在播放",System.currentTimeMillis());  
  29.   //  
  30.   mNotify.flags|=Notification.FLAG_ONGOING_EVENT;  
  31.   //佈局,getPackage()上下文方法  
  32.   mNotify.contentView=new RemoteViews(getPackageName(),R.layout.play_notify);  
  33.   //通知的圖片不用實時刷新,不然在31秒處會出現異常  
  34.   mNotify.contentView.setImageViewBitmap(R.id.notify_album_icon, mAlbumBitmap);  
  35.   //  
  36.   Intent intent=new Intent(this,PlayActivity1.class);  
  37.   mNotify.contentIntent=PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);  
  38.   mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);    
  39.   //發送通知  
  40.   startForeground(R.layout.play_notify, mNotify);  
  41.  }  


  
  
           
  
  
  1. // 自定義菜單  
  2.  @Override  
  3.  public boolean onCreateOptionsMenu(Menu menu) {  
  4.  
  5.   View parent = findViewById(R.id.home);  
  6.   Log.d("mPopupWindow", parent.toString());  
  7.   if (mPopupWindow.isShowing()) {  
  8.    mPopupWindow.dismiss();  
  9.   } else {  
  10.    mPopupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);  
  11.   }  
  12.   return false;  
  13.  }  
  14. @Override  
  15.  public boolean onKeyDown(int keyCode, KeyEvent event) {  
  16.   if (keyCode == KeyEvent.KEYCODE_BACK) {  
  17.    if (mPopupWindow.isShowing()) {  
  18.     mPopupWindow.dismiss();  
  19.     return true;  
  20.    }  
  21.   }  
  22.   return super.onKeyDown(keyCode, event);  
  23.  }  
相關文章
相關標籤/搜索