Service服务
1 package com.example.administrator.myapplication.service; 2 3 import android.app.Service; 4 import android.content.Intent; 5 import android.media.MediaPlayer; 6 import android.os.Binder; 7 import android.os.IBinder; 8 9 import com.example.administrator.myapplication.R; 10 11 public class MyService extends Service { 12 MediaPlayer mediaPlayer;//媒体播放类 13 public MyService() { 14 } 15 16 //自定义Binder类用于获取Service对象 17 public class MyBinder extends Binder { 18 public MyService getService(){ 19 return MyService.this; 20 } 21 } 22 @Override 23 public void onCreate() { 24 super.onCreate(); 25 mediaPlayer = MediaPlayer.create(this, R.raw.dawang); 26 } 27 28 //绑定类 29 //必须实现,Ibinder返回值,包含了service与Activity的连接状态给Activity 30 @Override 31 public IBinder onBind(Intent intent) { 32 // throw new UnsupportedOperationException("Not yet implemented"); 33 mediaPlayer.start(); 34 return new MyBinder(); 35 } 36 37 @Override 38 public boolean onUnbind(Intent intent) { 39 mediaPlayer.stop(); 40 return super.onUnbind(intent); 41 } 42 43 //start类的 44 @Override 45 public int onStartCommand(Intent intent, int flags, int startId) { 46 mediaPlayer.start(); 47 return super.onStartCommand(intent, flags, startId); 48 } 49 50 @Override 51 public void onDestroy() { 52 super.onDestroy(); 53 mediaPlayer.stop(); 54 } 55 }
Activity
package com.example.administrator.myapplication.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.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import com.example.administrator.myapplication.R; import com.example.administrator.myapplication.service.MyService; public class MusicActivity extends AppCompatActivity { Button bindService; Button unBindService; Button startService; Button stopService; MyService musicService; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_music); bindService = (Button) findViewById(R.id.bindService); unBindService = (Button) findViewById(R.id.unBindService); startService = (Button) findViewById(R.id.startService); stopService = (Button) findViewById(R.id.stopService); /*绑定服务 */ bindService.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MusicActivity.this,MyService.class); bindService(intent,connection, Context.BIND_AUTO_CREATE); } }); /*解绑服务*/ unBindService.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //服务不为空时 if (musicService != null){ unbindService(connection); musicService = null; } } }); /*启动服务*/ startService.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MusicActivity.this,MyService.class); startService(intent); } }); /*停止服务*/ stopService.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MusicActivity.this,MyService.class); stopService(intent); } }); } /*ServiceConnection是一个接口,该接口用于监听服务与启动源之间的链接与断开状态*/ ServiceConnection connection = new ServiceConnection() { /*当服务与启动源绑定时调用*/ /** * 在这里还涉及到IBinder,当启动源和服务成功链接后,可以获取到IBinder对象, * 通过IBinder对象,启动源与服务可以完成通信。 * 在实际开发中通常采用继承Binder(实现了IBinder接口)来实现自己IBinder对象。 */ @Override public void onServiceConnected(ComponentName name, IBinder service) { musicService = ((MyService.MyBinder)service).getService(); } /*当程序因异常而断开服务与启动源之间链接时调用*/ @Override public void onServiceDisconnected(ComponentName name) { } }; }