OnCreateMethod,
LocationManager manager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocationListener listener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
coordinates = new GeoPoint((int)location.getLatitude(), (int)location.getLongitude()).toString();
}
};
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
}
and when i try to show coordinates (setText) in a TextView, it says "null"
Android Manifest,
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
Even though my TextView displays "null", i get a notification on my status bar which says "Searching for GPS"
Here is my approach:
Always use a Asynch Task for getting Coordinates etc.
This is because GPS locking etc takes time and you don't want to bog down your main UI thread. Using a asynch task will enable you to throw a process in the background and auto update the UI field once the method has effectively executed.
Here is a code snippet: For the do in background part of the Asynch task
After the background task has finished executing you can run a onPostExecute method.
In your relevant Main Class you can then call the Aysch Task and ask it to display the results.
Note: this is only the relevant code snippets you need. Refer to my github if you require more detailed approach. Don't forget to upvote and accept answer!
Ps:
I included a couple of pictures of an app I made to illustrate what the code can do don't worry about the map and stuff. It just shows events I have added and shared with friends who have the same app on their phone via a Google Cloud Server.