自定義Notify發送通知,並進行更新,顯示播放進度;自定義menu菜單,設置響應事件ide
- //更新通知
- private void updateNotify(){
- Runnable r=new Runnable() {
- @Override
- public void run() {
- if(isStop){
- return;
- }
- SongInfo songInfo=mSongInfos.get(mCurrentIndex);
- //設置專輯信息
- mNotify.contentView.setTextViewText(R.id.notify_album_name, mAlbumName);
- mNotify.contentView.setTextViewText(R.id.notify_song_name, songInfo.getSongName());
- //獲取歌曲長度
- String duration=CommonUtil.convertTime(songInfo.getDuration());
- //獲取當前播放時間
- String currentTime=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);
- }
- //通知
- public void showNotify(){
- mNotify = new Notification(R.drawable.no_cd,"正在播放",System.currentTimeMillis());
- //
- mNotify.flags|=Notification.FLAG_ONGOING_EVENT;
- //佈局,getPackage()上下文方法
- mNotify.contentView=new RemoteViews(getPackageName(),R.layout.play_notify);
- //通知的圖片不用實時刷新,不然在31秒處會出現異常
- mNotify.contentView.setImageViewBitmap(R.id.notify_album_icon, mAlbumBitmap);
- //
- Intent intent=new Intent(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
- public boolean onCreateOptionsMenu(Menu menu) {
- View parent = findViewById(R.id.home);
- Log.d("mPopupWindow", parent.toString());
- if (mPopupWindow.isShowing()) {
- mPopupWindow.dismiss();
- } else {
- mPopupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
- }
- return false;
- }
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- if (keyCode == KeyEvent.KEYCODE_BACK) {
- if (mPopupWindow.isShowing()) {
- mPopupWindow.dismiss();
- return true;
- }
- }
- return super.onKeyDown(keyCode, event);
- }