音乐播放器:添加了广播和后台服务、通知

public class NewKugouMainActivity extends Activity implements OnClickListener,
		OnSeekBarChangeListener {

	private ImageView mUpMusicBtn, mStartAndSuspendMusicBtn, mNextMusicBtn,
			mListMusicBtn;
	public TextView mTitleMusicNameText;
	private TextView mCurrentTimeText;
	private TextView mAllTimeText;
	private SeekBar mSeekBar;
	private Handler mHander = new Handler();

	private boolean timeSwitch = true;

	// 弹出歌曲列表
	private PopupWindow mPopupWindom;
	private LayoutInflater mInflater;
	// SD卡的目录
	private File allFile = Environment.getExternalStorageDirectory();
	// 装遍历出来的音乐文件的名字
	private List<File> fileName = new ArrayList<File>();
	private ArrayList<MusicWayBean> musicBeanList = new ArrayList<MusicWayBean>();

	// 装遍历出来的音乐文件的名字,是分割过后的音乐文件名字,没有后缀名
	private ArrayList<String> sdcardMusicName = new ArrayList<String>();

	// 在点击ListView中会初次实例化,是当前选择的ListView中的哪一项,用于上一首,下一首,和名字数组中取名字,会在点击上一首,下一首中再次实例化
	private int checkMusicNumber = -1;
	// 服务定义的接口
	private ICopyMusicService mIMusicService;
	private ServiceConnection mConnection = new ServiceConnection() {
		public void onServiceDisconnected(ComponentName name) {

		}

		public void onServiceConnected(ComponentName name, IBinder service) {
			mIMusicService = (ICopyMusicService) service;

		}
	};

	String mDivicionUpName;// 点击上一首时取得上一首歌曲的名字
	String mDivicionNextName;// 点击下一首时取得下一首歌曲的名字
	String mNotificationName;// 通知处的音乐名字
	String blankString;
	// ---------界面销毁后,点击通知传进来当前歌曲的时间--------------
	private int mAllNotifiTime;
	private int mCurrentNotifiTime;

	// ----------活动法广播-------------
	public final String ACTIVITY_UP_CLICK = "com.kugou.activity.up_click_musicbtn";
	public final String ACTIVITY_NEXT_CLICK = "com.kugou.activity.next_click_musicbtn";
	public final String ACTIVITY_START_AND_SUSPEND_CLICK = "com.kugou.activity.start_and_suspend_click_musicbtn";
	public final String ACTIVITY_LISTVIEW_CLICK = "com.kugou.activity.listView_click_musicbtn";
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_kugou_music);

		getFileWithMusic(allFile);// 遍历文件获取音乐文件
		example();// 实例化控件

		mStartAndSuspendMusicBtn
				.setImageResource(R.drawable.kg_lock_screen_play_button_pressed);

	

		Intent serviceIntent = new Intent(NewKugouMainActivity.this,
				MusicService.class);
		Log.i("11","启动服务");
		startService(serviceIntent);
		bindService(serviceIntent, mConnection, BIND_AUTO_CREATE);
		
		new Thread(new MusicRefrshTime()).start();
		
		mListMusicBtn.setOnClickListener(this);
		mUpMusicBtn.setOnClickListener(this);
		mStartAndSuspendMusicBtn.setOnClickListener(this);
		mNextMusicBtn.setOnClickListener(this);
		mSeekBar.setOnSeekBarChangeListener(this);
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.new_kuguo_up_music_btn:
			Log.i("11", "检测上一曲前>>>"+checkMusicNumber);
			if (checkMusicNumber < 0) {
				Toast.makeText(this, "当前没有音乐可以播放", 0).show();
				return;
			} else {
				upMusic(checkMusicNumber);
			}
			break;
		case R.id.new_kuguo_start_and_suspend_music_btn:
			if (checkMusicNumber < 0) {
				Toast.makeText(this, "当前没有音乐可以播放", 0).show();
				return;
			} else {
				boolean falg = mIMusicService.iStartAndSuspendMusic();
				startORsuspendMusic(falg);

			}
			break;
		case R.id.new_kuguo_next_music_btn:
			Log.i("11", "检测下一曲前>>>"+checkMusicNumber);
			if (checkMusicNumber < 0) {
				Toast.makeText(this, "当前没有音乐可以播放", 0).show();
				return;
			} else {
				nextMusic(checkMusicNumber);
			}
			break;
		case R.id.new_kugou_list_music_imagebtn:
			if (!isUseable()) {// 判断SD卡是否能用
				Toast.makeText(NewKugouMainActivity.this, "外部存储不可用",
						Toast.LENGTH_SHORT).show();
				return;
			} else {
				listMusicForPopupWindom();
			}
			break;
		}
	}

	/**
	 * 实例化控件
	 */
	public void example() {
		mUpMusicBtn = (ImageView) findViewById(R.id.new_kuguo_up_music_btn);
		mStartAndSuspendMusicBtn = (ImageView) findViewById(R.id.new_kuguo_start_and_suspend_music_btn);
		mNextMusicBtn = (ImageView) findViewById(R.id.new_kuguo_next_music_btn);
		mListMusicBtn = (ImageView) findViewById(R.id.new_kugou_list_music_imagebtn);

		mTitleMusicNameText = (TextView) findViewById(R.id.new_music_name_text);
		mAllTimeText = (TextView) findViewById(R.id.new_kuguo_all_time_text);
		mCurrentTimeText = (TextView) findViewById(R.id.new_kuguo_current_time_text);

		mSeekBar = (SeekBar) findViewById(R.id.new_kuguo_seekbar);
		mInflater = LayoutInflater.from(this);
	}

	/**
	 * 点击Title最右边的按钮弹出音乐列表窗口,用PopupWindom来实现
	 */
	public void listMusicForPopupWindom() {
		// 高度要适当的设置
		mPopupWindom = new PopupWindow(getListViewForPopupWindom(), 700, 500);
		mPopupWindom.setFocusable(true);// 此方法可以保证PopupWindom里面嵌套的ListView有点击响应
		// 下面这三行代码可以让你点击空白处来关闭PopWindow 弹出的窗口
		mPopupWindom.setTouchable(true);
		mPopupWindom.setOutsideTouchable(true);
		mPopupWindom.setBackgroundDrawable(new BitmapDrawable());
		mPopupWindom.getContentView().setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				mPopupWindom.setFocusable(false);// 失去焦点
				mPopupWindom.dismiss();// 消除pw
				return true;
			}
		});

		// 点击按钮就弹出窗口
		mListMusicBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (mPopupWindom.isShowing()) {// 判断窗口是否已经弹出弹出
					mPopupWindom.dismiss();// 关闭窗口
				} else {
					mPopupWindom.showAsDropDown(v);// 弹出窗口
				}

			}
		});
	}

	/**
	 * 把装有ListView的布局返回给PopupWindom的实例化中,才有弹窗的布局类型
	 */
	public View getListViewForPopupWindom() {
		View v = mInflater.inflate(R.layout.copykugou_listview_music_layout,
				null);
		ListView mListview = (ListView) v
				.findViewById(R.id.copykugou_listview_to_musicname);
		TextView allMusicPopupText = (TextView) v
				.findViewById(R.id.popupwindow_allmusic_to_text);
		allMusicPopupText.setText("总共" + fileName.size() + "首歌曲");
		ListMusicAdapter listBaseadapter = new ListMusicAdapter(this);
		mListview.setAdapter(listBaseadapter);
		listBaseadapter.setData(fileName);
		// 点击事件,就启动服务,和添加歌曲的名字
		mListview.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				ListMusicAdapter backAdapter = (ListMusicAdapter) parent
						.getAdapter();
				File fileCheckMusic = (File) backAdapter.getItem(position);

				String backPrepareMusic = fileCheckMusic.getPath();

				Log.i("11",position+""+musicBeanList);
				// --------发送广播-传递数组(地址、名字)和当前的选项------------
				listSendBroacast(position,musicBeanList);
				
						
				mStartAndSuspendMusicBtn
						.setImageResource(R.drawable.kg_lock_screen_pause_button_pressed);

				// 点击就初始化
				checkMusicNumber = position;
				// 分割后缀名,并显示当前点击的歌曲名字
				String divisionNameAfter = divisionMusicName(fileCheckMusic
						.getName());

				mTitleMusicNameText.setText(divisionNameAfter);
				// PopupWindow关闭
				mPopupWindom.dismiss();
			}
		});

		return v;
	}
	
	/**
	 * 点击listview也发广播,传递当前点击的某一项,和音乐列表
	 * @param position
	 * @param ListMusicBeanList
	 */
	public void listSendBroacast(int positions,
			ArrayList<MusicWayBean> ListMusicBeanList) {

		Intent activityBroadcastIntent = new Intent(ACTIVITY_LISTVIEW_CLICK);
		activityBroadcastIntent.putExtra("LIST_V_CLICK_POSITON", positions);
		activityBroadcastIntent.putParcelableArrayListExtra(
				"LIST_V_CLICK_MUSICLIST", ListMusicBeanList);

		sendBroadcast(activityBroadcastIntent);
		Log.i("11","发送成功");
	}

	/**
	 * 无限遍历一个文件夹,找出音乐文件,并添加到List里面,然后设置给适配器
	 */
	public void getFileWithMusic(File fileAddress) {
		if (fileAddress != null) {
			File[] fileList = fileAddress.listFiles();
			if (fileList != null) {
				for (File f : fileList) {
					if (f.isDirectory()) {
						getFileWithMusic(f);
						
					} else {
						String s = f.getName();
						if (s.endsWith(".mp3") || s.endsWith(".m4a")
								|| s.endsWith(".amr")) {
							fileName.add(f);// 添加的是地址
							String namePostfix = divisionMusicName(s);// 返回的没有后缀名的音乐文件名

							MusicWayBean bean = new MusicWayBean();

							bean.setMusicPathBean("file://" + f.getPath());
							bean.setMusicNameBean(namePostfix);

							musicBeanList.add(bean);

							sdcardMusicName.add(namePostfix);
						}
					}
				}
			}
		}
	}

	/**
	 * 判断SD卡是否存在 返回的是true:存在
	 * 
	 * @return
	 */
	public boolean isUseable() {
		String state = Environment.getExternalStorageState();
		if (Environment.MEDIA_MOUNTED.equals(state)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 返回没有后缀名的音乐名字
	 */
	public String divisionMusicName(String s) {
		String[] names = s.split("[.]");
		return names[0];
	}

	/**
	 * 界面销毁时,就取消用户交互的连接
	 */
	protected void onDestroy() {
		super.onDestroy();
		Log.i("11", "销毁界面");
		if (mConnection != null && mIMusicService != null) {
			unbindService(mConnection);
			mConnection = null;
			timeSwitch = false;
		}

	}

	/**
	 * 上一首歌曲
	 */
	public void upMusic(int nowNumb) {
		Log.i("11", "检测上一曲前>>>"+checkMusicNumber);
		if (nowNumb - 1 < 0) {// 数组从0开始计数
			Toast.makeText(this, "已经是第一个了", 0).show();

			nowNumb = 0;// 再一次赋值,更新
		} else {
			// 传入新的歌曲播放地址
			mIMusicService.iUpMusic("File://" + fileName.get(nowNumb - 1));// 传入歌曲,
			// 设置title上面显示的名字
			mDivicionUpName = divisionMusicName(sdcardMusicName
					.get(nowNumb - 1));
			// Title处显示当前播放歌曲的名字
			mTitleMusicNameText.setText(mDivicionUpName);

			Log.i("11", "上一首的活动类>>" + checkMusicNumber);
			activityBroadcastReceiver(ACTIVITY_UP_CLICK,
					"activity_up_click_music", blankString, checkMusicNumber,
					true);
			checkMusicNumber = nowNumb - 1;// 再一次赋值,更新
		}
	}

	/**
	 * 暂停会播放更改图片按钮
	 */
	public void startORsuspendMusic(boolean fals) {

		if (fals == true) {
			mStartAndSuspendMusicBtn
					.setImageResource(R.drawable.kg_lock_screen_play_button_pressed);
			activityBroadcastReceiver(ACTIVITY_START_AND_SUSPEND_CLICK,
					blankString, "activity_startandsuspend_click_music", 0,
					true);
		} else {
			mStartAndSuspendMusicBtn
					.setImageResource(R.drawable.kg_lock_screen_pause_button_pressed);
			activityBroadcastReceiver(ACTIVITY_START_AND_SUSPEND_CLICK,
					blankString, "activity_startandsuspend_click_music", 0,
					false);

		}
	}

	/**
	 * 下一首歌曲
	 */
	public void nextMusic(int naexClickNumb) {
		if (naexClickNumb + 1 < fileName.size()) {// 数组从0开始计数

			// 传入新的歌曲播放地址
			mIMusicService.iNextMusic("File://"
					+ fileName.get(naexClickNumb + 1));// 传入歌曲,
			// 设置title上面显示的名字
			mDivicionNextName = divisionMusicName(sdcardMusicName
					.get(naexClickNumb + 1));
			mTitleMusicNameText.setText(mDivicionNextName);

			checkMusicNumber = naexClickNumb + 1;// 再一次赋值,更新
			Log.i("11", "下一首的活动类>>" + checkMusicNumber);
			activityBroadcastReceiver(ACTIVITY_NEXT_CLICK,
					"activity_next_click_music", blankString, checkMusicNumber,
					true);
		} else {
			Toast.makeText(this, "已经是最后一个了", 0).show();
			naexClickNumb = fileName.size();
		}
	}

	/**
	 * 刷新进度条和文本书的时间
	 * 
	 * @author Administrator
	 *
	 */
	public class MusicRefrshTime implements Runnable {

		public void run() {
			while (timeSwitch) {
				if (mIMusicService != null) {

					mHander.post(new Runnable() {
						public void run() {

							mAllNotifiTime = mIMusicService.iAllTimeMusic();
							mCurrentNotifiTime = mIMusicService
									.iCurrentTimeMusic();

							mSeekBar.setMax(mAllNotifiTime);
							mSeekBar.setProgress(mCurrentNotifiTime);

							// 设置总的时间
							int allTimePoint = (mAllNotifiTime / 1000) / 60;
							int allTimeSec = (mAllNotifiTime / 1000) % 60;
							if (allTimePoint < 10 && allTimeSec < 10) {
								mAllTimeText.setText("0" + allTimePoint + ":0"
										+ allTimeSec);
							} else if (allTimePoint < 10 && allTimeSec >= 10) {
								mAllTimeText.setText("0" + allTimePoint + ":"
										+ allTimeSec);
							} else if (allTimePoint > 10 && allTimeSec < 10) {
								mAllTimeText.setText(allTimePoint + ":0"
										+ allTimeSec);
							} else if (allTimePoint > 10 && allTimeSec >= 10) {
								mAllTimeText.setText(allTimePoint + ":0"
										+ allTimeSec);
							}

							// 设置当前的时间
							int currentPoint = (mCurrentNotifiTime / 1000) / 60;
							int currentSec = (mCurrentNotifiTime / 1000) % 60;
							if (currentPoint < 10 && currentSec < 10) {
								mCurrentTimeText.setText("0" + currentPoint
										+ ":0" + currentSec);
							} else if (currentPoint < 10 && currentSec >= 10) {
								mCurrentTimeText.setText("0" + currentPoint
										+ ":" + currentSec);
							} else if (currentPoint > 10 && currentSec < 10) {
								mCurrentTimeText.setText(currentPoint + ":0"
										+ currentSec);
							} else if (currentPoint > 10 && currentSec >= 10) {
								mCurrentTimeText.setText(currentPoint + ":"
										+ currentSec);
							}

//							if (allTimePoint == currentPoint
//									&& allTimeSec == currentSec) {
//								nextMusic(checkMusicNumber);
//							}

						}
					});
					try {
						// 设置刷新的时间
						Thread.sleep(1000 - (mCurrentNotifiTime % 1000));
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}

	// ------------进度条的控制---------------
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		// fromUser判断是用户改变的滑块的值,拖动SeekBar的监听并响应
		if (fromUser == true && mIMusicService != null) {
			mIMusicService.iSeekBarcheck(progress);
		}
	}

	// 该方法拖动进度条开始拖动的时候调用。
	public void onStartTrackingTouch(SeekBar seekBar) {

	}

	// 该方法拖动进度条停止拖动的时候调用
	public void onStopTrackingTouch(SeekBar seekBar) {

	}

	/**
	 * 界面点击上一首、(暂停、播放)、下/上一首发广播专用,暂停专用, (广播名、接收具体的对应,数组中已经跳到第几曲歌的下标,暂停按钮专用) 例如:
	 * activityBroadcastReceiver
	 * (ACTIVITY_NEXT_CLICK,"activity_next_click_music"
	 * ,str,checkMusicNumber,true); 你要传不同的参数时,就需要不同的标签,否则会被覆盖
	 */
	public void activityBroadcastReceiver(String activityBroadcast,
			String upAndNextClickLable, String startAndSuspend,
			int activityClickNumber, boolean temp) {
				
		Intent activityBroadcastIntent = new Intent(activityBroadcast);
		activityBroadcastIntent.putExtra(upAndNextClickLable,
				activityClickNumber);
		activityBroadcastIntent.putExtra(startAndSuspend, temp);
		sendBroadcast(activityBroadcastIntent);
	}

	// -----------注册接收通知的广播---------------
	public final String ACTION_UP_MUSIC = "com.kugou.up_music";
	public final String ACTION_START_SUSPEND_MUSIC = "com.kugou.start_suspend_music";
	public final String ACTION_NEXT_MUSIC = "com.kugou.next_music";

	/**
	 * 接收广播改变音乐的名字和下面暂停、开始的 按钮的图片
	 */
	BroadcastReceiver notifiReceiver = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (action.equals(ACTION_UP_MUSIC)) {// 上一首
				int notifiUpTime = intent
						.getIntExtra("UP_MUSIC_TIME_NOTIFI", 0);
				// 设置title上面显示的名字
				mDivicionUpName = divisionMusicName(sdcardMusicName
						.get(notifiUpTime));
				// Title处显示当前播放歌曲的名字
				mTitleMusicNameText.setText(mDivicionUpName);
				checkMusicNumber = notifiUpTime;
			} else if (action.equals(ACTION_START_SUSPEND_MUSIC)) {

				boolean notifiSwitch = intent.getBooleanExtra(
						"SWITCH_START_SUSPEND_NOTIFI", false);
				startORsuspendMusic(!notifiSwitch);

			} else if (action.equals(ACTION_NEXT_MUSIC)) {// 下一首
				int notifiNextTime = intent.getIntExtra(
						"NEXT_MUSIC_TIME_NOTIFI", 0);
				// 设置title上面显示的名字
				mDivicionNextName = divisionMusicName(sdcardMusicName
						.get(notifiNextTime));
				mTitleMusicNameText.setText(mDivicionNextName);
				checkMusicNumber = notifiNextTime;// 再一次赋值,更新
			}
		}
	};

	protected void onResume() {
		super.onResume();
		IntentFilter filter = new IntentFilter();
		filter.addAction(ACTION_UP_MUSIC);// 通知的 上一首
		filter.addAction(ACTION_START_SUSPEND_MUSIC);// 通知的 暂停、开始
		filter.addAction(ACTION_NEXT_MUSIC);// 通知的下一首
		
		registerReceiver(notifiReceiver, filter);

	};

	protected void onPause() {
		super.onPause();
		if (notifiReceiver != null) {
			unregisterReceiver(notifiReceiver);
			notifiReceiver = null;
		}
	}

}

我把通知放到了服务里面:

 

public class MusicService extends Service {
	NotificationManager manger;
	int notifinumber = 1;// 版本更新用
	// --------动态注册广播>>通知的广播--------------
	public final String ACTION_UP_MUSIC = "com.kugou.up_music";
	public final String ACTION_START_SUSPEND_MUSIC = "com.kugou.start_suspend_music";
	public final String ACTION_NEXT_MUSIC = "com.kugou.next_music";
	public final String ACTION_CLEAN_NOTIFICATION = "com.kugou.clean_notification";
	// -----------活动发出的广播----------
	public final String ACTIVITY_UP_CLICK = "com.kugou.activity.up_click_musicbtn";
	public final String ACTIVITY_NEXT_CLICK = "com.kugou.activity.next_click_musicbtn";
	public final String ACTIVITY_START_AND_SUSPEND_CLICK = "com.kugou.activity.start_and_suspend_click_musicbtn";
	public final String ACTIVITY_LISTVIEW_CLICK = "com.kugou.activity.listView_click_musicbtn";

	private ArrayList<MusicWayBean> musicBeanList;// 接收带有地址和名字的数组
	private int checknumber;// 接收传递点击的那一项,也可以作为每次点击更换的当前选值

	private MediaPlayer mediaPlay = new MediaPlayer();
	private MusicWayBean mBeanMusicList;
	private String musicPath;
	private String musicName;
	private boolean mNitifiSwitch = false;

	/**
	 * 定义上一首、下一首、暂停,开始的接口
	 */
	public interface ICopyMusicService {
		/**
		 * 上一首歌曲
		 */
		public void iUpMusic(String upmusic);

		/**
		 * 下一首歌曲
		 */
		public void iNextMusic(String nextmusic);

		/**
		 * 暂停、播放歌曲
		 */
		public boolean iStartAndSuspendMusic();

		/**
		 * 返回当前歌曲的总时间
		 */
		public int iAllTimeMusic();

		/**
		 * 返回当前歌曲的正在播放时间
		 */
		public int iCurrentTimeMusic();

		/**
		 * 进度条拖动的时间监听
		 */
		public void iSeekBarcheck(int checknumb);
	}

	/**
	 * 实现自己定义的接口和binder对象
	 */

	public class MusicBinderService extends Binder implements ICopyMusicService {

		public void iUpMusic(String upmusic) {
			musicMediaPlayerPrepare(upmusic);

		}

		public void iNextMusic(String nextmusic) {
			musicMediaPlayerPrepare(nextmusic);

		}

		public boolean iStartAndSuspendMusic() {
			return startAndSuspendMusic();
		}

		public int iAllTimeMusic() {
			if (mediaPlay != null) {
				return mediaPlay.getDuration();
			} else {
				return 0;
			}
		}

		public int iCurrentTimeMusic() {
			if (mediaPlay != null) {
				return mediaPlay.getCurrentPosition();
			} else {
				return 0;
			}

		}

		public void iSeekBarcheck(int checkNumb) {
			seekBarCheck(checkNumb);

		}

	}

	MusicBinderService mMusicBinderService = new MusicBinderService();

	public void onCreate() {
		super.onCreate();

	}

	public int onStartCommand(Intent intent, int flags, int startId) {
		if (intent != null) {
			mediaPlay.reset();

			IntentFilter filter = new IntentFilter();
			filter.addAction(ACTION_UP_MUSIC);// 通知的上一首
			filter.addAction(ACTION_START_SUSPEND_MUSIC);// 通知的 暂停、开始
			filter.addAction(ACTION_NEXT_MUSIC);// 通知的下一首
			filter.addAction(ACTION_CLEAN_NOTIFICATION);// 通知的 清除通知

			filter.addAction(ACTIVITY_UP_CLICK);// 活动类发广播的上一首
			filter.addAction(ACTIVITY_START_AND_SUSPEND_CLICK);// 活动类发广播的暂停、开始广播
			filter.addAction(ACTIVITY_NEXT_CLICK);// 活动发广播的下一首
			filter.addAction(ACTIVITY_LISTVIEW_CLICK);// 活动发广播的点击ListView播放音乐后

			registerReceiver(notifiReceiver, filter);
			// createNotification();// 启动 刷新通知
		}
		return super.onStartCommand(intent, flags, startId);
	}

	public IBinder onBind(Intent intent) {
		return mMusicBinderService;
	}

	public boolean onUnbind(Intent intent) {
		return super.onUnbind(intent);
	}

	public void onDestroy() {
		super.onDestroy();
		if (mediaPlay != null) {
			if (mediaPlay.isPlaying()) {
				mediaPlay.stop();
			}
			mediaPlay.release();
			mediaPlay = null;
		}

	}

	/**
	 * 启动音乐播放器,要恶魔暂停,要么播放
	 */

	public boolean startAndSuspendMusic() {
		if (!mediaPlay.isPlaying()) {
			mediaPlay.start();// 播放
			return false;
		} else {
			mediaPlay.pause();// 暂停
			return true;
		}
	}

	/**
	 * 播放器里面歌曲的准备阶段
	 */
	public void musicMediaPlayerPrepare(String s) {
		try {
			if (mediaPlay == null) {
				mediaPlay = new MediaPlayer();
			}
			mediaPlay.reset();

			mediaPlay.setDataSource(s);
			mediaPlay.prepare();
			mediaPlay.start();

		} catch (IllegalArgumentException e) {
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void seekBarCheck(int n) {
		mediaPlay.seekTo(n);
	}

	/**
	 * 启动一个通知
	 */
	public void createNotification() {
		// ----------------状态上提示消息-----------------
		NotificationCompat.Builder builder = new NotificationCompat.Builder(
				this);
		builder.setSmallIcon(R.drawable.common_m54);
		builder.setTicker("欢迎使用播放器");// 任务栏里提示
		// builder.setOngoing(true);// 设置为不可清除模式,流氓式通知
		// ----------------定义点击通知后能跳转到主界面-----------------
		Intent intents = new Intent(this, NewKugouMainActivity.class);
		intents.putExtra("nameTimeNotifi", musicName);

		PendingIntent pendIntent = PendingIntent.getActivity(this, 11, intents,
				PendingIntent.FLAG_UPDATE_CURRENT);
		builder.setContentIntent(pendIntent);

		// ------自定义notification界面
		RemoteViews remoteView = new RemoteViews(getPackageName(),
				R.layout.activity_kugou_music_notificstion_layout);
		// -----------自定义通知按钮的监听,通过广播来执行-----------------

		// ----------------通知的音乐名字更改-----------------

		// remoteView.setTextViewText(R.id.notification_music_name_text,
		// "酷狗音乐播放器");

		remoteView
				.setTextViewText(R.id.notification_music_name_text, musicName);

		// ----------------上一首按钮-----------------
		Intent upMusicIntent = new Intent(ACTION_UP_MUSIC);
		if (checknumber == 0) {
			upMusicIntent.putExtra("UP_MUSIC_TIME_NOTIFI", checknumber);
		} else {
			upMusicIntent.putExtra("UP_MUSIC_TIME_NOTIFI", checknumber - 1);
		}
		PendingIntent pendUpMusic = PendingIntent.getBroadcast(this, 2,
				upMusicIntent, PendingIntent.FLAG_UPDATE_CURRENT);
		remoteView.setOnClickPendingIntent(R.id.notification_music_up_btn,
				pendUpMusic);
		// ----------------下一首按钮-----------------

		Intent nextMusicIntent = new Intent(ACTION_NEXT_MUSIC);

		if (checknumber < musicBeanList.size() - 1) {
			nextMusicIntent.putExtra("NEXT_MUSIC_TIME_NOTIFI", checknumber + 1);
		} else {
			nextMusicIntent.putExtra("NEXT_MUSIC_TIME_NOTIFI", checknumber);
		}
		PendingIntent pendNextMusic = PendingIntent.getBroadcast(this, 2,
				nextMusicIntent, PendingIntent.FLAG_UPDATE_CURRENT);
		remoteView.setOnClickPendingIntent(R.id.notification_next_btn,
				pendNextMusic);
		// ----------------暂停、开始按钮-----------------
		Intent startAndSuspendMusicIntent = new Intent(
				ACTION_START_SUSPEND_MUSIC);
		startAndSuspendMusicIntent.putExtra("SWITCH_START_SUSPEND_NOTIFI",
				mNitifiSwitch);
		PendingIntent pendStartAndSuspendMusic = PendingIntent.getBroadcast(
				this, 2, startAndSuspendMusicIntent,
				PendingIntent.FLAG_UPDATE_CURRENT);
		// -----------更换暂停、播放的按钮背景图片------------
		if (mediaPlay.isPlaying()) {
			remoteView.setImageViewResource(
					R.id.notification_music_start_suspend_btn,
					R.drawable.minilyric_pause_button_press);
		} else {
			remoteView.setImageViewResource(
					R.id.notification_music_start_suspend_btn,
					R.drawable.minilyric_play_button_press);
		}
		remoteView.setOnClickPendingIntent(
				R.id.notification_music_start_suspend_btn,
				pendStartAndSuspendMusic);
		// ----------------移除广播按钮-----------------
		Intent cleanNotifiIntent = new Intent(ACTION_CLEAN_NOTIFICATION);
		PendingIntent pendcleanNotifiMusic = PendingIntent.getBroadcast(this,
				2, cleanNotifiIntent, PendingIntent.FLAG_UPDATE_CURRENT);
		remoteView.setOnClickPendingIntent(R.id.notification_destroy_btn,
				pendcleanNotifiMusic);

		builder.setContent(remoteView);
		// -----------自定义通知的更新-----------------

		manger = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		manger.notify(notifinumber, builder.build());

	}

	BroadcastReceiver notifiReceiver = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (action.equals(ACTION_UP_MUSIC)) {
				// 服务里面的通知广播----上一首
				if (checknumber - 1 < 0) {// 数组从0开始计数
					checknumber = 0;// 再一次赋值,更新
				} else {
					musicPath = musicBeanList.get(checknumber - 1)
							.getMusicPathBean();
					musicName = musicBeanList.get(checknumber - 1)
							.getMusicNameBean();

					musicMediaPlayerPrepare(musicPath);
					checknumber = checknumber - 1;// 再一次赋值,更新
					createNotification();
				}

			} else if (action.equals(ACTION_START_SUSPEND_MUSIC)) {
				// 服务里面的通知广播----播放、暂停
				musicName = musicBeanList.get(checknumber).getMusicNameBean();
				Log.i("11", "刷新前>>>" + musicName);
				mNitifiSwitch = startAndSuspendMusic();
				createNotification();

			} else if (action.equals(ACTION_NEXT_MUSIC)) {
				// 服务里面的通知广播----下一首

				Log.i("11", "下一首提提前的值>>" + checknumber);
				if (checknumber + 1 < musicBeanList.size()) {// 数组从0开始计数
					musicPath = musicBeanList.get(checknumber + 1)
							.getMusicPathBean();
					musicName = musicBeanList.get(checknumber + 1)
							.getMusicNameBean();
					musicMediaPlayerPrepare(musicPath);

					checknumber = checknumber + 1;// 再一次赋值,更新
					createNotification();
				} else {
					checknumber = musicBeanList.size();
				}

			} else if (action.equals(ACTION_CLEAN_NOTIFICATION)) {
				// 服务里面的通知广播----清除通知
				manger.cancel(notifinumber);
			} else if (action.equals(ACTIVITY_UP_CLICK)) {
				// Activity类里面的通知广播----上一首
				int activiUpClickNumb = intent.getIntExtra(
						"activity_up_click_music", 0);
				checknumber = activiUpClickNumb;
				if (checknumber > 0) {
					musicName = musicBeanList.get(checknumber - 1)
							.getMusicNameBean();
					createNotification();
				}
			} else if (action.equals(ACTIVITY_NEXT_CLICK)) {
				// Activity类里面的通知广播----下一首
				int activiNextClickNumb = intent.getIntExtra(
						"activity_next_click_music", 0);

				checknumber = activiNextClickNumb;
				musicName = musicBeanList.get(checknumber).getMusicNameBean();
				Log.i("11", "活动类下一首广播》》名字>>" + musicName);
				createNotification();
			} else if (action.equals(ACTIVITY_START_AND_SUSPEND_CLICK)) {
				// Activity类里面的通知广播----播放、暂停
				mNitifiSwitch = intent.getBooleanExtra(
						"activity_startandsuspend_click_music", true);
				createNotification();
			} else if (action.equals(ACTIVITY_LISTVIEW_CLICK)) {
				Log.i("11", "服务接收活动里的>>>>LISTVIEW的广播");
				checknumber = intent.getIntExtra("LIST_V_CLICK_POSITON", 0);
				musicBeanList = intent
						.getParcelableArrayListExtra("LIST_V_CLICK_MUSICLIST");

				mBeanMusicList = musicBeanList.get(checknumber);

				musicPath = mBeanMusicList.getMusicPathBean();
				musicName = mBeanMusicList.getMusicNameBean();

				Log.i("11", musicPath);

				musicMediaPlayerPrepare(musicPath);
				createNotification();
			}
		}
	};

}


感觉绕的地方应该是:自己定的那个计数器,就是随时更改播放的是ListView中哪一首,点击每个按钮都会发广播取改变那个值,然后点击之后,又要改变值.最好是提前用个白纸可以写下来,哪些地方会用到更改值,如果你运气不好,我估计会绕死在里面,现在还有一个BUG就是,点击通知进去的时候,进度条两边的时间不是想要的,也不能显示歌曲的名字(歌曲名字很好解决),但是当通知里面的那首歌曲播放到下一首,界面的进度条时间就会正常显示。通知的那个右上角X,在彻底销毁界面后也没有反应。

你可能感兴趣的:(音乐播放器:添加了广播和后台服务、通知)