0

Hai i develop a application for getting the Gps value its woking fine.But i faced some problem

problem

1.when mobile is screen locked

2.when mobile is swtched off into switch on

3.when mobile display light is off

MyRequirements

Anytime i want to get the gps value ,except the mobile is switched of

 Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, true);
networkLoc = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (networkLoc != null) {
double l = (double) (networkLoc.getLatitude());
double lng11 = (double) (networkLoc.getLongitude());
latituteField1.setText(Double.toString(l));
 longitudeField1.setText(Double.toString(lng11));
 } 
 locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
  }
  @Override
  protected void onResume() {
super.onResume();

locationManager.requestLocationUpdates(
        LocationManager.NETWORK_PROVIDER, 
        MINIMUM_TIME_BETWEEN_UPDATES, 
        MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
        this
    );
    }
   @Override
  protected void onPause() {
super.onPause();
locationManager.requestLocationUpdates(
        LocationManager.NETWORK_PROVIDER, 
        MINIMUM_TIME_BETWEEN_UPDATES, 
        MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
        this
   );
   }
@Override
  public void onLocationChanged(Location location) {
lat1 = (double) (networkLoc.getLatitude());
lng1 = (double) (networkLoc.getLongitude());
  latituteField1.setText(Double.toString(lat1));
  longitudeField1.setText(Double.toString(lng1));      
  }
  @Override
  public void onStatusChanged(String provider, int status, Bundle extras) {

  }
   @Override
  public void onProviderEnabled(String provider) {
Toast.makeText(this, "Enabled new provider " + provider,
        Toast.LENGTH_SHORT).show();
       }

    @Override
  public void onProviderDisabled(String provider) {
Toast.makeText(this, "Disenabled provider " + provider,
        Toast.LENGTH_SHORT).show();
   }
Mercy
  • 1,862
  • 9
  • 39
  • 75

2 Answers2

0

You can use TimerTask Class and Android's Alarm Class. In the TimerTask's Run method keep fetching gps data on particular interval ( suppose on every minute ). And use Alarm Class for closing LocationListener Class & Opening on every hour. This way your application will work very efficiently. I used same for my application. For Auto start on Switch on Mobile, you can use Android Services.

TimerTask's Example is here. AlarmManager's Example is here.

Lucifer
  • 29,392
  • 25
  • 90
  • 143
0

Usually the device powers down the screen after some time and than also the CPU. If this happens you will not get any more updates from the GPS. To prevent this behavior you need to aquire a WakeLock.

Wakelocks are described here: http://developer.android.com/reference/android/os/PowerManager.html

And here is another article on WakeLocks: How to get an Android WakeLock to work?

Community
  • 1
  • 1
Stefan
  • 4,645
  • 1
  • 19
  • 35