R Fauzdar
R Fauzdar

Reputation: 241

Android :How can i check whether GPS is working in my android device or not?

i am working in android. i am designing an application which is based on GPS location of my device.

Whenever i press any event key in my application i need to check whether my device is getting GPS location all the time or not.

Please help me for this. you may provide weblink for this.

Thank you in advance.

Upvotes: 3

Views: 1321

Answers (2)

Pushpendra Kuntal
Pushpendra Kuntal

Reputation: 6186

You should use this type of code:-

/* Use the LocationManager class to obtain GPS locations */

        LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
        Location location = mlocManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if(location!=null)
        {

           myLongitude = location.getLongitude();
           myLatitude= location.getLatitude();
        }
        else
        {

            myLongitude =0;
            myLatitude= 0;
        }



        LocationListener mlocListener;
        mlocListener = new MyLocationListener();

        mlocManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0, mlocListener);
        Log.v("Checkin_Inspect_activity","cordintes of this place = "+myLatitude+"  "+myLongitude);

and design you class like this:-

 /* Class My Location Listener */
    public class MyLocationListener implements LocationListener

{


@Override

public void onLocationChanged(Location loc)

{


    myLatitude= loc.getLatitude();
    myLongitude=loc.getLongitude();
   }


@Override

public void onProviderDisabled(String provider)

{



 // Toast.makeText( getApplicationContext(),"Gps Disabled",   Toast.LENGTH_SHORT ).show();

}


@Override

public void onProviderEnabled(String provider)

{



 //  Toast.makeText( getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();

}


@Override

public void onStatusChanged(String provider, int status, Bundle extras)

{


}

}

Upvotes: 2

Pratik
Pratik

Reputation: 30855

you can use the LocationListener class which will give the location detail whenever the location was update

here is the simple snippet of LocationListener

LocationListener locationListener = new MyLocationListener();
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 35000, 10, this.locationListener);

Edited

you can check the provider for location for gps like this way

if (manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
   // do something
}else{
  // do another thing
}

check out this tutorial link

http://blog.doityourselfandroid.com/2010/12/25/understanding-locationlistener-android/

Upvotes: 0

Related Questions