android service 学习
2010-08-23 01:13:00 来源:WEB开发网回调函数里面使用musicService = ((BindMusicService.MyBinder)(service)).getService();来获取BindMusicService服务对象,有了BindMusicService实例对象,就可以调用服务提供的各种控制音乐播放的哦功能。
下面看看BindMusicService.java的实现:
view sourceprint?01/**
02 * @author allin.dev
03 * http://allin.cnblogs.com/
04 */
05public class BindMusicService extends Service {
06
07 private static final String TAG = "MyService";
08 private MediaPlayer mediaPlayer;
09
10 private final IBinder binder = new MyBinder();
11
12 public class MyBinder extends Binder {
13 BindMusicService getService() {
14 return BindMusicService.this;
15 }
16 }
17
18 /*
19 * (non-Javadoc)
20 *
21 * @see android.app.Service#onBind(android.content.Intent)
22 */
23 @Override
24 public IBinder onBind(Intent intent) {
25 Log.d(TAG, "onBind");
26 play();
27 return binder;
28 }
29
30 @Override
31 public void onCreate() {
32 super.onCreate();
33
34 Log.d(TAG, "onCreate");
35 Toast.makeText(this, "show media player", Toast.LENGTH_SHORT).show();
36
37
38 }
39
40 @Override
41 public void onDestroy() {
42 super.onDestroy();
43
44 Log.d(TAG, "onDestroy");
45 Toast.makeText(this, "stop media player", Toast.LENGTH_SHORT);
46 if(mediaPlayer != null){
47 mediaPlayer.stop();
48 mediaPlayer.release();
49 }
50 }
51
52
53 public void play() {
54 if (mediaPlayer == null) {
更多精彩
赞助商链接