service和BroadcastReceive控制的音乐播放器

package cn.com.dne.activity.ceshi;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class LinearActivity extends  Activity implements OnClickListener{
	ImageButton start;//播放、暂停按钮
	ImageButton stop;//停止按钮
	ActivityReceiver activityReceiver;
	int status = 1;//当前的状态,1没有声音播放 ,2 正在播放声音,3暂停
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {//重写的onCreate方法
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);//设置当前的用户界面
        start = (ImageButton) this.findViewById(R.id.start);//得到start的引用
        stop = (ImageButton) this.findViewById(R.id.stop);//得到stop按钮的引用
        start.setOnClickListener(this);//为按钮添加监听
        stop.setOnClickListener(this);//为按钮添加监听
        activityReceiver = new ActivityReceiver();//创建BroadcastReceiver
        IntentFilter filter = new IntentFilter();//创建IntentFilter过滤器
        filter.addAction("wyf.ytl.update");//添加Action
        registerReceiver(activityReceiver, filter);//注册监听
        Intent intent = new Intent(this, MyService.class);//创建Intent
        startService(intent);//启动后台Service
    }
    public class ActivityReceiver extends BroadcastReceiver{//自定义的BroadcastReceiver
		@Override
		public void onReceive(Context context, Intent intent) {//重写的onReceive方法
			// TODO Auto-generated method stub
			int update = intent.getIntExtra("update", -1);//得到intent中的数据
			switch(update){//分支判断
			case 1://没有声音播放
				status = 1; //设置当前状态
				break;
			case 2://正在播放声音
				start.setImageResource(R.drawable.png3);//更换图片
				status = 2; //设置当前状态
				break;
			case 3://暂停中
				start.setImageResource(R.drawable.png2);//更换图片
				status = 3; //设置当前状态
				break;
			}
		}
    }
	public void onClick(View v) {//接口中的方法
		// TODO Auto-generated method stub
		Intent intent = new Intent("wyf.ytl.control");//创建Intent
		switch(v.getId()){//分支判断
		case R.id.start://按下播放、暂停按钮
			intent.putExtra("ACTION", 1);//存放数据
			sendBroadcast(intent);//发送广播
			break;
		case R.id.stop://按下停止按钮
			intent.putExtra("ACTION", 2);//存放数据
			sendBroadcast(intent);//发送广播
			break;
		}
	}
	@Override
	protected void onDestroy() {//释放时被调用
		// TODO Auto-generated method stub
		super.onDestroy();
        Intent intent = new Intent(this, MyService.class);//创建Intent
        stopService(intent);//停止后台的Service
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu){//弹出菜单 
		menu.add(0,Menu.FIRST,0,"退出")
			.setIcon(android.R.drawable.ic_menu_delete);//设置图标
		return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item){//选择的菜单项
		switch(item.getItemId()){//分支判断
		case Menu.FIRST:
			showDialog(1);//显示对话框
			break;
		}
		//将来可在此进行扩展
		return false;
	}
	@Override
	protected Dialog onCreateDialog(int id){//创建对话框
		switch(id){//判断
		case 1:
			return new AlertDialog.Builder(this)
				.setTitle("您确定退出?")
				.setPositiveButton("确定", new android.content.DialogInterface.OnClickListener(){
			
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						System.exit(0);//直接退出
					}
				})
				.setNegativeButton("取消", null)//取消按钮
				.create();
		default:
			return null;
		}
	}
}

 

package cn.com.dne.activity.ceshi;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.os.IBinder;

public class MyService extends Service{
	MediaPlayer mp;
	ServiceReceiver serviceReceiver;
	int status = 1;//当前的状态,1没有声音播放 ,2 正在播放声音,3暂停
	@Override
	public IBinder onBind(Intent intent) {//重写的onBind方法
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public void onCreate() {//重写的onCreate方法
		// TODO Auto-generated method stub		
		status = 1;
		serviceReceiver = new ServiceReceiver();//创建BroadcastReceiver
		IntentFilter filter = new IntentFilter();//创建过滤器
		filter.addAction("wyf.ytl.control");//添加Action
		registerReceiver(serviceReceiver, filter);//注册BroadcastReceiver
		super.onCreate();
	}
	@Override
	public void onDestroy() {//重写的onDestroy方法
		// TODO Auto-generated method stub
		unregisterReceiver(serviceReceiver);//取消注册
		super.onDestroy();
	}
	public class ServiceReceiver extends BroadcastReceiver{//自定义BroadcastReceiver
		@Override
		public void onReceive(Context context, Intent intent) {//重写的响应方法
			// TODO Auto-generated method stub
			int action = intent.getIntExtra("ACTION", -1);//得带需要的数据
			switch(action){
			case 1://播放或暂停声音
				if(status == 1){//当前没有声音播放
					mp = MediaPlayer.create(context, R.raw.nx);
					status = 2;
					Intent sendIntent = new Intent("wyf.ytl.update");
					sendIntent.putExtra("update", 2);
					sendBroadcast(sendIntent);
					mp.start();
				}
				else if(status == 2){//正在播放声音
					mp.pause();	//停止
					status = 3;//改变状态 
					Intent sendIntent = new Intent("wyf.ytl.update");
					sendIntent.putExtra("update", 3);//存放数据
					sendBroadcast(sendIntent);//发送广播
				}
				else if(status == 3){//暂停中
					mp.start();//播放声音
					status = 2;//改变状态
					Intent sendIntent = new Intent("wyf.ytl.update");
					sendIntent.putExtra("update", 2);//存放数据
					sendBroadcast(sendIntent);//发送广播
				}
				break;
			case 2://停止声音
				if(status == 2 || status == 3){//播放中或暂停中
					mp.stop();//停止播放
					status = 1;//改变状态
					Intent sendIntent = new Intent("wyf.ytl.update");
					sendIntent.putExtra("update", 1);//存放数据
					sendBroadcast(sendIntent);//发送广播
				}
			}
		}
	}
}

 

你可能感兴趣的:(android,OS,音乐)