title: Get GPS data while App is running background in Android using LocationListener
body: <p>i have problem with my Android application.I need an application that will get in background GPS location while open another application.application run in background but i want to also get GPS location in background.</p>
  
  <p>create start and stop button for get gps location it work properly but when click on home button gps stop to gettting location</p>
  
  <p>Here is My code..</p>
  
  <pre><code> public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_gps);
  
              /* get TextView to display the GPS data */
              txtLat = (TextView) findViewById(R.id.textview1);
  
              /* the location manager is the most vital part it allows access 
               * to location and GPS status services */
              locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
              locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER ,10000,10, this);
             // locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10 * 1000L, 0, this);
  
              btnShow = (Button) findViewById(R.id.btnstart);
              btnStop = (Button) findViewById(R.id.btnstop);
  
  
              btnShow.setOnClickListener(new OnClickListener() {
  
                  public void onClick(View arg0) {
                      Toast.makeText(getBaseContext(), "Trip Start.", Toast.LENGTH_SHORT).show();
                      txtLat.setVisibility(View.VISIBLE);
                      onResume();
                      btnShow.setEnabled(false);
                      btnStop.setEnabled(true);
  
                  }
              });
  
              Button btnstop = (Button) findViewById(R.id.btnstop);
              btnstop.setOnClickListener(new OnClickListener() {
                  public void onClick(View v) {
  
                   onPause();
                   Toast.makeText(getBaseContext(), "Trip Ended.", Toast.LENGTH_SHORT).show();
                   btnShow.setEnabled(true);
                   btnStop.setEnabled(false);
                  }
              });
              }
       public void onLocationChanged(Location location) {
  
              if (location == null) {
                    Toast.makeText(getApplicationContext(),"Searching for your location.", Toast.LENGTH_SHORT).show();
  
                    locationManager.requestLocationUpdates(provider, 10000, 10,locationListener);
                    onLocationChanged(location);
               }
               else
               {
  
              double cell_lat=location.getLatitude();
              double cell_long=location.getLongitude();
              double altitude=location.getAltitude();
              double accuracy=location.getAccuracy();                      
  
              String status="true";
  
              sb = new StringBuilder(512);
  
              noOfFixes++;
  
              /* display some of the data in the TextView */
  
              sb.append("Tracking: ");
              sb.append(noOfFixes);
              sb.append('\n');
              sb.append('\n');
  
              sb.append("Londitude: ");
              sb.append(location.getLongitude());
              sb.append('\n');
  
              sb.append("Latitude: ");
              sb.append(location.getLatitude());
              sb.append('\n');
  
              sb.append("Altitiude: ");
              sb.append(location.getAltitude());
              sb.append('\n');
  
              sb.append("Accuracy: ");
              sb.append(location.getAccuracy());
              sb.append("ft");
              sb.append('\n');        
  
              txtLat.setText(sb.toString());
      }
        public void onProviderDisabled(String provider) {
                  /* this is called if/when the GPS is disabled in settings */
                  Log.v(tag, "Disabled");
  
                  /* bring up the GPS settings */
                  Intent intent = new Intent(
                          android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                  startActivity(intent);
  
              }
  
              public void onProviderEnabled(String provider) {
                  Log.v(tag, "Enabled");
                  Toast.makeText(this, "GPS Enabled", Toast.LENGTH_SHORT).show();
  
              }
  
              public void onStatusChanged(String provider, int status, Bundle extras) {
                  /* This is called when the GPS status alters */
                  switch (status) {
                  case LocationProvider.OUT_OF_SERVICE:
                      Log.v(tag, "Status Changed: Out of Service");
                      Toast`enter code here`.makeText(this, "Status Changed: Out of Service",
                              Toast.LENGTH_SHORT).show();
                      break;
                  case LocationProvider.TEMPORARILY_UNAVAILABLE:
                      Log.v(tag, "Status Changed: Temporarily Unavailable");
                      Toast.makeText(this, "Status Changed: Temporarily Unavailable",
                              Toast.LENGTH_SHORT).show();
                      break;
                  case LocationProvider.AVAILABLE:
                      Log.v(tag, "Status Changed: Available");
                      Toast.makeText(this, "Status Changed: Available",
                              Toast.LENGTH_SHORT).show();
                      break;
                  }
              }
       @Override
              protected void onResume() {
                  /*
                   * onResume is is always called after onStart, even if the app hasn't been
                   * paused
                   * 
                   * add location listener and request updates every 1000ms or 10m
                   */
                  locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10f, this);
                  super.onResume();
              }
  
              @Override
              protected void onPause() {
                  /* GPS, as it turns out, consumes battery like crazy */
                  locationManager.removeUp`enter code here`dates(this);
                  super.onPause();
              }
  
  @Override
             public boolean onKeyDown(int keyCode, KeyEvent event) {
                 if ((keyCode == KeyEvent.KEYCODE_HOME)) {
                     System.out.println("KEYCODE_HOME");                                  
                     Toast.makeText(getBaseContext(), "Home Button.", Toast.LENGTH_SHORT).show();                   
                     this.moveTaskToBack(true);
                     //showDialog("'HOME'");                   
                     return true;
                 }
                 if ((keyCode == KeyEvent.KEYCODE_BACK)) {
                     System.out.println("KEYCODE_BACK");                 
                    finish();
                     showDialog("'BACK'");                   
                     return true;
                 }
                 if ((keyCode == KeyEvent.KEYCODE_MENU)) {
                     System.out.println("KEYCODE_MENU");
                     //showDialog("'MENU'");
                     return true;
                 }
                 return false;
             }
  
                 void showDialog(String the_key){
                 AlertDialog.Builder builder = new AlertDialog.Builder(this);
                 builder.setMessage("You have pressed the " + the_key + " button. Would you like to exit the app?")
                       .setCancelable(true)
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                                finish();
                            }
                        })
                        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                 dialog.cancel();
                            }
                        });
                 AlertDialog alert = builder.create();    
                 alert.setTitle("GPS.");
                 alert.show();
             }
  </code></pre>