I got an IntentService that's work is to update the Weather data from Google Awareness API, and pass it back to the Wear companion.
I got the API key, got location rights, and also everything works when the code called from a simple Activity. (in my mainActivity it is the same code, that in the IntentService, it is in the same package, in the same app).
Code:
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 0);
}
mGoogleApiClient = new GoogleApiClient.Builder(this, new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(@Nullable Bundle bundle) {
if (ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
Awareness.SnapshotApi.getWeather(mGoogleApiClient).setResultCallback(new ResultCallback<WeatherResult>() {
@Override
public void onResult(@NonNull WeatherResult weatherResult) {
Log.w("a", "result:" + weatherResult.getStatus().toString());
if (weatherResult.getStatus().isSuccess())
{
Log.w("a", "Weather:" + weatherResult.getWeather().toString());
}
}
});
}
@Override
public void onConnectionSuspended(int i) {
}
}, new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
}).addApi(Awareness.API).build();
mGoogleApiClient.connect();
SO, this code gives me the status ok, and passes correct weather data when It is in the Activity.
The code is ACL_ACCESS_DENIED. Location is enabled, got the permissions.
In GoogleApiClient.Builder tried "this", "getApplicationContext", "getBaseContext", "getApplication". None of them worked.
Anybody got a solution?
Edit: Here is the IntentService's relevant code. But it is similar to the upper code as I wrote:
public class SenderService extends IntentService {
private GoogleApiClient mGoogleApiClient;
private void handleActionWeather() {
mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext(), new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(@Nullable Bundle bundle) {
Log.w("a", "wsconn"); handleActionWeatherSupport(mGoogleApiClient);
}
@Override
public void onConnectionSuspended(int i) {
}
}, new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.w("a", "wsconnfail:" + connectionResult.toString());
}
}).addApi(Wearable.API).addApi(Awareness.API).useDefaultAccount().build();
mGoogleApiClient.connect();
}
private void handleActionWeatherSupport(final GoogleApiClient mGoogleApiClient) {
if (ActivityCompat.checkSelfPermission(getApplication(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
Awareness.SnapshotApi.getWeather(mGoogleApiClient).setResultCallback(new ResultCallback<WeatherResult>() {
@Override
public void onResult(@NonNull WeatherResult wr) {
if (!wr.getStatus().isSuccess()) {
Log.w("a", wr.getStatus().toString());
return;
}
Weather we = wr.getWeather();
Log.w("a", "got:" + we.toString());
}
});
}
...
}