I have a download activity which downloads some zip files for my application.
I want my activity have this features :
capable of pause/resume download
shows download progress in my activity's UI by a progress bar ( not with
dialogProgress)
shows notification with download progress and when user clicks on the
notification it opens my activity even when the app is closed and my UI keeps updated during download
I have searched a lot and saw lots of methods that can be used to download files (like using AsyncTask/Downloadmanager/Groundy ... ) but I don't know which one of them has the features I want
what do you think is the best way to achieve this features all together ??
I don't want the full code from you, just some tips and methods or references to help me implement each of these features and find the best way.
Thanks for your time.
Fundamentally you can use AsyncTask through which you would be able to achieve the above, if the download is strictly tied to the activity. However AsyncTask needs to be properly cancelled once user cancel or back out from activity. Also it provide basic mechanism to do the progress thing.
For example, imagine a simple async task (not the best implementation) but something like below
class SearchAsyncTask extends AsyncTask<String, Void, Void> {
SearchHttpClient searchHttpClient = null;
public SearchAsyncTask(SearchHttpClient client) {
searchHttpClient = client;
}
#Override
protected void onProgressUpdate(Void... values) {
// You can update the progress on dialog here
super.onProgressUpdate(values);
}
#Override
protected void onCancelled() {
// Cancel the http downloading here
super.onCancelled();
}
protected Void doInBackground(String... params) {
try {
// Perform http operation here
// publish progress using method publishProgress(values)
return null;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute() {
// If not cancelled then do UI work
}
}
Now in your activity's onDestroy method
#Override
protected void onDestroy() {
Log.d(TAG, "ResultListActivity onDestory called");
if (mSearchTask != null && mSearchTask.getStatus() != AsyncTask.Status.FINISHED) {
// This would not cancel downloading from httpClient
// we have do handle that manually in onCancelled event inside AsyncTask
mSearchTask.cancel(true);
mSearchTask = null;
}
super.onDestroy();
}
However if you allow user to Download file even independent of activity (like it continues downloading even if user got away from Activity), I would suggest to use a Service which can do the stuff in background.
UPDATE: Just noticed there is a similar answer on Stackoverflow which explains my thoughts in further detail Download a file with Android, and showing the progress in a ProgressDialog
Related
I have problem with AsyncTasks.
My application is simple: ListView and Activity with custom Extras.
In Activity class I have some AsyncTasks with DefaultHttpClient (Downloading Jsons, Images)
When I wait until AsyncTask load everything and then go back to list and choose another row everything is fine.
But If I start my activity and immediately press "back", then choose another row, then new activity starts, but with task of the previous AsyncTask ( inappropriate images and strings ).
After a while everything back to normal(AsyncTask download proper content), but I want to avoid downloading unnecessary data.
How to not stop, but remove AsyncTask permanently when I press "back" ? Or maybe is other way ?
I will be grateful for the help.
Edit:
Of course I use cancel method:
Activity.class
#Override
public void onDestroy() {
super.onDestroy();
lastFM.cancel(true);
}
AsyncTask.class
#Override
protected String doInBackground(String... urls) {
if (isCancelled()) return null;
inputStream = getInputStreamFromHttp(urls[0]);
if (isCancelled()) return null;
jsonObject = getJSONObjectFromInputStream(inputStream);
bio = getBioFromJSON(jsonObject);
return bio;
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if (!isCancelled()) {
Intent i1 = new Intent(ActivityStation.UPDATEFRAGMENT3).putExtra(ActivityStation.ARTISTBIO, s);
LocalBroadcastManager.getInstance(context).sendBroadcast(i1);
}
}
There are techniques to leverage AsyncTasks to avoid this problem. Rather than dwelling on the details of these techniques, I would advise you to look at Loaders (specifically AsyncTaskLoader) which will handle the nuance of managing state for you. Have a look at this link: http://developer.android.com/guide/components/loaders.html
Calling
myAsyncTask.cancel(true);
will attempt to cancel the task. After this has been called, inside the AsyncTask's doInBackground() method, check
isCancelled()
to decide whether to stop or continue.
The simplest way to kill the asynctask
Shoutdown the HtppClient it will automatically stops the http requests
httpclient.getConnectionManager().shutdown()
I am developing an Android application and when it launches :
1) I make an HTTP Request to my server to send a small JSON file.
2) Open a webView to show a URL.
When the server is running properly there is absolutely no problem and everything goes smoothly.
HOWEVER , if for any reason the server is down , the HTTP request literally hangs and i need to wait till there is an HTTP timeOut which is around 30seconds till i actually see the webView with the URL loading.
I read that i shouldn't make any networking inside the UI thread and i should use another thread for that.
In BlackBerry , that i have already developed the application for , this is as simple as that :
new Thread(){
public void run(){
HttpConnection hc =
(HttpConnection)Connector.open("http://www.stackoverflow.com");
}
}.start();
I just start a new thread and inside i make the requests and all the necessary networking. That way , even when my server is not reachable the webView is loaded immediately without making the user wait and sense that the app is actually hanging.
How could i do exactly the same in Android , easiest way possible ?
Why not to use the same method as you use it for BlackBerry?
new Thread() {
public void run() {
new URL("http://www.stackoverflow.com").getContent();
}
}.start();
Use AsyncTask, it's the simplest way to do that. For more details:
http://developer.android.com/reference/android/os/AsyncTask.html
In icecream sandwich and above you are not allowed to use any web cal in UI thread. However you may use threads, but best way proposed by android is to use Async task. A very simple example is as follow.
"AsyncTask < Parameters Type, Progress Value Type, Return Type >"
class MyAsyncTask extends AsyncTask<String, String, String> {
#Override
protected void onPreExecute() {
// Runs on UI thread- Any code you wants
// to execute before web service call. Put it here.
// Eg show progress dialog
}
#Override
protected String doInBackground(String... params) {
// Runs in background thread
String result = //your web service request;
return result;
}
#Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
}
#Override
protected void onPostExecute(String resp) {
// runs in UI thread - You may do what you want with response
// Eg Cancel progress dialog - Use result
}
}
As I've asken on another question HERE it seems that the PackageManager.getInstalledPackages() doesn't play nice with Threading. As CommonsWare stated HERE:
Perhaps the PackageManager needs to be invoked on the main application
thread, for some reason
So having it on a thread gets undesired behavior, entering and exiting a few times in my Activity makes the list of displayed apps sometimes with items, sometimes empty. Having everything in the UI Thread works like a dream, loads fine everytime. The thing is, the user expects some sort of feedback and I need to provide one. As I start the activity, the screen remains black for 3-4-5-6 seconds (depending on the device and apps installed). How can I provide some sort of feedback ? I am thinking of a ProgressDialog but I don't know how can I start it. Thank you.
As discovered, the loop to work through the applications takes awhile (which can be done in a separate thread), compared to the call to PackageManager.getInstalledPackages() (which has to be done on the UI thread).
Use Async to do background work and show indicator while loading data.
in you onCreate(). call new AsyncCommonDataFetcher(this).execute();
public class AsyncCommonDataFetcher extends AsyncTask<Void, Void, Void> {
Context mContext = null;
private ProgressDialog mProgressIndicator = null;
public AsyncCommonDataFetcher(Context ctx) {
mContext = ctx;
}
protected void onPreExecute() {
mProgressIndicator = ProgressDialog.show(((Activity) mContext), null,
"Please wait", true);
}
#Override
protected Void doInBackground(Void... voids) {
// Do what ever work you like to do. It will do this in backgound and show user a indicator to wait.
}
#Override
protected void onPostExecute(Void voidInstance) {
try {
if (mProgressIndicator != null) {
mProgressIndicator.hide();
mProgressIndicator.dismiss();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
Try the following for ProgressDialog in the onCreate() of your activity
this.requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
ProgressDialog LoadingDialog = ProgressDialog.show(this, "", "Loading..", true);
And then dismiss it when the process causing the delay is over
LoadingDialog.dismiss();
I am using AsyncTask on button click to refresh the screen. Following is the sequence of events that happen on btn click
progress dialog shows up
The doInBackground is called and thread is initialized which calls a web service. The web service fetches/uploads data. A pass/fail flag is set once the web service is called.
My problem is the onPostExecute is never called and therefore the screen is never refreshed.
And secondly by the time the data is downloaded and the web service sets the flag my code has already hit return stmt in doInBackground.
Question is how do i stop execution in my asynctask so that the web service is done downloading/uploading the data and finally execute onPostexecute.
FYI
I also get the following warning in eclipse
The method onPostExecute(boolean) from
the type
Screen.ConnectWebService is
never used locally
private class ConnectWebService extends AsyncTask <Void, Void, Boolean>
{
private final ProgressDialog pd = new ProgressDialog(screen.this);
protected void onPreExecute() {
pd.show(Screen.this, "Sync", "Sync in progress",true,false);
}
protected Boolean doInBackground(Void... unused) {
if (SyncInProgress == false)
{
CallWSThread();//creates thread which calls web service
}
Log.d("doInBackground","doInBackground");
return SyncStatus;
}
protected Void onPostExecute(boolean result)
{
pd.dismiss();
if (result==true) drawRadioButtons();
return null;
}
}
It should be:
protected Void onPostExecute(Boolean result)
As djg noted, you have a typo in your method declaration. You can avoid these kinds of mistakes by using the annotation #Override when you're implementing methods from a super class.
The documentation at http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html#getWritableDatabase%28%29 states:
Database upgrade may take a long time,
you should not call this method
[getWritableDatabase] from the
application main thread, including
from ContentProvider.onCreate().
This begs the question: for best practice, where should getWritableDatabase be called from?
My feeling is that, perhaps, it should be called once upon application launch with a callback to mark the database as ready. Is this correct?
For small and agile databases I imagine this isn't much of an issue.
Otherwise, I'd use an always-wonderful AsyncTask, called from onCreate.
It can be called from anywhere, but it should not be called from the UI thread because you don't know how long the process will take (especially with the different file systems in use). Even if you know the database should be small, you don't know about the file system (can it perform more than one job at a time? are there are thousand other jobs waiting in line already?). You can use an AsyncTask or a Thread to call getWriteableDatabase.
It seems that the intended use of the open helper framework, is to open the db on activity start, and close it when the Activity is destroyed.
In an AsyncTask from within onCreate()...
new StartupTask().execute();
The AsyncTask Thread.sleep() below is just to give enough time to show the dialog so that you can see it work. Obviously take that out when you're done playing. ;)
private class StartupTask extends AsyncTask
{
private ProgressDialog progressDialog;
#Override
protected Object doInBackground(final Object... objects)
{
openHelperRef.getWritableDatabase();
try
{
Thread.sleep(5000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
return null;
}
#Override
protected void onPreExecute()
{
super.onPreExecute();
runOnUiThread(new Runnable()
{
public void run()
{
progressDialog = ProgressDialog.show(
MyActivity.this, "Title",
"Opening/Upgrading the database, please wait", true);
}
});
}
#Override
protected void onPostExecute(Object object)
{
super.onPostExecute(object);
progressDialog.dismiss();
}
}
in onDestroy()...
openHelper.close();