【问题标题】:Android Studio - GoogleAPIClient deprecated fixAndroid Studio - GoogleAPIClient 已弃用修复
【发布时间】:2020-11-26 20:54:59
【问题描述】:

我正在构建一个 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();
    }
}    

【问题讨论】:

    标签: java android-studio mobile google-api deprecated


    【解决方案1】:

    关于您对与 Google API 相关的参考的关注,此 API 可能会帮助您满足您的 GPS 应用需求。

    Location and Context APIs

    • 位置和上下文 API 利用移动设备的传感器和信号来提供对用户操作及其环境的感知,从而实现令人愉悦且引人入胜的体验,从而简化用户交互、提供帮助并帮助用户更好地了解自己。

    • 这包含一个 API 和平台列表,您可以根据需要使用这些 API。

    根据您的情况,您可以检查以下内容:

    1. 地点 API

      为您的用户提供有关他们在哪里、何时在那里的上下文信息。访问各种类别的 1 亿个地点的详细信息。

    2. 地理围栏

      地理围栏结合了对用户当前位置的感知与对用户与可能感兴趣的位置的接近度的感知。

    3. 融合位置提供程序 API 使用省电的 API,根据来自设备传感器的组合信号为您的应用获取位置数据。



    Places API

    • 在开始使用 Places API 之前,您需要一个具有结算帐户并启用 Places API 的项目。 (有关如何使用计费帐户创建项目,请参阅链接)
    • 您需要 get an API key 来验证您的请求。
    • 有关此 API 的更多详细信息,您可以访问提供的链接。

    以下地点请求可用:

    地点搜索会根据用户的位置或搜索字符串返回地点列表。

    地点详情返回有关特定地点的更详细信息,包括用户评论。

    Place Photos 提供对存储在 Google Place 数据库中的数百万张与地点相关的照片的访问。

    地点自动完成会在用户键入时自动填写地点的名称和/或地址。

    查询自动完成为基于文本的地理搜索提供查询预测服务,在用户键入时返回建议的查询。



    Fused Location Provider API

    融合位置提供程序是 Google Play 服务中的位置 API 之一。它管理底层定位技术并提供一个简单的 API,以便您可以在高级别指定要求,例如高精度或低功耗。它还优化了设备对电池电量的使用。

    支持常见的定位场景:

    • Last known location

      包括设置和访问融合位置提供程序的分步程序

    • 位置设置

    • 位置更新

    您可以浏览此参考资料以了解更多信息: https://developer.android.com/training/location

    【讨论】:

      猜你喜欢
      • 2021-09-25
      • 2020-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多