Last active
October 20, 2024 01:36
-
-
Save SamukaDEV/204f6e695a4fa367c908833420570c7b to your computer and use it in GitHub Desktop.
Kotlin GPS integration (not tested)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// reference: https://stackoverflow.com/questions/46906017/getting-location-offline | |
/* | |
Android Permissions needed: | |
<uses-permission android:name="android.permission.INTERNET" /> | |
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> | |
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> | |
*/ | |
public class Location extends AppCompatActivity { | |
LocationManager locationManager; | |
Context mContext; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_location); | |
mContext=this; | |
locationManager=(LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE); | |
locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, | |
2000, | |
10, locationListenerGPS); | |
isLocationEnabled(); | |
} | |
LocationListener locationListenerGPS=new LocationListener() { | |
@Override | |
public void onLocationChanged(android.location.Location location) { | |
double latitude=location.getLatitude(); | |
double longitude=location.getLongitude(); | |
String msg="New Latitude: "+latitude + "New Longitude: "+longitude; | |
Toast.makeText(mContext,msg,Toast.LENGTH_LONG).show(); | |
} | |
@Override | |
public void onStatusChanged(String provider, int status, Bundle extras) { | |
} | |
@Override | |
public void onProviderEnabled(String provider) { | |
} | |
@Override | |
public void onProviderDisabled(String provider) { | |
} | |
}; | |
protected void onResume(){ | |
super.onResume(); | |
isLocationEnabled(); | |
} | |
private void isLocationEnabled() { | |
if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){ | |
AlertDialog.Builder alertDialog=new AlertDialog.Builder(mContext); | |
alertDialog.setTitle("Enable Location"); | |
alertDialog.setMessage("Your locations setting is not enabled. Please enabled it in settings menu."); | |
alertDialog.setPositiveButton("Location Settings", new DialogInterface.OnClickListener(){ | |
public void onClick(DialogInterface dialog, int which){ | |
Intent intent=new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); | |
startActivity(intent); | |
} | |
}); | |
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){ | |
public void onClick(DialogInterface dialog, int which){ | |
dialog.cancel(); | |
} | |
}); | |
AlertDialog alert=alertDialog.create(); | |
alert.show(); | |
} | |
else{ | |
AlertDialog.Builder alertDialog=new AlertDialog.Builder(mContext); | |
alertDialog.setTitle("Confirm Location"); | |
alertDialog.setMessage("Your Location is enabled, please enjoy"); | |
alertDialog.setNegativeButton("Back to interface",new DialogInterface.OnClickListener(){ | |
public void onClick(DialogInterface dialog, int which){ | |
dialog.cancel(); | |
} | |
}); | |
AlertDialog alert=alertDialog.create(); | |
alert.show(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment