1

Trying to get weather update from googeleAwareness api.Error saying:Call requires permission which may be rejected by user: code should explicitly check to see if permission is available (with checkPermission) or explicitly handle a potential SecurityException. Here is my code:

public class MainActivity extends AppCompatActivity {

private static final String TAG = "Awareness";
private GoogleApiClient mGoogleApiClient;
private TextView tv;

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

    mGoogleApiClient = new GoogleApiClient.Builder(MainActivity.this)
            .addApi(Awareness.API)
            .build();
    mGoogleApiClient.connect();
    initSnapshots();
}

private void initSnapshots() {
    Awareness.SnapshotApi.getWeather(mGoogleApiClient)
            .setResultCallback(new ResultCallback<WeatherResult>() {
                @Override
                public void onResult(@NonNull WeatherResult weatherResult) {
                    if (!weatherResult.getStatus().isSuccess()) {
                        tv.setText("weather failed");
                        return;
                    }
                    Weather weather = weatherResult.getWeather();
                    tv.setText(weather + "");
                }
            });
}

}

Permissions of my manifest.xml files:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  • 1
    Possible duplicate of [Android marshmallow request permission?](https://stackoverflow.com/questions/33666071/android-marshmallow-request-permission) – Pavneet_Singh Oct 20 '17 at 06:35
  • 1
    You have to request the permission, since they could not even be granted on installation, or they could be revoked at any time. And don't make an image next time. You should always paste the code, so people can copy and try it. Check this: https://developer.android.com/training/permissions/requesting.html?hl=en-419 – JMedinilla Oct 20 '17 at 06:37
  • 1
    Avoid using screen shots to share code, use text and format it instead. Allows us to copy paste to reproduce it. – Veneet Reddy Oct 20 '17 at 06:37

0 Answers0