我已经使用以下代码来打开像 OLA 应用程序这样的 gps。下面的代码在带有播放服务 8.4.0 的 Android Studio 中成功运行。但是,我想在 Eclipse 中使用 ADT 23 运行它。
我在 sdk 中搜索了最新的 google playservices。它不可用。然后,最后,我找到了最新的 aar8.4.0 文件。在我将 AAR 转换为普通的 Eclipse 库项目并导入到 Eclipse 并刷新并作为库添加到我的主项目中之后。
仍然收到此错误。主要源代码活动不支持以下 API。
导入 com.google.android.gms.common.ConnectionResult;导入 com.google.android.gms.common.api.GoogleApiClient;导入 com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;导入 com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;导入 com.google.android.gms.location.LocationRequest;
我的代码:
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); 设置内容视图(R.layout.main);设置请求();} //------------------------------------------------ -------------------------------------
public void settingsrequest()
{
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(30 * 1000);
locationRequest.setFastestInterval(5 * 1000);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
builder.setAlwaysShow(true); //this is the key ingredient
PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
final LocationSettingsStates state = result.getLocationSettingsStates();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
// All location settings are satisfied. The client can initialize location
// requests here.
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied. But could be fixed by showing the user
// a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
status.startResolutionForResult(this, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
// Ignore the error.
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// Location settings are not satisfied. However, we have no way to fix the
// settings so we won't show the dialog.
break;
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
// Check for the integer request code originally supplied to startResolutionForResult().
case REQUEST_CHECK_SETTINGS:
switch (resultCode) {
case Activity.RESULT_OK:
//startLocationUpdates();
GetLocation() ;
break;
case Activity.RESULT_CANCELED:
settingsrequest();//keep asking if imp or do whatever
break;
}
break;
}
}
//------------------------------------------------ ---------------------
注意:此代码在 Android Studio 2.1 中完美运行。并看到输出,例如直接使用对话框是/否本身打开 gps,而无需转到设置页面。
但是,我想要 Eclipse 中的相同输出。我也发现了问题,在哪里?问题仅适用于 Google Play 服务。现在 Google API 仅提供 AAR 格式的库(工作室可接受)。
请帮助获得解决方案。