Android lyrics show design ideas (6) use Proxy design mode to simplify the use of the lyrics play service

Source: Internet
Author: User

It was still summer when I started to develop the lyrics show. I didn't think that Dalian had ushered in the first large-scale cooling this year when I wrote this article. It's a bit winter.
As we have already introduced in the previous article, the Service with the lyrics playing function should be followed by an application that uses the lyrics playing service. But here we will first introduce another class: LyricPlayerServiceProxy. Let's take a look at the position of this class in the entire software.

Why is there such a class?
The reason is: the services in Android are not very convenient to use.
Let's take a look at the following code from the Android documentation, which describes how to use the Service.
Link: 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
// 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 shoshould 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 ();
}
 
The code itself is not complicated, but it is a bit annoying. After you call the doBindService method, you cannot immediately obtain mBoundService instances ). To use an instance of the available service, you must wait until the onServiceConnected method of ServiceConnection is called. There are still some inconveniences. How can this problem be solved.
Here we use the Proxy design mode. In the GOF design mode, several examples of proxy mode include remote proxy, virtual proxy, protection proxy, and intelligent guidance. The usage here is similar to that of virtual proxy, but the purpose is different. The purpose of the virtual proxy is related to resources, which is to simplify usage.
Find an example of real proxy in your life to illustrate this difference.
Say there is a big-name star who wants to hold a concert. Of course, there are a lot of preparations beforehand, such as meetings and so on, in addition, it is too expensive, so a lot of things will be done by agents until the real performance is a big name. This is called a virtual proxy.
The same as a big-name economic company has a just-in-the-box actress, and the company has assigned him a broker. Since he is not able to perform independently, the company will arrange for him to play the big-name game. But this person has a problem. He often gets late in the morning. So the company assigned his broker to accept the task first and waited for the young actor to come and tell him. This will not delay the big-name performances.
This is the scenario we want to describe today. To reduce the burden on users, we use the Proxy mode.
First look at the data members.
Private LyricPlayerService mPlaybackService = null;
This is our little actor. Although the Economic Man is not late, actors are still playing the drama. This is a service that truly implements functions.
 
The next step is what we need to explain to the actors. Existing brokers are kept.
Private ServiceConnectionListener mConnectionListener = null;
Private LyricPlayerService. LyricPlayerListener mLyricListener = null;
Private MediaPlayerService. icationicationprovider mNotificationProvider = null;
 
Of course, once you see an actor, it is also necessary to give the task to him.
 
Private ServiceConnection mPlaybackConnection = new ServiceConnection (){
Public void onServiceConnected (ComponentName className, IBinder service ){
MPlaybackService = (LyricPlayerService. LocalBinder) service). getService ();
MPlaybackService. setLyricPlayerListener (mLyricListener );
MPlaybackService. setNotificationProvider (mNotificationProvider );
If (mConnectionListener! = Null ){
MConnectionListener. onServiceConnected ();
}
}
 
Public void onServiceDisconnected (ComponentName className ){
MPlaybackService. setLyricPlayerListener (null );
MPlaybackService = null;
If (mConnectionListener! = Null ){
MConnectionListener. onServiceDisconnected ();
}
}
};
An actor sometimes needs to break down an instruction and perform the command step by step. It's not easy for a broker.
Void startAndBindService (){
MContextWrapper. startService (new Intent (mContextWrapper, LyricPlayerService. class ));
MContextWrapper. bindService (new Intent (mContextWrapper, LyricPlayerService. class), mPlaybackConnection, Context. BIND_AUTO_CREATE );
}
 
Void stopService (){
If (mPlaybackService! = Null ){
MContextWrapper. stopService (new Intent (mContextWrapper, LyricPlayerService. class ));
}
}
The agent did a good job, so he simply disappeared from the minor actors and sent a message from the agent. As a result, it would be okay if the minor actors did not come.
Public String getTitle (){
If (mPlaybackService! = Null ){
Return mPlaybackService. getTitle ();
} Else {
Return null;
}
}
 
Public void setMediaInfoProvider (MediaPlayerService. MediaInfoProvider provider ){
MPlaybackService. setMediaInfoProvider (provider );
}
 
Public void setNotificationProvider (MediaPlayerService. NotificationProvider provider ){
MNotificationProvider = provider;
}
 
Public String getDataSource (){
If (mPlaybackService! = Null ){
Return mPlaybackService. getDataSource ();
} Else {
Return null;
}
}
 
Public int getLyricCount (){
If (mPlaybackService! = Null ){
Return mPlaybackService. getLyricCount ();
} Else {
Return 0;
}
}
 
Public String getLyric (int index ){
If (mPlaybackService! = Null ){
Return mPlaybackService. getLyric (index );
} Else {
Return null;
}
}
 
Public void start (){
If (mPlaybackService! = Null ){
MPlaybackService. start ();
}
}
 
Public void stop (){
If (mPlaybackService! = Null ){
MPlaybackService. stop ();
}
}
 
Public void pause (){
If (mPlaybackService! = Null ){
MPlaybackService. pause ();
}
}
 
Public boolean isPlaying (){
If (mPlaybackService! = Null ){
Return mPlaybackService. isPlaying ();
} Else {
Return false;
}
}
 
Public boolean isPausing (){
If (mPlaybackService! = Null ){
Return mPlaybackService. isPausing ();
} Else {
Return false;
}
}
 
Public int getDuration (){
If (mPlaybackService! = Null ){
Return mPlaybackService. getDuration ();
} Else {
Return 0;
}
}

Public int getPosition (){
If (mPlaybackService! = Null ){
Return mPlaybackService. getPosition ();
} Else {
Return 0;
}
}
 
Public long seekToLyric (int index ){
If (mPlaybackService! = Null ){
Return mPlaybackService. seekToLyric (index );
} Else {
Return 0;
}
}
 
Public void seekToNextLyric (){
If (mPlaybackService! = Null ){
MPlaybackService. seekToNextLyric ();
}
}
 
Public void seekToPrevLyric (){
If (mPlaybackService! = Null ){
MPlaybackService. seekToPrevLyric ();
}
}
 
Public long seek (long whereto ){
If (mPlaybackService! = Null ){
Return mPlaybackService. seek (whereto );
} Else {
Return 0;
}
}
 
Public void setLyricPlayerListener (LyricPlayerService. LyricPlayerListener listener ){
MLyricListener = listener;
If (mPlaybackService! = Null ){
MPlaybackService. setLyricPlayerListener (mLyricListener );
}
}
 
Software features, source code download: Original: Android Application Development-Andorid lyrics show, including source code
Author: "From Dalian"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.