有效的方法来测试Android位置服务的可用性以及如何测试它?

时间:2014-02-12 04:55:03

标签: android google-play-services

我正在开发一个使用位置服务的Android应用程序。从给出here的文档中,我了解到位置服务是google play services apk的一部分。

我的疑问是:

1.在使用位置服务之前是否有必要测试Google Play服务的可用性,或者我可以通过使用以下代码检查位置管理器返回的是否为null来直接测试。

locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);

哪种方法有效?

2.播放服务有可能随时无法使用吗?

3.如果我需要测试Google Play服务是否可用,  我如何安全地模拟谷歌的不可用行为  我的设备上播放服务apk,以测试我的代码?

提前致谢

1 个答案:

答案 0 :(得分:1)

在我看来,检查设备中是否提供Google Play服务总是好的,如果没有,则提示安装它,这将完全处理您的情况3。一旦您在设备中安装了该设备,就没有可能无法使用它,如果由于任何原因不可用,那么再次检查将提示您安装它,所以你不必考虑它符合你的条件2.

对于条件1,我想说,首先检查谷歌播放服务的可用性,然后像上面的代码一样检查位置服务。 我们来看看如何查看Google Play服务的可用性:

// Getting status
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());

    // Showing status
    if(status == ConnectionResult.SUCCESS)
        tvStatus.setText("Google Play Services are available");  //success message
    else
    {
        tvStatus.setText("Google Play Services are not available");  // not success
        int requestCode = 10;
        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
        dialog.show();   //this will prompt to install
    }
相关问题