Android歌词秀设计思路(6)运用Proxy设计模式简化歌词播放服务
开始开发歌词秀的时候还是夏天,没有想到写这篇文章的时候大连已经迎来的今年的第一次大规模降温。多少有点冬天的感觉了。
上一篇文章我们已经介绍了,带有歌词播放功能的服务,按说接下来就该是利用歌词播放服务的应用程序了。不过我们在这里要先介绍另外一个类: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
相关新闻>>
- 发表评论
-
- 最新评论 更多>>