Which is the best way to load the data for a single item (read only) detail activity?
Should i use LoaderManager or directly AsyncTask?
Use an Asyntask for best result:
Public class example extends AsyncTask<Void,Void,Void>
{
private ProgressDialog dialog;
public example(Context context){
dialog = new ProgressDialog(context);
}
#Override
protected void onPreExecute() {
dialog.setTitle("Demo");
dialog.setMessage("Please Wait..");
dialog.show();
}
#Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
return null;
}
#SuppressLint("NewApi")
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
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 have created dialog and it inflates xml which gets updated with the info .But the progress dialog is shown behind the dialog which pops up .How do I show those progressdialog on top of dialog with inflated xml.
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dialog();
}
public class loadasync extends AsyncTask<Void, Void, JSONObject> {
ProgressDialog progressDialog ;
#Override
protected JSONObject doInBackground(Void... params) {
}
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
runOnUiThread(new Runnable() {
#Override
public void run() {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("loading");
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
}
});
}
#Override
protected void onPostExecute(JSONObject result) {
runOnUiThread(new Runnable() {
#Override
public void run() {
progressDialog.dismiss();
}
});
}
}
public void dialog() {
dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.dialogxml);
loadasync loadasyncdata=new loadasync();
loadasyncdata.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
dialog.show();
}
I cannot see that you are using the setProgressStyle() in your code. That is:
progessDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
for instance.
So in your code try the following:
public void run() {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("loading");
progressBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
}
Something to consider : OnPostExecute and onPreExecute are both run on the UI thread, so you can remove the runOnUIThread stuff, just do
protected void onPreExecute() {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("loading");
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
}
and respectively in onPostExecute
protected void onPostExecute(JSONObject result) {
progressDialog.dismiss();
}
Should maybe be a comment, but the reputation ...
How to show a dialog box in AsyncTask. Getting BadToketException in dialog.show();
I tried many ways but I could not solve it.
Also tried to pass context to the dialog box in different ways, but it is giving me the same result.
public class RetriveStock extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
message = client.clientReceive(1); // I get data here.
return null;
}
#Override
protected void onCancelled() {
super.onCancelled();
}
#Override
protected void onPostExecute(Void result) {
if (message.contains("AlertExecuted:")) {
final Dialog dialog = new Dialog(CreateAlert.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.display_dialog);// Dialog layout
TextView dialogText = (TextView) dialog.findViewById(R.id.digMsg);
dialogText.setText("Alert Executed!");
Button ok = (Button) dialog.findViewById(R.id.ok);
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
try {
dialog.show(); //WindowManager$BadTokenException
} catch (Exception e) {
e.printStackTrace();
}
}
super.onPostExecute(result);
}
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
Please help.
protected void onPreExecute() {
// TODO Auto-generated method stub
// progressDialog = ProgressDialog.show(this, "", "loading news content");
progressDialog = new ProgressDialog(context , AlertDialog.THEME_HOLO_LIGHT);
progressDialog.setMessage(""+getString(R.string.laodnews));
progressDialog.setIndeterminateDrawable(getResources().getDrawable(R.drawable.animate));
progressDialog.setCancelable(false);
progressDialog.show();
}
start dailoge in pre execute and stop in onpostexecute..
is CreateAlert registered activity in manifest..if not then you have to pass registered activity context
I have AsynTask and show progress dialoge while it is running. Phone receive incoming call. After call stopped the progress dialoge is not showing but the activity layout is dim in such way if the dialoge is showing. Has you any ideas?
I have somethin like this:
Recovery recovery=new Recovery();
recovery.execute();
And asynktask:
public class Recovery extends AsyncTask<String, Void, Integer>{
#Override
protected Integer doInBackground(String... uri) {
publishProgress();
//some code
return Ret;
}
#Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
showDialog(RECOVERY);
}
#Override
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
dismissDialog(RECOVERY);
}
}
use this way it works
ProgressDialog dialog = new ProgressDialog(YourActivity.this);
#Override
protected void onPreExecute()
{
this.dialog.setMessage("Loading Please Wait...");
this.dialog.show()
}
#Override
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
}
I am working on Address book app in android.In my application i import contacts from phonebook in my app.while importing i am showing progress bar.I want to show the contacts being imported on the progressbar while importing.how to do this?
following is my code:-
public class Task extends AsyncTask<String, Integer, Void> {
private ProgressDialog dialog;
protected Context applicationContext;
#Override
protected void onPreExecute()
{
System.out.println("IN PreExecute");
this.dialog = ProgressDialog.show(applicationContext, "Importing Contacts", "Please Wait...", true);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
}
#Override
protected Void doInBackground(String... params) {
// TODO Auto-generated method stub
System.out.println("IN BACKGROUND");
addcontacts();//return flag1;
//dialog.setMessage(name);
return null ;
}
protected void onProgressUpdate(String... progress)
{
System.out.println("IN update");
}
#Override
protected void onPostExecute(Void unused) {
this.dialog.cancel();
System.out.println("IN PostExecute");
final AlertDialog.Builder alertbox1=new AlertDialog.Builder(Import.this);
Cursor c=data.getData();
int num=c.getCount();
alertbox1.setMessage(+num+" contacts imported");
c.close();
// set a positive/yes button and create a listener
alertbox1.setPositiveButton("OK", new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1)
{
call();
}
});
alertbox1.show();
}
onProgressUpdate gets called every time we call publishProgress and the arguments from publishProgress go to onProgressUpdate
So in doInBackground() you can do
protected Void doInBackground(String... params) {
for(int i=1; i<=totalContacts; ++i) {
importNextContact();
publishProgress(i/(float)totalContacts)*100);
}
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
Using dialog.setMessage("msg") is correct but remember that you can not change the ui in the
doInBackground(...) method, so either you post a msg using an handler or you use runOnUiThread.