Created
February 15, 2016 08:21
-
-
Save pantos27/250f2d257790ee335b96 to your computer and use it in GitHub Desktop.
async task home work
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.pantos27.www.lesson17_asynctask; | |
import android.os.AsyncTask; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.Toast; | |
import java.net.URL; | |
public class MainActivity extends AppCompatActivity { | |
AsyncTask<String,String,Boolean> asyncTask; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
} | |
public void OnClick(View v){ | |
String[] arr={"1","2","3","4","5"}; | |
//check if already running | |
if (v.getId()==R.id.buttonSerial && asyncTask!=null && asyncTask.getStatus()== AsyncTask.Status.RUNNING){ | |
if(asyncTask.cancel(true)) | |
Toast.makeText(MainActivity.this, "Stating Over", Toast.LENGTH_SHORT).show(); | |
} | |
for (String url : arr) { | |
asyncTask =new AsyncTask<String, String, Boolean>() { | |
@Override | |
protected Boolean doInBackground(String... params) { | |
for (String url : params) { | |
try { | |
Thread.sleep(360); | |
if (this.isCancelled()) | |
return false; | |
this.publishProgress(url); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
return true; | |
} | |
@Override | |
protected void onProgressUpdate(String... values) { | |
Toast.makeText(MainActivity.this,values[0]+ " File(s) downloaded", Toast.LENGTH_SHORT).show(); | |
} | |
@Override | |
protected void onCancelled(Boolean aBoolean) { | |
super.onCancelled(aBoolean); | |
} | |
}; | |
if (v.getId()==R.id.buttonParallel){ | |
asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,url ); | |
} | |
else{ | |
asyncTask.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR,arr ); | |
return; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment