I have an AsyncTask that is created in onCreate() it is called GatherText() I have an animation that slides a TextView out of view. When that animation ends I want it to call the AsyncTask.
Here is a basic idea of my code
public class MainActivity extends Activity implements AnimationListener {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
class GatherText extends AsyncTask<JSONObject, String, JSONObject> {
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
}
}
#Override
protected void onPostExecute(JSONObject json) {
}//end onPostExecute
}//end GatherText Asynctask
}//end onCreate
#Override
public void onAnimationEnd(Animation animation) {
if(animation == slideout){
//THIS IS WHERE THE ERROR OCCURS
new GatherText().execute();
}
}//End onAnimationEnd
}//end Main Activity
In the onAnimationEnd() method I receive an error that says GatherText() can not be resolved to a type. How can I call GatherText() outside of onCreate?
Put your asynctask class outside of onCreate
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}//end onCreate
class GatherText extends AsyncTask<JSONObject, String, JSONObject> {
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
}
}
#Override
protected void onPostExecute(JSONObject json) {
}//end onPostExecute
}//end GatherText Asynctask
#Override
public void onAnimationEnd(Animation animation) {
if(animation == slideout){
gathertext = new GatherText();
new GatherText().execute();
}
}//End onAnimationEnd
Related
I'm developing an app that when user clicks on a Button it executes an Asynctask that this class calls a method at it's onPostExecute method that calls another Asynctask! It works when user clicks once, but at the second time it crashes and says Cannot execute task: the task has already been executed (a task can be executed only once)
.
public class Test extends Activity {
A a;
B b;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
a = new A();
a.execute();
}
});
}
class A extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... arg0) {
// doing st
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
runB();
a.cancel(true);
}
}
public void runB() {
b = new B();
b.execute();
}
class B extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... arg0) {
// doing st
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
//doing st
b.cancel(true);
}
}
Use AsyncTask.Status for checking status of AsyncTask before calling AsyncTask.execute() method on Button onClick:
#Override
public void onClick(View arg0) {
if(a ==null){
a = new A();
a.execute();
}
}
And in onPostExecute of B class assign null to a :
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
//doing st
b.cancel(true);
a=null;
}
I'm practicing on async tasks. I want to use intent while the program is performing some calculation.
I can't use the intent in onClick, probably in onProgressUpdate.
public class MainActivity extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnStart=(Button) findViewById(R.id.btnStart);
Button btnAsync=(Button) findViewById(R.id.btnAsync);
btnStart.setOnClickListener(this);
btnAsync.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnStart:
doLongTaskOnMain(50001);
break;
case R.id.btnAsync:
LongTask task=new LongTask();
task.execute(50000);
break;
}
}
private void doLongTaskOnMain(int number){
TextView textOutput=(TextView) findViewById(R.id.textOutput);
textOutput.setText("Calculating sum of :"+number);
long sum=0;
for(int i=0;i<number; i++){
sum+= i;
textOutput.setText("Progress"+100f*i/number);
}
textOutput.setText("Sum: "+sum);
}
class LongTask extends AsyncTask<Integer, Float, Long>{ //<Params, Progress, Result>
#Override
protected void onPreExecute() {
TextView textOutput=(TextView) findViewById(R.id.textOutput);
textOutput.setText("Start");
}
#Override
protected Long doInBackground(Integer... params) {
int number= params[0];
long sum=0;
for(int i=0;i<number;i++){
sum+=i;
publishProgress(100f*i/number);
}
return sum;
}
#Override
protected void onProgressUpdate(Float... values) {
Float progress=values[0];
TextView textOutput=(TextView) findViewById(R.id.textOutput);
textOutput.setText("progress "+Math.round(progress)+"%");
Button btnNext=(Button) findViewById(R.id.btnNext);
}
#Override
protected void onPostExecute(Long result) {
TextView textOutput=(TextView) findViewById(R.id.textOutput);
textOutput.setText("sum: "+result);
}
}
}
The Second Activity:
public class SecondActivity extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Button btnBack= (Button) findViewById(R.id.btnBack);
btnBack.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
Intent intent=new Intent(this,MainActivity.class);
startActivity(intent);
}
}
How can I move to the second activity without stopping the calculation?
You can make your AsyncTask a standalone class, and have it an Activity member (better a weak reference though). Then, in your first activity's onCreate() you set that member to the first Activity, in a second Activity's onCreate() to the 2nd activity.
This way your onPostExecute will have the correct activity.
You can probably use an event bus instead such as Green Robot
As below code shows I have a function named record() I want to call this function with asynctask but I do not know how to work with asynctask, record function takes long to do some tasks so I need to use asynktsak.
public class Record extends Activity {
MediaPlayer mp;
String name;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.record);
mp = new MediaPlayer();
record();
}
public void record() {
.
.
.
}
}
Try this-
public class Record extends Activity {
MediaPlayer mp;
String name;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.record);
mp = new MediaPlayer();
new Task1().execute();
}
class Task1 extends AsyncTask<Void, Void, String> {
#Override
protected void onPreExecute()
{
super.onPreExecute();
}
#Override
protected String doInBackground(Void... arg0)
{
//Record method
}
#Override
protected void onPostExecute(String result)
{
super.onPostExecute(result);
}
}
}
Make AsyncTask Class and write code there and call your AsyncTask Class by new AsyncTaskClass().execute();
Try below code for your reference.
public void record() {
new AsyncTask<Void, Void, Void() {
#Override
protected void doInBackground(Void... params) {
}
Override
protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
}
}.execute();
}
When I call only wpCategories = JsonToElement.getllAllCategory(); in the buttons onClick methood it works fine. (wpCategories get filled with data). But when I put this in a asynctask, wpCategories returns null. (is the doInBackground not being called?)
Here is my buttons on click methood:
public void onImageGridClick(View view) {
new GetJsonElementTask().execute();
Intent intent = new Intent(this, CategoryGridActivity.class);
intent.putParcelableArrayListExtra(Extra.IMAGES, wpCategories);
startActivity(intent);
}
And the asyncTask:
private class GetJsonElementTask extends AsyncTask<Void, Integer, String> {
#Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(HomeActivity.this, "",
"Loading...");
}
#Override
protected String doInBackground(Void... arg0) {
wpCategories = JsonToElement.getllAllCategory();
return null;
}
#Override
protected void onPostExecute(String value) {
progressDialog.dismiss();
}
#Override
protected void onProgressUpdate(Integer... progress) {
Log.v("DEBUG_LOG", "In onProgressUpdate");
}
}
Start your Activity inside onPostExecute because this method execute after doInBackground execution complete :
#Override
protected String doInBackground(Void... arg0) {
wpCategories = JsonToElement.getllAllCategory();
return wpCategories; //<<< return value from here
}
#Override
protected void onPostExecute(String value) {
progressDialog.dismiss();
Intent intent = new Intent(this, CategoryGridActivity.class);
intent.putParcelableArrayListExtra(Extra.IMAGES, value);
startActivity(intent);
}
Try following
public void onImageGridClick(View view) {
new GetJsonElementTask().execute();
}
public void startCategoryGridActivity(){
Intent intent = new Intent(this, CategoryGridActivity.class);
intent.putParcelableArrayListExtra(Extra.IMAGES, wpCategories);
startActivity(intent);
}
And the asyncTask:
private class GetJsonElementTask extends AsyncTask<Void, Integer, String> {
#Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(HomeActivity.this, "",
"Loading...");
}
#Override
protected String doInBackground(Void... arg0) {
wpCategories = JsonToElement.getllAllCategory();
return null;
}
#Override
protected void onPostExecute(String value) {
progressDialog.dismiss();
startCategoryGridActivity();
}
#Override
protected void onProgressUpdate(Integer... progress) {
Log.v("DEBUG_LOG", "In onProgressUpdate");
}
}
I am making a loading effect when calling MainActivity. I have no idea why my Dialog.show is not working in the AsyncTask. All i just see the just the instant when it dismiss, but the dialog never appear before that.
Thank you.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new LoadViewTask().execute();
setContentView(R.layout.activity_main);
....}
private class LoadViewTask extends AsyncTask<Void, Integer, Void>
{
#Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(MainActivity.this,"Loading...","Loading application View, please wait...", false, false);
}
#Override
protected Void doInBackground(Void... params)
{
try
{
synchronized (this)
{
int counter = 0;
while(counter <= 4)
{
this.wait(1000);
counter++;
publishProgress(counter*25);
}
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
return null;
}
#Override
protected void onProgressUpdate(Integer... values)
{
progressDialog.setProgress(values[0]);
}
#Override
protected void onPostExecute(Void result)
{
progressDialog.dismiss();
}
}
You should use FragmentDialogs for using dialogs in Android.
Here it is well explained:
http://developer.android.com/intl/es/reference/android/app/DialogFragment.html
Try to set the content view before starting your async task:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the content view first
setContentView(R.layout.activity_main);
new LoadViewTask().execute();
....}