1

I am trying to write an app that gets the current location and displays the latitude and longitude realtime. However, when I run this app, the gps doesn't seem to search for a location and try to get a fix. I don't know why this is happening. Have I missed something out? If I open the maps app the location immediately gets a fix. But in my app the gps is not searching for one, or atleast, in the notification bar, it is not showing anything like 'searching for gps' which is usually shown by the maps app.

My main activity:

package com.example.android.location1;

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;

public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, LocationListener{

private final String LOG_TAG = "TestApp";
private TextView txtOutput;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
private static final int REQUEST_PERMISSION = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(LocationServices.API).addConnectionCallbacks(this).build();
    txtOutput = (TextView) findViewById(R.id.txtOutput);
}

@Override
protected void onStart(){
    super.onStart();
    mGoogleApiClient.connect();
}

@Override
protected void onStop(){
    mGoogleApiClient.disconnect();
    super.onStop();
}

@Override
public void onConnected(Bundle bundle){
    mLocationRequest = LocationRequest.create();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setInterval(1000).setMaxWaitTime(2000);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
            requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_PERMISSION);
    }
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}

@Override
public void onConnectionSuspended(int i){
    Log.i(LOG_TAG, "GoogleApiClient connection has been suspended.");
}

@Override
public void onLocationChanged(Location location){
    Log.i(LOG_TAG, location.toString());
    txtOutput.setText(String.valueOf(location.getLatitude()) +  ", " + String.valueOf(location.getLongitude()));
}

}

My manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.location1">

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

    <uses-feature android:name="android.hardware.location.gps"/>
    <uses-feature android:name="android.hardware.location.network"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version"/>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

I have tested this on android 6 and 7. Doesn't work in either of them. Any help would be appreciated.

speedster01
  • 433
  • 3
  • 19

1 Answers1

-1

Acquire a reference to the system Location Manager

LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

Then register the locationupdates and attach it to the listener that you already have implementet

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3, 1, locationListener);

Read about the requestlocationsUpdates method on how often etc you what your location. https://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates

typecode
  • 23
  • 5
  • No your solution has not solved the issue. The problem still persists. – speedster01 Aug 27 '17 at 04:19
  • follow these steps https://developers.google.com/android/guides/setup Check that your mGoogleApiClient gets authenticated and connected in the monitor log and that the onConnected() method is run in your callback GoogleApiClient.ConnectionCallbacks – typecode Aug 27 '17 at 15:05
  • I used the link you provided only to set up the functions initially. I checked; the google client does get connected and the onConnected() method is also run. The locationChanged() method is called too. Just that it is the same location over and over again. The location doesn't seem to be changing, apart from the gps icon not showing in the notification menu. – speedster01 Aug 29 '17 at 12:06