android service 学习
2010-08-23 01:13:00 来源:WEB开发网先创建一个IMyRemoteService.aidl文件
view sourceprint?1package org.allin.android.remote;
2interface IMusicControlService{
3 void play();
4 void stop();
5 void pause();
6}
如果你正在使用eclipse的Android插件,则它会根据这个aidl文件生成一个Java接口类。生成的接口类中会有一个内部类Stub类,你要做的事就是去继承该Stub类:
view sourceprint?01/**
02 * @author allin.dev
03 * http://allin.cnblogs.com/
04 *
05 */
06public class RemoteMusicService extends Service {
07
08 private static final String TAG = "RemoteMusicService";
09 private MediaPlayer mediaPlayer;
10
11 /*
12 * (non-Javadoc)
13 *
14 * @see android.app.Service#onBind(android.content.Intent)
15 */
16 @Override
17 public IBinder onBind(Intent intent) {
18 return binder;
19 }
20
21 private final IMusicControlService.Stub binder = new IMusicControlService.Stub() {
22
23 @Override
24 public void stop() throws RemoteException {
25 Log.d(TAG,"stop....");
26 if (mediaPlayer != null) {
27 mediaPlayer.stop();
28 try {
29 // 在调用stop后如果需要再次通过start进行播放,需要之前调用prepare函数
30 mediaPlayer.prepare();
31 } catch (IOException ex) {
32 ex.printStackTrace();
33 }
34 }
35
36 }
37
38 @Override
39 public void play() throws RemoteException {
40 Log.d(TAG,"play....");
41 if (mediaPlayer == null) {
42 mediaPlayer = MediaPlayer.create(RemoteMusicService.this,
43 R.raw.tmp);
44 mediaPlayer.setLooping(false);
45 }
更多精彩
赞助商链接