开始开发歌词秀的时候还是夏天,没有想到写这篇文章的时候大连已经迎来的今年的第一次大规模降温。多少有点冬天的感觉了。

上一篇文章我们已经介绍了,带有歌词播放功能的服务,按说接下来就该是利用歌词播放服务的应用程序了。不过我们在这里要先介绍另外一个类:LyricPlayerServiceProxy,先看一下这个类在整个软件中的位置。

为什么要有这么一个类呢?

原因是:Android里的服务用起来还不是很方便.

先看下面一段来自Android文档的的代码,这段代码是说明如何使用Service的。

链接:http://developer.android.com/reference/android/app/Service.html

private LocalService mBoundService;
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
// This is called when the connection with the service has been
// established, giving us the service object we can use to
// interact with the service. Because we have bound to a explicit
// service that we know is running in our own process, we can
// cast its IBinder to a concrete class and directly access it.
mBoundService
= ((LocalService.LocalBinder)service).getService();

// Tell the user about this for our demo.
Toast.makeText(Binding.this, R.string.local_service_connected,
Toast.LENGTH_SHORT).show();
}
public void onServiceDisconnected(ComponentName className) {
// This is called when the connection with the service has been
// unexpectedly disconnected -- that is, its process crashed.
// Because it is running in our same process, we should never
// see this happen.
mBoundService
= null;
Toast.makeText(Binding.this, R.string.local_service_disconnected,
Toast.LENGTH_SHORT).show();
}
};

void doBindService() {
// Establish a connection with the service. We use an explicit
// class name because we want a specific service implementation that
// we know will be running in our own process (and thus won't be
// supporting component replacement by other applications).
bindService
(new Intent(Binding.this,
LocalService.class), mConnection, Context.BIND_AUTO_CREATE);
mIsBound
= true;
}

void doUnbindService() {
if (mIsBound) {
// Detach our existing connection.
unbindService
(mConnection);
mIsBound
= false;
}
}

@Override
protected void onDestroy() {
super.onDestroy();
doUnbindService
();
}

代码本身不算复杂,但是有一点比较讨厌,用户在调用doBindService方法以后,不能立即得到可供使用的服务的实例(mBoundService)。要想使用可供使用的服务的实例,必须要等到ServiceConnectiononServiceConnected方法被调用之后才可以。多少还是有一些不方便的。怎么解决这个问题呢。

这里我们使用了Proxy(代理)设计模式。在GOF设计模式中代理模式的几个例子有远程代理,虚代理,保护代理,智能指引等,这里的用法和虚代理比较相似,但目的不同。虚代理的目的是有关资源的,这里是为了简化使用。

找一个生活中真正代理的例子说明一下这种区别吧。

说有一个大牌明星要举办一场演唱会,当然事前要做好多准备工作,比如开会啦什么的,如果每次都是大牌亲自去,一个是档期排不开,另外也太贵了,于是好多事就由经纪人代劳,直到真正演出是大牌才出场。这叫虚代理。

和大牌同一个经济公司有一个刚入行的小演员,公司也给他指派了经纪人。由于他还不能独立演出,公司就安排他给大牌垫场。可是这人有个毛病,早上爱睡懒觉,经常迟到。于是公司就指派他的经纪人先去接受任务,等小演员来了在转告给他。这样就不会耽误大牌的演出了。

这就是我们今天要说明的场景。为了减轻使用者的负担,我们运用了Proxy模式。

先看看数据成员。

private LyricPlayerService mPlaybackService = null ;

这就是我们的小演员了。经济人虽然不迟到,但是戏还是要演员演。这是我们真正实现功能的服务。

接下来就是需要交代给演员做的事了。现有经纪人保管着。

private ServiceConnectionListener mConnectionListener = null;

private LyricPlayerService.LyricPlayerListener mLyricListener = null;

private MediaPlayerService.NotificationProvider mNotificationProvider = null;

当然了,一旦见到小演员,把接到的任务交代给他那也是必须的。

        
  1. privateServiceConnectionmPlaybackConnection=newServiceConnection(){
  2. publicvoidonServiceConnected(ComponentNameclassName,IBinderservice){
  3. mPlaybackService=((LyricPlayerService.LocalBinder)service).getService();
  4. mPlaybackService.setLyricPlayerListener(mLyricListener);
  5. mPlaybackService.setNotificationProvider(mNotificationProvider);
  6. if(mConnectionListener!=null){
  7. mConnectionListener.onServiceConnected();
  8. }
  9. }
  10. publicvoidonServiceDisconnected(ComponentNameclassName){
  11. mPlaybackService.setLyricPlayerListener(null);
  12. mPlaybackService=null;
  13. if(mConnectionListener!=null){
  14. mConnectionListener.onServiceDisconnected();
  15. }
  16. }
  17. };

