There are things in Android that you don't know about.

Source: Internet
Author: User

In the development of Android, there are always some seemingly simple, but actually stumbling problems, and so you go to explore, and so you go to solve, perhaps you have met the solution, perhaps you have not met, write this summary, hope to help the line will meet the friends, quickly solve these small problems!

First, the activity of the Popupwindow.

I did not notice the problem when I used Popupwindow, and I knew that it was a coincidence that the view view was always reported as null when the pop-up window in fragment was transplanted into the activity, such as:

Later through the guidance of the Big God, Baidu, finally found a reason to think that it is justified, that is, Popupwindow the Code of the window is written in the activity of the Oncreat method or other ordinary life cycle method caused by the error, because the activity is not rendered finished. So it is necessary to put the display method in the Popupwindow showatlocation in the Oncreat method outside, where can we ensure that after the activity rendering is finished? Of course it is possible to put it in the fragment. So how does the activity work? Some netizens have offered some ways:

1. Move to event (for example, in the Click event of a button)
2. Move to a sub-thread, and then cycle through the threads until you determine whether the control is rendered (such as a length width greater than 0)
3. Move to the overridden control and generate a pop when the control OnDraw () finishes

The above method I personally only agree with the first, but sometimes the first kind can not satisfy us, for example, you ask the activity to come in on the window prompt, of course, there is no click event.

Finally find a great way to do the following:

@Override

Public void Onwindowfocuschanged (Boolean hasfocus) {

//TODO auto-generated method stub

super.onwindowfocuschanged (hasfocus);

if (hasfocus) {

//Play window operation here

}

} The method is to rewrite the activity of the self-contained method, when the activity gets the focus of the automatic call, at this time the activity is absolutely rendered finished yo, never cheat you ! second, use the sound pool Soundpool in Android. for children 's shoes that are not yet known to Soundpool, I enclose an online summary of the following information:    in Android Development we often use MediaPlayer to play audio files, but MediaPlayer there are some shortcomings, such as: high resource consumption, long delay time, does not support multiple audio simultaneous playback and so on. These shortcomings have determined that the use of MediaPlayer in certain situations will not be ideal, for example in game development where time accuracy is a relatively high requirement. Here are the features of Soundpool :     1. Soundpool Loading music files uses a separate thread that does not block the operation of the UI main thread. But here if the sound file is too large to load, we may have serious consequences when we call the Play method, where Android The SDK provides a Soundpool.onloadcompletelistener class to help us understand whether the media files are loaded and finished, we reload Onloadcomplete (soundpool soundpool, int SampleID, int status) method can be obtained.
   2. from the Onloadcomplete method above can be seen that the class has a number of parameters, such as ID, yes Soundpool can handle multiple media at load initialization and into memory, where efficiency is much higher than MediaPlayer.
   3. the Soundpool class supports simultaneous playback of multiple sound effects, which is necessary for the game, while the MediaPlayer class is a single file that can be played synchronously. the last simple way to apply Soundpool:Soundpool Soundpool = new Soundpool (5,audiomanager.stream_system,0);
Soundpool.load (Mcontext,r.raw.raws, 1); soundpool.play (1, 0, 0, 1);    as above is a simple play, wherein the meaning of each parameter can be found on their own internet, specifically said the shortcomings of the above steps . at first I use the time has been out of voice, and then use the debug step by step debugging, but can walk through, sound also came out, think of the two run the only difference is the commissioning of the time I am very slow to go, is it loading takes time? Later the study was confirmed that the load () is really loading time, cannot be played immediately, and perhaps you also encounter this problem, the solution is to open a thread, sleep a second in the threads, and then in the play method call, the perfect solution. Of course, if your sound doesn't need to be executed immediately, but you need to click on one of the buttons, that doesn't happen. third, not to be continued.   

There are things in Android that you don't know about.

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.