This problem occurs when I write a player these two days.
This problem is very difficult. I didn't listen to debugging at that time. It took about one afternoon to come up with this speculation. I will ask someone to confirm it tomorrow.
I am writing my personal speculation here for reference only. If I do not assume any legal responsibility or condemn morality, then I cannot ......
Since I just learned about the service, the design of the music player I use the service is a mess... Tears ~
However, this is irrelevant to the question I want to talk about now.
I found this problem. Run the pseudo-code first. This is the code of the player's main interface.
Package yanis. flyplay; public class flyplayactivity extends activity {/** called when the activity is first created. */private listview songlist; private seekbar processbar; private textview curtimeview, alltimeview; private imagebutton previusbtn, playbtn, nextbtn; protected mediaplayerservice identifier; private string path; private list <string> songs; protected serviceconnection connection = ne W serviceconnection () {@ overridepublic void onservicedisconnected (componentname name) {// todo auto-generated method stubmmediaplayerservice = NULL;} @ overridepublic void onserviceconnected (componentname, ibinder Service) {// todo auto-generated method stubmmediaplayerservice = (mediaplayerservice. localbinder) Service ). getservice (); system. out. println ("try to not null"); If (mmediaplayerservice! = NULL) {system. Out. println ("2not null !!!!!!! ");} Else {system. Out. println (" 2 null !!!!!!! ") ;}};@ Override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main );............ // other code startservice (new intent (getapplicationcontext (), mediaplayerservice. class); bindservice (new intent (getapplicationcontext (), mediaplayerservice. class), connection, service. bind_auto_create );............ // other code // while (! (Mmediaplayerservice! = NULL) {} // mmediaplayerservice. test (); // mmediaplayerservice. resetdatasource (path + songs. get (0 ));............ // other code }............ // other code}
Service Code is relatively common, mainly the override of some functions.
I only want to give key code.
public class LocalBinder extends Binder{MediaPlayerService getService() {return MediaPlayerService.this;}}public IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn mBinder;}public void test(){System.out.println("Test!!!!!!");}
Specifically, the onbind method and a custom test method are used. When the service is bound, it returns mbinder. Then, the mmediaplayerservice will be assigned to the mmediaplayerservice in the code of the player, so that you can use mmediaplayerservice to call your method of service, such as test. However, the problem occurs. Currently, the code I provided is correct. We can also verify from the sytem. Out input that the mmediaplayerservice is not null, that is, the binding is successful.
// While (! (Mmediaplayerservice! = NULL )){}
// Mmediaplayerservice. Test ();
// Mmediaplayerservice. resetdatasource (path + songs. Get (0 ));
However, pay attention to the player activity code. All three codes are displayed in the oncreate method. That is to say, if you cancel the second line of mmediaplayerservice when the activity is being constructed. test (); the comment of this sentence or the comment on the third line, so unfortunately this activity will stop ped unexpectedly. There is no output of "Try not null", that is to say, no attempt is made to bind it.
Why? Didn't I verify that the mmediaplayerservice was not empty just now? Why did it fail again? (Of course, you can use this service instance to call the service method only when the mmediaplayerservice is not empty after the binding is successful ).
One explanation is that binding takes time and you can use the mmediaplayerservice instance when the binding is not successful. Of course, an error occurs.
In this case, we can try again.
Comment out the second and third rows, and then cancel the first line, that is, while (! (Mmediaplayerservice )! = NULL. Obviously, if the mmediaplayer is always null, this statement is an endless loop, which will block the entire oncreate function.
Then you can try. It was found that there was nothing on the interface at the beginning, a piece of black.
Well, it seems that binding takes some time! But, really?
Well, I will tell you that even if you wait until your computer crashes, wait until there is no edge in the mountains, and wait until the sky and ground are merged, the interface is also black.
What does this mean? Combined with the example of successful binding for the first time, I guess that although the binding statement has long been written in oncreate, the actual binding operation will not be performed until oncreate is complete. This explains why the previous stopped unexpectedly, because oncreate was not completed yet!
If you don't believe it, you can test whether the mmediaplayerservice is empty at any time after oncreate is complete. I promise. It is definitely not empty!
However, I still have some minor problems to solve.
If you really want to use the service in oncreate, you can consider using the thread. It seems that it is safe when I use it, and there is no bug. I guess there is other thread security protection or something.