【问题标题】:Accessing Google Awareness API for Android Crashes App访问适用于 Android 崩溃应用的 Google Awareness API
【发布时间】:2017-11-23 18:28:07
【问题描述】:

我正在尝试使用 Android Awareness API 来访问天气数据。我的应用程序在显示数据之前崩溃。我相信问题出在 onComplete 方法内部,因为 UI 在崩溃之前确实在屏幕上短暂闪烁,并且我能够将调试器运行到之前的行。

控制台显示“FATAL EXCEPTION: GoogleApiHandler”和“java.lang.SecurityException: Invalid API Key for package” 我正在使用不受限制的 API 密钥来确保问题不是指纹或包名称。我已使用

在清单中包含我的 API 密钥

<meta-data android:name="com.google.android.awareness.API_KEY" android:value="[key here]"/>

我的应用模块 Gradle 脚本还在依赖项中包含“implementation 'com.google.android.gms:play-services-awareness:11.6.0'”。

还有一个警告说 Awareness.API 已被弃用,但我不知道用什么替换它,因为它在文档中使用。

我的代码如下。

public class MainActivity extends AppCompatActivity {

private static int MY_PERMISSION_LOCATION;

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

    if (ContextCompat.checkSelfPermission(
            MainActivity.this,
            Manifest.permission.ACCESS_FINE_LOCATION) ==
            PackageManager.PERMISSION_GRANTED) {

        GoogleApiClient client = new GoogleApiClient.Builder(this.getApplicationContext())
                .addApi(Awareness.API)
                .build();
        client.connect();

        SnapshotClient sc = Awareness.getSnapshotClient(this);
        Task<WeatherResponse> weatherResponseTask = sc.getWeather().addOnCompleteListener(new OnCompleteListener<WeatherResponse>() {
            @Override
            public void onComplete(@NonNull Task<WeatherResponse> task) {
                WeatherResponse wr = task.getResult();
                Weather weather = wr.getWeather();
                float temp = weather.getTemperature(Weather.FAHRENHEIT);
                TextView textView = findViewById(R.id.tempText);
                textView.setText("It is currently " + temp + " degrees outside.");
            }
        });
    } else {
        ActivityCompat.requestPermissions(
                MainActivity.this,
                new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                MY_PERMISSION_LOCATION
        );
        return;
    }
}
}

【问题讨论】:

    标签: java android google-awareness


    【解决方案1】:

    我遇到了同样的问题。我尝试将密钥限制在包中,但仍然遇到“无效密钥”问题。

    最后,这个post 帮助了我。确保 meta_data 标记放在清单中的 application 标记内。

    【讨论】:

    • 感谢有关元数据标签的提示。我确实把它放在了错误的地方,但是在移动它之后我有同样的错误。我的问题与您链接的问题不同,因为他们的应用程序在构建器处崩溃,但我的问题比这更进一步。
    猜你喜欢
    • 1970-01-01
    • 2015-09-11
    • 2013-10-03
    • 2014-01-16
    • 2023-03-08
    • 2018-03-14
    • 1970-01-01
    • 1970-01-01
    • 2019-07-03
    相关资源
    最近更新 更多