第二种是通过绑定的方式来启动Service。先看流程图,点击放大。
在这里我们使用了this.bindService(intent, myServiceConnection, Context.BIND_AUTO_CREATE); 来启动Service,当Service创建了同时绑定了之后,会回调我们定义的ServiceConnection(),从而传回IBinder接口,我们就能够调用Service中的方法了。这时候Activity就和Service实现了绑定,Activity退出了Service就相应的退出了。
<service android:enabled="true" android:name=".service.mediaplayer.BindMusicService">
<intent-filter>
<action android:name="com.androidtest.service.mediaplayer.BindMusicService" />
</intent-filter>
</service>
|
主要代码如下:
- /**
- * BinderMusicPlayerActivity.java
- * com.androidtest.activity.musicplayer
- *
- * Function: TODO
- *
- * ver date author
- * ──────────────────────────────────
- * 2011-5-17 Leon
- *
- * Copyright (c) 2011, TNT All Rights Reserved.
- */
- package com.androidtest.activity.musicplayer;
- import com.androidtest.R;
- import com.androidtest.service.mediaplayer.BindMusicService;
- import com.androidtest.service.mediaplayer.MyBinder;
- import com.androidtest.service.mediaplayer.MyMediaController;
- import android.app.Activity;
- import android.content.ComponentName;
- import android.content.Context;
- import android.content.Intent;
- import android.content.ServiceConnection;
- import android.os.Bundle;
- import android.os.IBinder;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- /**
- * ClassName:BinderMusicPlayerActivity Function: TODO ADD FUNCTION Reason: TODO
- * ADD REASON
- *
- * @author Leon
- * @version
- * @since Ver 1.1
- * @Date 2011-5-17
- */
- public class BinderMusicPlayerActivity extends Activity implements
- OnClickListener {
- private static final String TAG = BinderMusicPlayerActivity.class
- .getSimpleName();
- private BindMusicService bindMusicService;
- private Button playButton, pauseButton, stopButton, closeActivityButton,
- exitActivityButton;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- Log.v(TAG , TAG+"onCreate()");
- this.setContentView(R.layout.music_player_layout);
- findViews();
- bindViews();
- connection();
- }
- private void findViews() {
- playButton = (Button) this.findViewById(R.id.play);
- pauseButton = (Button) this.findViewById(R.id.pause);
- stopButton = (Button) this.findViewById(R.id.stop);
- closeActivityButton = (Button) this.findViewById(R.id.close);
- exitActivityButton = (Button) this.findViewById(R.id.exit);
- }
- private void bindViews() {
- playButton.setOnClickListener(this);
- pauseButton.setOnClickListener(this);
- stopButton.setOnClickListener(this);
- closeActivityButton.setOnClickListener(this);
- exitActivityButton.setOnClickListener(this);
- }
- private void connection() {
- Log.v(TAG , TAG+"connection");
- Intent intent = new Intent("com.androidtest.service.mediaplayer.BindMusicService");
- this.startService(intent);
- this.bindService(intent, myServiceConnection, Context.BIND_AUTO_CREATE);
- }
- @Override
- public void onClick(View view) {
- // TODO Auto-generated method stub
- // TODO Auto-generated method stub
- switch (view.getId()) {
- case R.id.play:
- Log.d(TAG, "play.......");
- MyMediaController.play.execute();
- break;
- case R.id.pause:
- Log.d(TAG, "pause.......");
- MyMediaController.pause.execute();
- break;
- case R.id.stop:
- Log.d(TAG, "stop.......");
- MyMediaController.stop.execute();
- break;
- case R.id.close:
- Log.d(TAG, "close.......");
- this.finish();
- break;
- case R.id.exit:
- Log.d(TAG, "exit.......");
- this.finish();
- this.stopService(new Intent("com.androidtest.service.mediaplayer.BindMusicService"));
- }
- }
- //调用bindService后 Service调用onBind()后 回调此函数
- private ServiceConnection myServiceConnection = new ServiceConnection() {
- @Override
- public void onServiceConnected(ComponentName name, IBinder binder) {
- Log.v(TAG , TAG+ "onServiceConnected");
- // TODO Auto-generated method stub
- bindMusicService = ((MyBinder)binder).getService();
- //给Controller设置Service初始化的MediaPlayer
- MyMediaController.mediaPlayer=bindMusicService.getMyMediaPlayer();
- }
- @Override
- public void onServiceDisconnected(ComponentName name) {
- // TODO Auto-generated method stub
- bindMusicService=null;
- Log.v(TAG, "..............onServiceDisconnected");
- }
- };
- //当Activity finish时必须解绑 不然会出现溢出
- public void finish() {
- // TODO Auto-generated method stub
- super.finish();
- this.unbindService(myServiceConnection);
- }
- }
- /**
- * BindMusicService.java
- * com.androidtest.service.mediaplayer
- *
- * Function: TODO
- *
- * ver date author
- * ──────────────────────────────────
- * 2011-5-16 Leon
- *
- * Copyright (c) 2011, TNT All Rights Reserved.
- */
- package com.androidtest.service.mediaplayer;
- import com.androidtest.R;
- import android.app.Service;
- import android.content.Intent;
- import android.media.MediaPlayer;
- import android.os.IBinder;
- import android.util.Log;
- /**
- * ClassName:BindMusicService
- * Function: TODO ADD FUNCTION
- * Reason: TODO ADD REASON
- *
- * @author Leon
- * @version
- * @since Ver 1.1
- * @Date 2011-5-16
- */
- public class BindMusicService extends Service {
- private static final String TAG = BindMusicService.class.getSimpleName();
- private MediaPlayer myMediaPlayer ;
- private IBinder binder = new MyBinder(this);
- public MediaPlayer getMyMediaPlayer() {
- return myMediaPlayer;
- }
- public void setMyMediaPlayer(MediaPlayer myMediaPlayer) {
- this.myMediaPlayer = myMediaPlayer;
- }
- @Override
- public IBinder onBind(Intent intent) {
- Log.v(TAG, TAG+" onBind");
- // TODO Auto-generated method stub
- return binder;
- }
- @Override
- public void onCreate() {
- // TODO Auto-generated method stub
- super.onCreate();
- if(myMediaPlayer==null){
- myMediaPlayer=MediaPlayer.create(this, R.raw.test) ;
- myMediaPlayer.setLooping(false);
- }
- Log.v(TAG , TAG +" onCreate()");
- }
- @Override
- public void onDestroy() {
- // TODO Auto-generated method stub
- super.onDestroy();
- Log.v(TAG, TAG+"onDestroy()");
- if(myMediaPlayer!=null){
- myMediaPlayer.stop();
- myMediaPlayer.release();
- }
- }
- //通过bindService的方式,onStart函数不会调用
- @Override
- public void onStart(Intent intent, int startId) {
- // TODO Auto-generated method stub
- super.onStart(intent, startId);
- Log.v(TAG, TAG+" onStart()");
- }
- @Override
- public boolean onUnbind(Intent intent) {
- // TODO Auto-generated method stub
- Log.v(TAG , TAG +" onUnbind ,成功没有?" + super.onUnbind(intent));
- return true;
- }
- @Override
- public void onRebind(Intent intent) {
- // TODO Auto-generated method stub
- Log.v(TAG , TAG +" onRebind()----------------------------------->");
- super.onRebind(intent);
- }
- }
- /**
- * MyBinder.java
- * com.androidtest.service.mediaplayer
- *
- * Function: TODO
- *
- * ver date author
- * ──────────────────────────────────
- * 2011-5-17 Leon
- *
- * Copyright (c) 2011, TNT All Rights Reserved.
- */
- package com.androidtest.service.mediaplayer;
- import android.os.Binder;
- /**
- * ClassName:MyBinder Function: TODO ADD FUNCTION Reason: TODO ADD REASON
- *
- * @author Leon
- * @version
- * @since Ver 1.1
- * @Date 2011-5-17
- */
- public class MyBinder extends Binder {
- private BindMusicService bindMusicService;
- public MyBinder(BindMusicService bindMusicService) {
- this.bindMusicService = bindMusicService;
- };
- public BindMusicService getService() {
- return bindMusicService;
- }
本文出自 “最牛傻蛋的博客” 博客,转载请与作者联系!