How I use the Progress bar

Asked

Viewed 8,939 times

2

I have an android app, which brings latitude and longitude. I wish the moment he’s ordering him to put up a bar for feedback .

follows the location code

 private void start(){
         locationManager = (LocationManager)
                getSystemService(Context.LOCATION_SERVICE);

        locationListener = new MyLocationListener();
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, locationListener);
 }

 private class MyLocationListener implements LocationListener {
        @Override
        public void onLocationChanged(Location loc) {

            String longitude = "Longitude: " + loc.getLongitude();
            String latitude = "Latitude: " + loc.getLatitude();

        txtTextos =  (TextView) findViewById(R.id.txtUpdate);


            txtTextos.setText(txtTextos.getText().toString() + "\n Longitude: " + loc.getLongitude() + "\n Latitude : " + loc.getLatitude());


        }
  • Here is an example, which language you are using, and java? https://docs.oracle.com/javase/tutorial/uiswing/components/progress.html

  • Yes, and for android

1 answer

7


When you need to do some tasks that need loading like network operations or something that might look like your application is stuck, you should use a separate thread for this. But this other thread cannot update the graphical interface, IE, it should not change the content of any visual component. To get around this problem, there is the Asynctask class, which allows us to implement the code that will be executed before, during and after performing some task performed in the background, you can place this class within your Activity. Follows code

 import android.location.Location;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;

public class testeActivity extends ActionBarActivity {

    ProgressBar mProgressBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_teste);

        mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
    }

    private void exibirProgress(boolean exibir) {
        mProgressBar.setVisibility(exibir ? View.VISIBLE : View.GONE);
    }

    class MinhaTask extends AsyncTask<Location,Void,Location> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            exibirProgress(true);
        }

        @Override
        protected Location doInBackground(Location... params) {
            return medotodoRetornaLocation();
        }

        @Override
        protected void onPostExecute(Location location) {
            super.onPostExecute(location);
            exibirProgress(false);
            metodoAtualizaAInterfaceGrafica(location);
        }
    }
}

The display method displayProgress(Boolean); is the method responsible for whether or not to display the Progress in the view.

Preview statement in view:

<ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleLarge"
        android:layout_centerInParent="true"
        android:visibility="gone"
        android:id="@+id/progressBar"
        />

Now add the methods that update the graphical interface and what will fetch the location and be happy! o/

To learn more about the Asynctask class follow link: http://developer.android.com/reference/android/os/AsyncTask.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.