The Below is the MainActivity.Java
file. I have imported all the packages required for LocationService but still I am getting the error
"Cannot find Symbol Variable API"
Below is the line where I am getting the error.
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LOCATION_SERVICE.API) //Cannot resolve symbol Variable
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
List of SDK Tools installed along with version.
- Android SDK Build Tools
- Android SDK Tools 24.3.4
- Android Support Repository , rev 19
- Android Support Library, rev 23.0.1
- Google Play Service, rev 26
- Google Repository,rev 21
- Google Play APK expansion library , rev3
- Android Auto API Simulators
Code:
package com.example.android.location2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.location.Location;
import android.widget.TextView;
import android.util.Log;
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;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
private final String LOG_TAG ="LaurenceTestApp";
private TextView txtOutput;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create a GoogleApiClient instance
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LOCATION_SERVICE.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
txtOutput= (TextView) findViewById(R.id.txtOutput);
}
@Override
protected void onStart() {
super.onStart();
// Connect the client.
mGoogleApiClient.connect();
}
@Override
protected void onStop() {
// Disconnecting the client invalidates it.
mGoogleApiClient.disconnect();
super.onStop();
}
@Override
public void onConnected(Bundle bundle) {
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(10); // Update location every second
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onConnectionSuspended(int i) {
Log.i(LOG_TAG, "GoogleApiClient connection has been suspend");
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.i(LOG_TAG, "GoogleApiClient connection has failed");
}
@Override
public void onLocationChanged(Location location) {
Log.i(LOG_TAG, location.toString());
//txtOutput.setText(location.toString());
txtOutput.setText(Double.toString(location.getLatitude()));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}