标签:
Service绑定模式
案例:
布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageButton android:id="@+id/pause" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/play" android:layout_marginLeft="58dp" android:onClick="pause" android:layout_toRightOf="@+id/play" android:src="@android:drawable/ic_media_pause" /> <ImageButton android:id="@+id/play" android:onClick="play" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="61dp" android:layout_marginTop="68dp" android:src="@android:drawable/ic_media_play" /> </RelativeLayout>MainActivity,
package com.example.music_service; import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.view.Menu; import android.view.MenuItem; import android.view.View; public class MainActivity extends Activity { private ServiceConnection conn; private Player player; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); conn = new InnerServiceConnection(); int flags = BIND_AUTO_CREATE; Intent service = new Intent(this,PlayService.class); bindService(service, conn, flags); } public void pause(View view){ player.playMusic(); } public void play(View view){ player.pauseMusic(); } private class InnerServiceConnection implements ServiceConnection{ @Override public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub player = (Player) service; } @Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub } } protected void onDestroy(){ unbindService(conn); super.onDestroy(); } }Service:
package com.example.music_service; import java.io.IOException; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.Binder; import android.os.Environment; import android.os.IBinder; public class PlayService extends Service{ private MediaPlayer player; private int Current; @Override public void onCreate() { // TODO Auto-generated method stub player = new MediaPlayer(); player.setOnPreparedListener(new InnerPreparedListener()); player.setOnCompletionListener(new InnerCompletionListener()); } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return new InnerBinder(); } public class InnerBinder extends Binder implements Player{ @Override public void playMusic() { // TODO Auto-generated method stub play(); } @Override public void pauseMusic() { pause(); // TODO Auto-generated method stub } } private void play(){ try { player.reset(); player.setDataSource(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Music/Groove Coverage - She.mp3"); player.prepareAsync(); // player.start(); } catch (IllegalArgumentException e) { // TODO: handle exception e.printStackTrace(); }catch(SecurityException e){ e.printStackTrace(); } catch(IllegalStateException e){ e.printStackTrace(); } catch(IOException e){ e.printStackTrace(); } } private void pause(){ if(player.isPlaying()){ Current = player.getCurrentPosition(); player.pause(); } } private class InnerPreparedListener implements MediaPlayer.OnPreparedListener{ @Override public void onPrepared(MediaPlayer mp) { // TODO Auto-generated method stub mp.seekTo(Current); mp.start(); Current = 0; } } private class InnerCompletionListener implements MediaPlayer.OnCompletionListener{ @Override public void onCompletion(MediaPlayer mp) { // TODO Auto-generated method stub play(); } } @Override public void onDestroy() { // TODO Auto-generated method stub player.release(); player = null; super.onDestroy(); } }
package com.example.music_service; public interface Player { void playMusic(); void pauseMusic(); }OK,对了,service不要忘了在AndroidMainfest.xml中配置一下
标签:
原文地址:http://blog.csdn.net/wojiaohuangyu/article/details/50411463