I tried to get the current location in Android. At least, I took this example from this post.
How do I get the current GPS location programmatically in Android?
While this post is five years old, commonsware told me to ask a new question. Here is my code Snippet.
@Override
public void onCreate(Bundle arg0) {
super.onCreate(arg0);
Log.i(TAG, "onCreate");
setHasOptionsMenu(true);
mActivity = (CustomerTabs) this.getActivity();
mActivity.getActionBar().hide();
if (checkGooglePlayServicesAvailable()){
enableTabs(false);
showDialog(true, null);
requestPosition();
}
boolean checkGooglePlayServicesAvailable() {
final int connectionStatusCode = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(getActivity());
Log.i(TAG,
"checkGooglePlayServicesAvailable, connectionStatusCode="
+ connectionStatusCode);
if (GooglePlayServicesUtil.isUserRecoverableError(connectionStatusCode)) {
//showDialog(false,null);
showGooglePlayServicesAvailabilityErrorDialog(connectionStatusCode);
return false;
}
return true;
}
void showGooglePlayServicesAvailabilityErrorDialog(
final int connectionStatusCode) {
getActivity().runOnUiThread(new Runnable() {
public void run() {
try {
final Dialog dialog = GooglePlayServicesUtil.getErrorDialog(
connectionStatusCode, getActivity(),
101);
if (dialog == null) {
Log.e(TAG,
"couldn't get GooglePlayServicesUtil.getErrorDialog");
}
}catch (Exception e){
e.printStackTrace();
}
}
});
}
void requestPosition(){
Log.i(TAG,"requestPosition");
locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
try {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
60000, 1000,
onLocationChange);
}catch (Exception e){
e.printStackTrace();
showDialog(false, null);
enableTabs(true);
displayMessage(getString(R.string.position_fail));
}
}
LocationListener onLocationChange=new LocationListener() {
@Override
public void onLocationChanged(android.location.Location location) {
Log.i(TAG, "onLocationChange " + location.getLatitude() + "/" + location.getLongitude());
TemporaryPersister.getInstance().setCurrentPosition(new LatLng(location.getLatitude(),location.getLongitude()));
requestLocations();
}
permissions at manifest:
<permission
android:name="versatec.organightandroid.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="versatec.organightandroid.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES "/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
EDIT:
This solution is working on 4.1 (S3 mini) with and without WiFi but not working on another device (S3: without SIM - problem?) in WiFi. The dialog appears, requestPosition() is called - and noting happens...
I got feedback from a tester that its not working (nothing happens) on 4.2 - S4, network available.
Log:
I/Google Maps Android API﹕ Google Play services package version: 6599036
I/Map﹕ onCreate
I/Map﹕ checkGooglePlayServicesAvailable, connectionStatusCode=0
I/Map﹕ showDialog true
I/Map﹕ requestPosition
I/Map﹕ onCreateView
I/Map﹕ onActivityCreated