1

尝试从 googeleAwareness api 获取天气更新。错误提示:调用需要权限,可能会被用户拒绝:代码应明确检查权限是否可用(使用checkPermission)或明确处理潜在的SecurityException. 这是我的代码:

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 + "");
                }
            });
}

}

我的 manifest.xml 文件的权限:

<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" />
4

0 回答 0