繁体   English   中英

访问适用于Android的Google Awareness API会导致应用崩溃

[英]Accessing Google Awareness API for Android Crashes App

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

控制台显示“ FATAL EXCEPTION:GoogleApiHandler”和“ java.lang.SecurityException:程序包的无效API密钥”我正在使用不受限制的API密钥以确保问题不是指纹或程序包名称。 我使用以下方式在清单中包含了我的API密钥

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

我的应用程序模块Gradle脚本在依赖项中还包含“实现'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;
    }
}
}

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

最后,这篇文章对我有所帮助。 确保meta_data标记放在清单中的application标记内。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM