[英]Android Studio - GoogleAPIClient deprecated fix
我正在构建一个 GPS 应用程序,但 GoogleAPIClient 已被弃用,我不知道如何修复它。 此外,当我运行该应用程序时,我得到一个不会导致任何结果的登录按钮,这可能与此有关,因为我知道 GoogleSignInClient 存在。 另外,如果您可以将我链接到更详细的 GoogleApi 解释,我将不胜感激!
public static final String MA = "MainActivity";
private final static int REQUEST_CODE = 100;
private GoogleApiClient gac;
private Location location;
private TextView locationTV;
private TextView distanceTV;
private TextView addressET;
private TextView timeLeftTV;
private String destinationAddress = "";
private TravelManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationTV = (TextView) findViewById(R.id.location_tv);
manager = new TravelManager();
addressET = (EditText) findViewById(R.id.destination_et);
distanceTV = (TextView) findViewById(R.id.distance_tv);
timeLeftTV = (TextView) findViewById(R.id.time_left_tv);
gac = new GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build();
}
public void updateTrip(View v) {
String address = addressET.getText().toString();
boolean goodGeoCoding = true;
if (!address.equals(destinationAddress)) {
destinationAddress = address;
Geocoder geocoder = new Geocoder(this);
try {
List<Address> addresses = geocoder.getFromLocationName(destinationAddress, 5);
if (address != null) {
double latitude = addresses.get(0).getLatitude();
double longitude = addresses.get(0).getLongitude();
Location destinationLocation = new Location("destination");
destinationLocation.setLatitude(latitude);
destinationLocation.setLongitude(longitude);
manager.setDestination(destinationLocation);
}
} catch (IOException ioException) {
goodGeoCoding = false;
}
}
FusedLocationProviderApi fusedLocationProviderApi = LocationServices.FusedLocationApi;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
Location current = fusedLocationProviderApi.getLastLocation(gac);
if (current != null && goodGeoCoding) {
distanceTV.setText(manager.milesToDestination(current));
timeLeftTV.setText(manager.timeToDestination(current));
}
}
public void displayLocation() {
FusedLocationProviderApi fusedLocationProviderApi = LocationServices.FusedLocationApi;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
location = fusedLocationProviderApi.getLastLocation(gac);
if (location != null) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
locationTV.setText(latitude + ", " + longitude);
Log.w(MA, "latutude = " + latitude + "; longitude = " + longitude);
}else
locationTV.setText("Error locating the device");
}
public void onConnected(Bundle hint) {
Log.w(MA, "connected");
displayLocation();
}
public void onConnectionSuspended(int cause) {
Log.w(MA, "connection suspended");
}
public void onConnectionFailed(ConnectionResult result) {
Log.w(MA, "connection failed");
if (result.hasResolution()){
try {
result.startResolutionForResult(this, REQUEST_CODE);
}catch (IntentSender.SendIntentException sendIntentException) {
Toast.makeText(this,"Google Play services problem, exiting", Toast.LENGTH_LONG).show();
finish();
}
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
gac.connect();
}
}
protected void onStart() {
super.onStart();
if (gac != null)
gac.connect();
}
}
关于您对与 Google API 相关的参考的关注,此 API 可能会帮助您满足您的 GPS 应用程序需求。
位置和上下文 API 利用移动设备的传感器和信号来提供对用户行为及其环境的感知,从而实现令人愉悦和引人入胜的体验,从而简化用户交互、提供帮助并帮助用户更好地了解自己。
这包含一个 API 和平台列表,您可以根据您的需要使用这些 API 和平台。
在您的情况下,您可以检查以下内容:
地方 API
为您的用户提供有关他们在哪里、何时在那里的上下文信息。 访问各种类别的 1 亿个地点的详细信息。
地理围栏
地理围栏将用户当前位置的感知与用户与可能感兴趣的位置的接近度的感知结合起来。
Fused Location Provider API 使用省电的 API 根据来自设备传感器的组合信号为您的应用程序获取位置数据。
以下地点请求可用:
地点搜索根据用户的位置或搜索字符串返回地点列表。
地点详细信息返回有关特定地点的更多详细信息,包括用户评论。
Place Photos提供对存储在 Google Place 数据库中的数百万张与地点相关的照片的访问。
地点自动完成功能会在用户键入时自动填写地点的名称和/或地址。
查询自动完成为基于文本的地理搜索提供查询预测服务,在用户键入时返回建议的查询。
融合位置提供程序是 Google Play 服务中的位置 API 之一。 它管理底层定位技术并提供简单的 API,以便您可以指定高水平的要求,如高精度或低功耗。 它还优化了设备对电池电量的使用。
支持常见的定位场景:
包括设置和访问融合位置提供程序的分步程序
位置设置
位置更新
您可以浏览此参考以了解更多信息: https://developer.android.com/training/location
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.