Android show progress dialog while waiting for location - android

I'm developing location based app using this example: http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/
But when I turn on phone, the location isn't available right on that moment. So I would like to show progress dialog while waiting for the location. Wanna do this in the background using AsyncTask.
Can you give any ideas how and where to do that?

There is no need of AsyncTask because Location service already running in different process, Just implement the LocationListener and register it on resume method, and in onCreateActivity check if location is null, the show the ProgressDialog, and in onLocationChanged() set the location and close the ProgressDialog

Implement locationListner interface and start your wait dialog override onlocation change method and there just cancel the dialog, all the best.
public class MainActivity extends Activity implements LocationListener{
ProgressDialog dialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//-------------------- Start your GPS Reading ------------------ //
dialog = new ProgressDialog(this);
dialog.setMessage("Please wait!");
dialog.show();
}
#Override
public void onLocationChanged(Location arg0) {
dialog.dismiss();
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}

Place your ProgressDialog in onPreExecute, sample code below:
private ProgressDialog progressdialog;
#Override
protected void onPreExecute(){
super.onPreExecute();
progressdialog = new ProgressDialog(yourContext);
progressdialog.setMessage("Loading...");
progressdialog.show();
}
#Override
protected void onPostExecute(){
super.onPostExecute();
progressdialog.dismiss();
}

Related

AsyncTask not even starting

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());

How to start page loading animation and stop on page loaded

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();

unable to show a progress bar properly

i want to show in my activity a progress bar as a response to a button click.
i read in another question that i should use async task in order to show/not show the progress bar but when i click on the button the progress bar is not shown properly (it appears for much less time then it should)
any suggestions?
the activity code:
public void chooseContactFromList(View view){
ProgressBar pBar = (ProgressBar) findViewById(R.id.progressBar1);
circleActivity progressTask = (circleActivity) new circleActivity(pBar).execute();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
CharSequence[] cs=nameList.toArray(new CharSequence[nameList.size()]);
builder.setTitle("Make your selection");
builder.setItems(cs, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
reciverNumber = phoneList.get(item);
}
});
AlertDialog alert = builder.create();
alert.show();
progressTask.cancel(true);
}
the AsyncTask code:
public class circleActivity extends AsyncTask<Void, Void, Void> {
private ProgressBar progressBar;
public circleActivity(ProgressBar pBar) {
progressBar=pBar;
}
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
progressBar.setVisibility(View.VISIBLE);
}
#Override
protected void onPostExecute(Void result) {
progressBar.setVisibility(View.INVISIBLE);
}
#Override
protected void onProgressUpdate(Void ... progress) {
}
#Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
return null;
}
}
thanks
As you are doing nothing in the doInBackground(), the progressBar is shown for few moments only. If you really want to see it, then try doing some operation in doInBackground() which will take some time.
eg. Try Thread.sleep(1000); in doInBackground to test it.
And I suggest you to refer following links.
http://www.vogella.com/articles/AndroidBackgroundProcessing/article.html
http://developer.android.com/reference/android/os/AsyncTask.html

AsyncTask Class Showing Null Pointer Exception in onProgressUpdate Method

I am a beginner in android. I am trying to control a progressbar with AsyncTask in a class that extends android's inbuilt messenger class. I am getting an Exception but, can't understand the fault in my code.
public class MyMessenger extends Service {
private ProgressDialog downloadProgressDialog;
static final int v1=1,v2=2;
ProgressBar bar;
class MyHandler extends Handler
{
#Override
public void handleMessage(Message msg) {
switch (msg.what) {
case v1:
Toast.makeText(getApplicationContext(), "message = 1 in handler of messenger", Toast.LENGTH_LONG).show();
break;
case v2:
new sync().execute();
break;
}
}
}
Messenger messenger=new Messenger(new MyHandler());
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), "binding...", Toast.LENGTH_LONG).show();
return messenger.getBinder();
}
public class sync extends AsyncTask<Void, Integer, Void>
{
int progress=0;
#Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
// super.onProgressUpdate(values);
downloadProgressDialog.setProgress(values[0]);
}
#Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
while (progress<100) {
progress++;
publishProgress(progress);
SystemClock.sleep(1000);
}
return null;
}
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
downloadProgressDialog = new ProgressDialog(getApplicationContext());
downloadProgressDialog.setMessage("Downloading file...");
downloadProgressDialog.setMax(100);
downloadProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
downloadProgressDialog.setCancelable(false);
downloadProgressDialog.show();
}
}
}
Since you are unable to findViewById in service , do the same in your activity that starts the service and make bar as a public static variable.bar is null because you have not initialsed it by findViewById
public static ProgressBar bar = (ProgressBar) findViewById(R.id.progressbar);//in activity
Code in async task
//async task
#Override
protected void onProgressUpdate(Integer... values) {
if(!MyActivityName.bar=null)
MyActivityName.bar.setProgress(values[0]);
super.onProgressUpdate(values);
}
Hope this works.
You never initialize bar, so it is null when you use it at bar.setProgress(values[0]).
because you didn't initialize your progress bar. initialize like below:
bar = (ProgressBar) findViewById(R.id.progressbar);
EDIT:
Then you have to use Progressdialog rather than progress bar like below:
private ProgressDialog downloadProgressDialog;
protected void onPreExecute() {
super.onPreExecute();
downloadProgressDialog = new ProgressDialog(context);
downloadProgressDialog.setMessage("Downloading file...");
downloadProgressDialog.setMax(100);
downloadProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
downloadProgressDialog.setCancelable(false);
downloadProgressDialog.show();
}
and then use dialog object in your progressupdate method.
EDIT:
Via Binder you can send callbacks to your Activity, which means that you can update UI like progress dialog.
Add according method to your Binder (let's name it onProgress)
From your AsyncTask call method of this Binder
In order to know about progress updates consider using Observer pattern (in other words - your Activity should listen for updates of your Binder, or more specifically - of calling Binder.onProgress method)
You can not to show progress dialog , You have extend service class that is used to run thread in background. just use only doinbackground() method, remove progress dialog method
.

show Progress Dialog in OnLocationChanged

I know there are thousands of questions like this. But I can't find an explanation to mine.
I use the onLocationChanged method to update the user's location on a mapView. Everything's fine; I display a ProgressDialog in the onCreate method and dismiss it at the end of OnlocationChanged and it works.
The problem is when I create and show a Progress Dialog inside the onLocationChanged method. Somehow it doesn't work. I think it's because the method runs on a different thread.
But my question is, if the onLocationChanged method runs on a different thread, why does it let me to dismiss the dialog but not create a new one?
Here's part of my class:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tiendas);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setSatellite(false);
ProgressDialog dialog = ProgressDialog.show(StoresActivity.this, "",
"Getting your location", true, true);
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
/////if i show or initialize the dialog here, doesn't work!
updateMap(location) // this function calls asynctask
// for an HTTPrequest
dialog.dismiss();
}
}
That code works and dismisses the dialog correctly, but if I declare or show the dialog inside the onLocationChanged method, it never displays. Anybody?
Why does it dismiss it but can't show it?
Pass the Context of LocationListener class in ProgressDialog.
Check this code its working fine for me
public class LocationFinderActivity extends Activity implements LocationListener{
LocationManager locationManager = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location_finder);
Log.i("inside","oncreate");
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0,this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_location_finder, menu);
return true;
}
#Override
public void onLocationChanged(Location location) {
Log.i("inside","onlocationchange");
ProgressDialog dialog = ProgressDialog.show(LocationFinderActivity.this, "",
"Getting your location", true, true);
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}

Categories

Resources