Multiple asynctasks in android - android

I want to run 2 method simultaneously. However it show only the output of second method. Can you help me?
ImageView imgView;
ImageView imgView2;
public void loadImageToImageView(){ imgView.setImageBitmap(currentBitmap);}
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void DrawLetter() {
new AsyncTask<Void, Void, Bitmap>() {
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected Bitmap doInBackground(Void... params) {
Draw();
Draw2();
return currentBitmap;
}
#Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
if(bitmap!=null) {
loadImageToImageView();
}
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}

I'm assuming you're running this task twice. But the two times are going to load the result into the same image view. That means whichever finishes first will be overwritten by the one that finishes second. If you want to see both, you need to use two separate image views.

Related

Fetch data in background and display in UI

I'm using volley to fetch data using volley and display in User Interface, and it takes time to load the data. What i want to happen is, I want to fetch the data in background during the splash screen or loading screen and display it in User interface. I want the fetching part to be done in another activity and it should be a background service and this should be called in "main activity" to populate the fields.
You can try using the AsyncTask. So the process goes like that:
In onPreExecute() you can show a ProgresDialog or a ProgresBar to visualize your loading process
in doInBackground(Params...) you start loading your data
and in onPostExecute(Result) you display your data in UI and hide your ProgresDialog/ProgresBar.
Example of AsyncTask usage.
Do it like this:
private class LoadDataBaseData extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
//Start loading your data
return "Data loaded";
}
#Override
protected void onPostExecute(String result) {
//Update your UI with data you loaded/start your activity with loaded data
}
#Override
protected void onPreExecute(){
}
#Override
protected void onProgressUpdate(Void... values) {
}
}
use AsyncTask like that
private class LongOperation extends AsyncTask<String, Void, String>
{
#Override
protected String doInBackground(String... params)
{
//do your work here
return "Executed";
}
#Override
protected void onPostExecute(String result)
{
progressBar.dismiss();
}
#Override
protected void onPreExecute()
{
progressBar = ProgressDialog.show(getActivity(), null, "message...");
progressBar.show();
}
#Override
protected void onProgressUpdate(Void... values)
{
}
}

how to load asyctask if new image is arrived in device ?

i have designed custom gallery but problem is that every time when i open gallery asyctask is call and all image getting load again. i want to load asyctask if new images arrived into device otherwise directly gallery will be shown without load
i have no more idea that how to do this kind of programming stuff so if there is anyway to do this kind of stuff then please suggest me
is there any way to do this ... ??
private class AsyncTaskForListview extends
AsyncTask<String, String, String> {
private String responce;
ProgressDialog progressDialog;
#Override
protected String doInBackground(String... params) {
getAllGalleryFolderName(); // process for get image
return responce;
}
#Override
protected void onPostExecute(String result) {
lvShowAllImageFolder.setAdapter(effectImageAdapter); // set adapeter in listview
effectImageAdapter.notifyDataSetChanged();
progressDialog.dismiss();
}
#Override
protected void onPreExecute() {
progressDialog = ProgressDialog
.show(DisplayGalleryActivity.this,
getResources().getString(
R.string.progress_dialog_heading),
getResources().getString(
R.string.progress_waiting_message));
}
#Override
protected void onProgressUpdate(String... text) {
}
}
You can use the FileObserver class to monitor a file or fold. It will notify if a change by any process in the device occurs. More details here .

AsynkTask arguments confusion

