I have one running some methods in doInBackground in AsyncTask ,which is calling webservices and inserting number of records into the database , at the same user click on other screen then outOfmemoryError is coming I want to stop the background thread temporarily till the screen loads and then thread has to resume from it stopped such that the memory will release. can anybody tell me how to achieve this.
Thanks in Advance.
The Asynctask can be executed only once (an exception will be thrown if a second execution is attempted.).. so to pause and resume .. i think you should use a backgroundthread instead...
//decleration
ProgressDialog dialog;
//onCreate
dialog = new ProgressDialog(ActivityName.this);
public class BackGroundTask extends AsyncTask<String, String, String> {
#Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
//perform background task
return null;
}
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dialog.setMessage("Please wait.....");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
#Override
protected void onPostExecute(String sResponse) {
if (dialog.isShowing())
{
dialog.dismiss();
}
}
Related
I was sitting 5 hours on one code with AyncTask which was not running properly. I just created another simple Activity (because in last one onPostExecute() wasn't working) and now this simple Activity is also not starting the AsyncTask. Can anyone see what I'm doing wrong?
public class ServerStatus extends Activity {
Context context;
private ProgressDialog pd;
int a;
TextView test;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.server_status);
context=this;
test=(TextView) findViewById(R.id.welcomemessage);
new Download().execute();
}
public class Download extends AsyncTask<Void, Void, Void>{
protected Void onPreExecute(Void... arg0) {
pd = new ProgressDialog(context);
pd.setTitle("Processing...");
pd.setMessage("Please wait.");
pd.setCancelable(false);
pd.setIndeterminate(true);
pd.show();
return null;
}
#Override
protected Void doInBackground(Void... arg0) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
a++;
return null;
}
protected Void onPostExecute(Void... arg0) {
if (pd!=null)
pd.dismiss();
test.setText(a);
return null;
}
}
}
Also, does NavigationDrawer block UI thread? Because I can't even update TextView when I implement it.
The methods aren't correct. You should add the #Override annotation to them so it will yell at you when doing it wrong.
onPreExecute() doesn't take any params so it should be
#Override
protected Void onPreExecute() {
also change the param type of `onPostExecute() to
#Override
protected Void onPostExecute(Void arg0) {
Remove the "..." See Varargs for an explanation.
Docs
Post explaining AsyncTask and getting values
those params in the class declaration are for doInBackground(), onProgressUpdate(), and onPostExecute()
As far as the NavDrawer, I'm not sure what issue you are having with that.
You need to change your code in onPreExecute
pd = new ProgressDialog(context);
to
pd = new ProgressDialog(getActivity());
I am displaying progress dialog box on screen,
I can not make any action until the dialog got dismissed.
How can I make some action (like Clicking on button) on screen even if progress dialog box is loading.
Loading should not stop when I click on any of button.
Here is my AsyncTask:
//My Progress Dialog
progress = ProgressDialog.show(DefaultMarketWatch.this, "",
"Loading", true);
// My AsyncTask, Which executes for every 1 sec.
public class RetriveStock extends AsyncTask<Void, Void, ArrayList<User>> {
#Override
protected ArrayList<User> doInBackground(Void... params) {
message += client.clientReceive(1); // Receives data from TCP socket.
return null;
}
#Override
protected void onCancelled() {
super.onCancelled();
}
protected void onPostExecute(ArrayList<User> result) {
progress.dismiss(); // Dismissing my progress dialog
// My UI updations and all using "message" string.
super.onPostExecute(result);
}
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
Please help..
Simply use AsyncTask for getting that data.
Don't use any progress dialog for showing progress.
AsyncTask will run on background not on UI thread so it will not hurdle any of your process while you are fetching or uploading any data.
Try this...
#Override
protected Void doInBackground(Void... params)
{
publishProgress(data to send on UI thread here );
return null;
}
#Override
protected void onProgressUpdate(String... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
// write your UI change code here This will perform on UI therad oh Activity
loading.setMessage("UI change");
}
If you want to do some action while loading the progress dialog means,You are violating the concept of the progress dialog.Because it is also an UI process.Better don't use the progress dialog.Use AsyncTask or runOnUIthread for your background data fetching event and do your stuff on UI.
I am developing one Android application which is connecting to Web Service. How can I keep page loading animation while redirecting to another page ? I have tried following code but whenever I have pressed back button the loading animation is remains on previous page. And sometimes blank screen is coming. Please help me to improve my code and help me to resolve this issue.
public void redirection()
{
ProgressDialog dialog = ProgressDialog.show(MainActivity.this, "","Please wait...", true);
Intent i = new Intent(this, SecondClass.class);
startActivity(i);
}
Follow that link http://www.androidhive.info/2013/06/android-working-with-xml-animations/
Here define the animations
method start
#Override
public void onAnimationStart(Animation animation) {
}
method end
#Override
public void onAnimationEnd(Animation animation) {
}
use asytask in this process...
public class asynclass extends AsyncTask<Void, Void, Void>{
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
ProgressDialog dialog = ProgressDialog.show(MainActivity.this, "","Please wait...", true);
}
#Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
try {
//do your work which u want
}
}
#Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
dialog .dismissDialog();
//and here u can pass intent if u want to pass
}
}
Hope that you are using AsyncTask for to download your data ..
Initiate progress dialog as
Private ProgressDialog pDialog;
Try putting this code on onPreExecute()..
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pDialog = new ProgressDialog(getParent());
pDialog.setMessage("Please wait ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
And on PostExcute stop progress dialog after you completly downloaded data as
pDialog.dismiss();
Supposing I'm using the following AsycTask (called A) for sending data over internet:
private class A extends AsyncTask<Void, Void, Void>{
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pd = ProgressDialog.show(context, "Notification", "Sending message...");
}
#Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
sendMessage();
return null;
}
#Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if(pd!=null && pd.isShowing())
pd.dismiss();
}
}
Inside the doInbackground is called the my method sendMessage(). I will not go into details of this method, just immagine that this method executes a Thread (suppouse that it is called B).
So, the question is:
The code inside the onPostExecute() of AsyncTask A will be executed after the end of the Thread B?
If not, how can make possible that the onPostExecute will be executed only when the AsyncTask inside the sendMessage() will be terminated?
you could let the sendMessage() return the Thread object and then call
Thread.join()
This will cause the doInBackground to wait until the thread is finished.
Well it is pretty simple. Just call that AsyncTask B in onPostExecute of AsyncTask A.
Edit : Thread inside thread is not possible from ICS onwards. It will not give you any error, but it will not work too and your app will stuck there.
i want to add a progress Dialog button when i click on this button before the new activity apperar, i think i don't need a thread, i did search but i find only that i need to do a thread and many other think it s not clear
i just want when i clik on a progress Dialog say to the user to wait so a few sec the other activity will appear that's all:
btn_newsfeed.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Launching News Feed Screen
Intent i = new Intent(getApplicationContext(), CustomizedListView.class);
startActivity(i);
}
});
There are three different different ways in which you can use a ProgressDailog -using threads, handlers and async tasks.
here a example of async task for using a progress Dialog
private class Operation extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params)
{
// code to be executed in background thread
for(int i=0;i<5;i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return "Executed";
}
#Override
protected void onPostExecute(String result) {
// runs on UI thread and updated UI after executing doInBackground
progressDialog.dismiss();
}
#Override
protected void onPreExecute() {
ProgressDialog progressDialog = ProgressDialog.show(MainActivity.this, "Title ", "Loading...");
progressDialog.show();
}
#Override
protected void onProgressUpdate(Void... values) {
// runs on UI thread and starts first
}
}