演员新入行,有时需要把一个指令分解,一步一步的指挥。经纪人也不容易啊。

        
  1. voidstartAndBindService(){
  2. mContextWrapper.startService(newIntent(mContextWrapper,LyricPlayerService.class));
  3. mContextWrapper.bindService(newIntent(mContextWrapper,LyricPlayerService.class),mPlaybackConnection,Context.BIND_AUTO_CREATE);
  4. }
  5. voidstopService(){
  6. if(mPlaybackService!=null){
  7. mContextWrapper.stopService(newIntent(mContextWrapper,LyricPlayerService.class));
  8. }
  9. }

经纪人做的挺好,干脆就不见小演员了,全又经纪人传话算了,这样一来,小演员来不来就没有什么关系了。

        
  1. publicStringgetTitle(){
  2. if(mPlaybackService!=null){
  3. returnmPlaybackService.getTitle();
  4. }else{
  5. returnnull;
  6. }
  7. }
  8. publicvoidsetMediaInfoProvider(MediaPlayerService.MediaInfoProviderprovider){
  9. mPlaybackService.setMediaInfoProvider(provider);
  10. }
  11. publicvoidsetNotificationProvider(MediaPlayerService.NotificationProviderprovider){
  12. mNotificationProvider=provider;
  13. }
  14. publicStringgetDataSource(){
  15. if(mPlaybackService!=null){
  16. returnmPlaybackService.getDataSource();
  17. }else{
  18. returnnull;
  19. }
  20. }
  21. publicintgetLyricCount(){
  22. if(mPlaybackService!=null){
  23. returnmPlaybackService.getLyricCount();
  24. }else{
  25. return0;
  26. }
  27. }
  28. publicStringgetLyric(intindex){
  29. if(mPlaybackService!=null){
  30. returnmPlaybackService.getLyric(index);
  31. }else{
  32. returnnull;
  33. }
  34. }
  35. publicvoidstart(){
  36. if(mPlaybackService!=null){
  37. mPlaybackService.start();
  38. }
  39. }
  40. publicvoidstop(){
  41. if(mPlaybackService!=null){
  42. mPlaybackService.stop();
  43. }
  44. }
  45. publicvoidpause(){
  46. if(mPlaybackService!=null){
  47. mPlaybackService.pause();
  48. }
  49. }
  50. publicbooleanisPlaying(){
  51. if(mPlaybackService!=null){
  52. returnmPlaybackService.isPlaying();
  53. }else{
  54. returnfalse;
  55. }
  56. }
  57. publicbooleanisPausing(){
  58. if(mPlaybackService!=null){
  59. returnmPlaybackService.isPausing();
  60. }else{
  61. returnfalse;
  62. }
  63. }
  64. publicintgetDuration(){
  65. if(mPlaybackService!=null){
  66. returnmPlaybackService.getDuration();
  67. }else{
  68. return0;
  69. }
  70. }
  71. publicintgetPosition(){
  72. if(mPlaybackService!=null){
  73. returnmPlaybackService.getPosition();
  74. }else{
  75. return0;
  76. }
  77. }
  78. publiclongseekToLyric(intindex){
  79. if(mPlaybackService!=null){
  80. returnmPlaybackService.seekToLyric(index);
  81. }else{
  82. return0;
  83. }
  84. }
  85. publicvoidseekToNextLyric(){
  86. if(mPlaybackService!=null){
  87. mPlaybackService.seekToNextLyric();
  88. }
  89. }
  90. publicvoidseekToPrevLyric(){
  91. if(mPlaybackService!=null){
  92. mPlaybackService.seekToPrevLyric();
  93. }
  94. }
  95. publiclongseek(longwhereto){
  96. if(mPlaybackService!=null){
  97. returnmPlaybackService.seek(whereto);
  98. }else{
  99. return0;
  100. }
  101. }
  102. publicvoidsetLyricPlayerListener(LyricPlayerService.LyricPlayerListenerlistener){
  103. mLyricListener=listener;
  104. if(mPlaybackService!=null){
  105. mPlaybackService.setLyricPlayerListener(mLyricListener);
  106. }
  107. }

软件的功能介绍,源码下载:原创:Android应用开发-Andorid歌词秀,含源码

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Android历代版本的代号
  2. Android 代码设置Color的几种方式
  3. 开始学习、翻译android文档
  4. android studio在编辑时出现如Failed to
  5. Cocos2d-x12和NDK-r8编译android
  6. 弹出sub View在当前窗口
  7. A simple guide to 9-patch for Android
  8. Android电池信息(Battery information)
  9. 【Android】两步搞定AndroidSDKManager在
  10. libGDX引擎在android APP开发中应用系列-