android


Why does this code take a long time?


I am using this following code for getting all songs stored in the sdcard.
http://stackoverflow.com/a/12227047/2714061
Well why does this code take so long to return this list of songs.
I have included this code in a function which is called from the oncreate method in my player's playlist.
This is what happens.
1: When the application runs is executed for the first time on my android ph, the playlist has nothing to show, and hence is seen empty.
2: Well after for instance-> 30sec when I again call for the playlist it returns instantly all the songs.
Hence, giving the feel as though this thing takes time to execute?
Why does this happen?
How about using an asynchronous task, reading a file or downloading something, takes time that requires the user to wait, you must think of using an Asynchronous task for this purpose,
1: From the developer reference we have :
AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers. http://developer.android.com/reference/android/os/AsyncTask.html
An asynchronous task is defined by 3 generic types, called Params, Progress and Result, and 4 steps, called onPreExecute, doInBackground, onProgressUpdate and onPostExecute.
2: So you may include an Async task class as:
class DoBackgroundTask extends AsyncTask<URL, Void, ArrayList> {
/*
URL is the file directory or URL to be fetched, remember we can pass an array of URLs,
Void is simple void for the progress parameter, you may change it to Integer or Double if you also want to do something on progress,
Arraylist is the type of object returned by doInBackground() method.
*/
#Override
protected ArrayList doInBackground(URL... url) {
//Do your background work here
//i.e. fetch your file list here
return fileList; // return your fileList as an ArrayList
}
protected void onPostExecute(ArrayList result) {
//Do updates on GUI here
//i.e. fetch your file list from result and show on GUI
}
#Override
protected void onProgressUpdate(Integer... values) {
// Do something on progress update
}
}
//Meanwhile, you may show a progressbar while the files load, or are fetched.
This AsyncTask can be called from you onCreate method by calling its execute method and passing the arguments to it:
new DoBackgroundTask().execute(URL);
3: And at last, there is also a very nice tutorial about AsyncTasks here, http://www.vogella.com/articles/AndroidBackgroundProcessing/article.html

Related Links

Data Binding and Lambda in Android [duplicate]
Cordova file plugin - File not created
Difference between Argcis v10.2.8 and Argcis v100 (Android)
Android Location Update After Every 5min If Vehicle Not Moving
Clean resources from main project if exists in submodule
Already request dangerous permission on android 6.0, and check it in setting=>app. but app still crash on android 6.0
WebView cancelBuffer: BufferQueue has been abandoned
Get more details in JSON with feed request
How to run SpeechRecognition in android in the background service
Android app to app communication
Opening different application from an Android activity and getting and storing screenshots
facebook login is not working in webview
Cordova app, facebook login not working only on device
Android Google map's marker isn't centered
Decoder_setSearch returned -1 exception
Android Geo-fence not working properly

Categories

HOME
xml
jpa
cobalt
developer-tools
depth
composite-primary-key
requirements
sublimetext
carousel
soa
polling
vsm
nfs
pattern-matching
project
reactive-programming
angular-dart
jsonpath
openbugs
tput
altera
internationalization
google-data-studio
nodemcu
delphi-xe5
google-api-client
failover
mustache
left-join
sql-server-2000
angularjs-material
login-script
keras-layer
reactjs.net
carriage-return
numpy-broadcasting
flatmap
nohup
protein-database
diacritics
css-position
signals-slots
stringtokenizer
watchface
print-css
plan-9
automapper-5
dotnet-httpclient
zenhub
magic-draw
aquamacs
tinkerpop
spring-data-couchbase
backbone-collections
avx
jtree
pocketsphinx-android
chromium-os
usart
stax
actor-platform
redbeard-ios
mapinfo
i386
vorpal.js
cfchart
stream-processing
project-organization
yajsw
nullable
concept-insights
highland.js
opl
vigenere
fuelcms
zen-cart
ocra
user-permissions
theos
cbind
raygun
jqmodal
cancellation
spring-remoting
syndicationfeed
accesscontrolexception
ecslidingviewcontroller
isqlquery
ice-cube
ui-select2
kaleidoscope
android-icons
video-codecs
virtual-pc
twisted.web
veracity
wcf-configuration
azman
ilasm
jquery-address
automationelement
mbeans
undefined-index
updatesourcetrigger
drupal-fivestar
aspmenu
isapi-extension
skype4java

Resources

Encrypt Message