I have the following fully working code which was cobbled together with a bit of cut & paste from other people's examples:
private class Get_tweets_async_task_class extends AsyncTask<String, Integer, Bitmap>
{
protected void onPreExecute()
{
}
#Override
protected Bitmap doInBackground(String... params)
{
array_of_single_tweets = getTweets(params[0], 1);
return null;
}
protected void onProgressUpdate(Integer... params)
{
}
protected void onPostExecute(Bitmap img)
{
update_display_list();
}
protected void onCancelled()
{
}
}
My code does not in fact need anything to do with bitmaps (that was a hangover from the example) and now I'm trying to get rid of it. I edited the code as follows:
private class Get_tweets_async_task_class extends AsyncTask<String, Integer, Void>
{
protected void onPreExecute()
{
}
#Override
protected Void doInBackground(String... params)
{
array_of_single_tweets = getTweets(params[0], 1);
return null;
}
protected void onProgressUpdate(Integer... params)
{
}
protected void onPostExecute()
{
update_display_list();
}
protected void onCancelled()
{
}
}
but now the code no longer works. Did I do something wrong in my removal of the bitmap?
Edit: Sorry I mistyped the onPostExecute line - now corrected
protected void onPostExecute(Void nothing)
You need to fix your onPostExecute method.
It still receiving Bitmap as a result, change it to Void.
You edit your post but the problem is the same in onPostExecute.
Please check the official documentation : http://developer.android.com/reference/android/os/AsyncTask.html

Fill ViewFlipper in AsyncTask

I am trying to load images into my FlipView using AsyncTask but Im not sure how to continue.
I need to use AsyncTask because the application is force closing since I have 20 images to load from the drawables.
private class asyncImage extends AsyncTask<Void, Void, Void>{
int i;
#Override
protected Void doInBackground(Void... params) {
for (i=0;i<imageID.length;i++){
image = new ImageView(getBaseContext());
image.setId(i);
image.setImageResource(imageID[i]);
image.setBackgroundDrawable(getResources().getDrawable(R.drawable.border));
image.setScaleType(ImageView.ScaleType.FIT_XY);
FlipV.addView(image);
}
return null;
}
protected void onPostExecute(Void result){
image.setBackgroundDrawable(getResources().getDrawable(R.drawable.border));
image.setScaleType(ImageView.ScaleType.FIT_XY);
for(int x=0;x<imageID.length;x++){
FlipV.addView(???); //How to add images into FlipView?
}
}
}
Im stuck in adding the images to the FlipView. Any help?
As I have mentioned from my comment I've already solved my problem by using an imageview array instead ang loading the images in the doInBackground and adding the imageviews in the viewflipper in the postExecute. I've also added a progress dialog to show a loading time instead of a blank screen in my app. Thanks to everyone that replied.
private class asyncImage extends AsyncTask<Void, Void, Void>{
int i;
#Override
protected Void doInBackground(Void... params) {
for (i=0;i<imageID.length;i++){
image[i] = new ImageView(getBaseContext());
image[i].setImageResource(imageID[i]);
image[i].setBackgroundDrawable(getResources().getDrawable(R.drawable.border));
image[i].setScaleType(ImageView.ScaleType.FIT_XY);
}
return null;
}
protected void onPostExecute(Void result){
for (int x=0;x<imageID.length;x++){
FlipV.addView(image[x]);
}
count = 1;
progDialog.dismiss();
txItem.setText(Integer.toString(count) + "/" + (imageID.length));
}
}
You cant add instructions to doInBackground method which affect event/UI thread. Instead you should use, publishProgress() method to notify event thread, and in onProgressUpdate() add view to flipview.

ProgressBar betweed activities

I have two activities. The first one executes the second one.
Intent i = new Intent(MyOne.this, MyTwo.class);
startActivity(i);
The problem: My second activity does some heavy work on launching so it launches couple of second and before it is launched i see a black screen.
Is it any way to set a progressbar or some image instead of this black screen? Because i don't think user will wait for something, that he doesn't know. I tried setting progressbar after setcontentview in my second activity, but progressbar shows up only when activity fully started.
I suggest you to do this asynchronously using AsyncTask.
Short example:
ProgressDialog dialog;
class YourTask extends AsyncTask<Void, Void, Void> {
protected void onPreExecute() {
dialog = ProgressDialog.show(...);
}
protected Void doInBackground(Void... unused) {
try {
// doSomethingHeavy();
// publishProgress(...);
} catch(Exception e) {
//...
}
return null;
}
protected void onProgressUpdate(Void... unused) {
}
protected void onPostExecute(Void unused) {
dialog.dismiss();
}
}
You sure can. Check out the AsyncTask and this tutorial for inspiration.

Categories

Resources