CurrentLocationDemo PrNo31
CurrentLocationDemo PrNo31
xml file
</LinearLayout>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtLat = (TextView) findViewById(R.id.textview1);
locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
this);
}
@Override
public void onLocationChanged(@NonNull Location location) {
txtLat= findViewById(R.id.textview1);
txtLat.setText("Latitude" + location.getLatitude() +", Longitude:" +
location.getLongitude());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Toast.makeText(this,"This Your cururent
Location",Toast.LENGTH_LONG).show();
}
@Override
public void onProviderEnabled(@NonNull String provider) {
Toast.makeText(this," Location Enable",Toast.LENGTH_LONG).show();
}
@Override
public void onProviderDisabled(@NonNull String provider) {
Toast.makeText(this,"Location disable",Toast.LENGTH_LONG).show();
}
}