Activity四大组件之Service 方式一startService

以编写音乐播放器为例

res文件夹下建raw文件夹放入要播放的音乐


先在AndroidManifest.xml文件注册

<service android:name="com.servicemusic.action.MyService"></service>

界面代码

	
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        >
        <Button 
            android:id="@+id/btn_play"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="播放"
            />
        <Button 
            android:id="@+id/btn_suspend"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="暂停"
            />
        <Button 
            android:id="@+id/btn_stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="停止"
            />
        <Button 
            android:id="@+id/btn_quit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="退出"
            />

        <Button
            android:id="@+id/btn_quit_stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="退出并停止播放" />
        
    </LinearLayout>

MainActivity类

public class MainActivity extends Activity implements OnClickListener{
	
	private Button btn_play,btn_suspend,btn_stop,btn_quit,btn_quit_stop;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init(){
    	btn_play=(Button) findViewById(R.id.btn_play);
    	btn_suspend=(Button) findViewById(R.id.btn_suspend);
    	btn_stop=(Button) findViewById(R.id.btn_stop);
    	btn_quit=(Button) findViewById(R.id.btn_quit);
    	btn_quit_stop=(Button) findViewById(R.id.btn_quit_stop);
    	
    	btn_quit_stop.setOnClickListener(this);
    	btn_quit.setOnClickListener(this);
    	btn_stop.setOnClickListener(this);
    	btn_suspend.setOnClickListener(this);
    	btn_play.setOnClickListener(this);
    }
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		int count=0;
		switch (arg0.getId()) {
		case R.id.btn_play:
			count=1;
			play(count);
			break;
			
		case R.id.btn_suspend:
			count=2;
			play(count);
			break;
			
		case R.id.btn_stop:
			count=3;
			play(count);
			break;
			
		case R.id.btn_quit:
			finish();
			break;
			
		case R.id.btn_quit_stop:
			stop(count);
			finish();
			break;
			
		default:
			break;
		}
	}

	private void play(int count){
		Intent intent=new Intent(this, MyService.class);
		Bundle bundle=new Bundle();
		bundle.putInt("count", count);
		intent.putExtras(bundle);
		startService(intent);
	}
		
	private void stop(int count){
		Intent intent=new Intent(this, MyService.class);
		//停止service服务,并调用MyService类中的onDestroy()方法
		stopService(intent);
	}
}


service类

public class MyService extends Service {

	private MediaPlayer player;

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		if (player == null) {
			player = MediaPlayer.create(this, R.raw.what_hurts_the_most);
			player.setLooping(false);
		}
		super.onCreate();
	}

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		int count = 0;
		if (intent != null) {
			Bundle bundle = intent.getExtras();
			count = bundle.getInt("count");
		}

		switch (count) {
		case 1:
			if (player != null && !player.isPlaying()) {
				start();
			}
			break;

		case 2:
			if (player.isPlaying()) {
				suspend();
			}
			break;

		case 3:
			if (player.isPlaying()) {
				stop();
			}
			break;

		default:
			break;
		}
		return count;
	}

	// 调用onDestroy退出service
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if (player != null) {
			player.stop();
			player.release();
		}
	}

	/** 播放的方法 */
	private void start() {
		if (player != null && !player.isPlaying()) {
			player.start();
		}
	}

	/** 暂停的方法 */
	private void suspend() {
		if (player != null) {
			player.pause();
		}
	}

	/** 停止播放的方法 */
	private void stop() {
		if (player != null) {
			player.stop();
			try {
				// 在调用stop后如果需要再次通过start进行播放,需要之前调用prepare函数
				player.prepare();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
	}

}


但是,停止播放音乐和暂停播放功能相同。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值