0

I've this code and it's working :

 if(MyLocationListener.latitude>0){
                MyToast.makeText(Weather.this,Double.toString(MyLocationListener.latitude));
                MyToast.makeText(Weather.this, Double.toString(MyLocationListener.longitude));
                lat=MyLocationListener.latitude;
                lon=MyLocationListener.longitude;
                Log.v("this","lat "+Double.toString(lat) );
                Log.v("this","lon "+Double.toString(lon) );
             }

I need to call a class when gps connected . The above code is in a button , so user should click on button every time to know if the gps is connected or not !!

How can I call a class when the gps is connected ?

thanks

user3718930
  • 381
  • 1
  • 8
  • 19

3 Answers3

4

This code might be help you

Add the ACCESS_FINE_LOCATION permission to your manifest Get an instance of the system LocationManager Create a GpsStatus.Listener that reacts to GPS_EVENT_SATELLITE_STATUS Register the listener with LocationManager with addGpsStatusListener

GpsStatus.Listener listener = new GpsStatus.Listener() { void onGpsStatusChanged(int event) {

        if (event == GPS_EVENT_SATELLITE_STATUS)    
        {

            GpsStatus status = mLocManager.getGpsStatus(null);
            Iterable<GpsSatellite> sats = status.getSatellites();
            // Check number of satellites in list to determine fix state
        }
    }
}
vadher jitendra
  • 590
  • 5
  • 16
0

i think this code help u in your ploblem

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Above is permition you have to add in your manifest.xml

following is code

public class ExampleApp extends Activity {
/** Called when the activity is first created. */
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
        Toast.makeText(this, "GPS is Enabled in your devide", Toast.LENGTH_SHORT).show();
    }else{
        showGPSDisabledAlertToUser();
    }
}

private void showGPSDisabledAlertToUser(){
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setMessage("GPS is disabled in your device. Would you like to enable it?")
    .setCancelable(false)
    .setPositiveButton("Goto Settings Page To Enable GPS",
            new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int id){
            Intent callGPSSettingIntent = new Intent(
                    android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(callGPSSettingIntent);
        }
    });
    alertDialogBuilder.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int id){
            dialog.cancel();
        }
    });
    AlertDialog alert = alertDialogBuilder.create();
    alert.show();
}

}

Vijay Vala
  • 73
  • 4
0

Add permission in Manifest

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Try This in your Activity:

  LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );

        if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
            buildAlertMessageNoGps();
        }

      private void buildAlertMessageNoGps() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Your GPS seems to be disabled, do you want to enable it?")
               .setCancelable(false)
               .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                   public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
                       startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                   }
               })
               .setNegativeButton("No", new DialogInterface.OnClickListener() {
                   public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
                        dialog.cancel();
                   }
               });
        final AlertDialog alert = builder.create();
        alert.show();
    }
Namrata
  • 1,683
  • 1
  • 17
  • 28