[英]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.