如何检测Android设备是否支持谷歌地图API

TS.*_*.xy 19 android

我目前正在开发一个使用谷歌地图API的Android应用程序.

我想知道所有的Android设备都支持map API,因为这个api是一个optinal api,它是平台的附加组件.

我担心我的应用程序将无法在该设备上运行.

我需要知道的是以编程方式检测设备支持映射API,并捕获异常并执行其他操作.

因为,使用地图功能只是我的应用程序的功能之一,我想让那些不支持map api的设备仍然可以下载和运行我的应用程序而不影响我的应用程序的其他功能.

欢迎提出任何意见或建议

小智 15

除了描述之外,我还使用了以下解决方案

<uses-library android:name="com.google.android.maps" android:required="false"/>
Run Code Online (Sandbox Code Playgroud)

在另一个答案:

public void mapClick(View view)
{
    try
    {
        // check if Google Maps is supported on given device
        Class.forName("com.google.android.maps.MapActivity");

        this.startActivity(new Intent(this, MyMapActivity.class));
    }
    catch (Exception e)
    {
        e.printStackTrace();
        UIUtils.showAlert(this, R.string.google_maps_not_found);
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 5

我需要在尝试任何调用之前查看该库是否存在,因此我可以事先填写相关的首选项.这是我提出要检查的代码.

public static boolean hasSystemSharedLibraryInstalled(Context ctx,
        String libraryName) {
    boolean hasLibraryInstalled = false;
    if (!TextUtils.isEmpty(libraryName)) {
        String[] installedLibraries = ctx.getPackageManager()
                .getSystemSharedLibraryNames();
        if (installedLibraries != null) {
            for (String s : installedLibraries) {
                if (libraryName.equals(s)) {
                    hasLibraryInstalled = true;
                    break;
                }
            }
        }
    }
    return hasLibraryInstalled;
}
Run Code Online (Sandbox Code Playgroud)

然后我检查是否com.google.android.maps已安装.


TS.*_*.xy 2

感谢大家的帮助!你所有的建议对我都很有用!!

我编写了一个简单的应用程序,能够部署在 None-Google-Map API 模拟器上,并有问题地检测 Google API 的存在。

我所做的是我指定<uses-library android:name="com.google.android.maps" android:required="false" />

(但是android“required”属性仅适用于2.1,不适用于1.6。我需要找出原因。因为当我查看文档时,它说该属性受1.6支持)

因此,我能够将应用程序部署到模拟器上。

其次,我在我的主要活动中创建了一个名为 HelloMaps 的地图活动

try{
     mapActivity = new Intent(TestApp.this, HelloMaps.class); 
     startActivityForResult(mapActivity, 0); 
}catch(NoClassDefFoundError e){
(Toast.makeText(TestApp.this, "Google Map API not found", Toast.LENGTH_LONG)).show(); 
}
Run Code Online (Sandbox Code Playgroud)

这将捕获异常并告诉我设备无法运行地图活动。