我目前正在开发一个使用谷歌地图API的android应用程序。
我想知道是否所有的android设备都支持地图API,因为这个api是一个可选的api,它是平台的一个附加组件。
我担心我的应用程序无法在该设备上运行。
我需要知道的是通过编程检测设备是否支持map API,并捕获异常并执行其他操作。
因为,使用地图功能只是我的应用程序的一个功能,我想让那些不支持地图api的设备仍然可以下载和运行我的应用程序,而不会影响我的应用程序的其他功能。
欢迎提出任何意见或建议。
发布于 2010-08-09 00:21:04
感谢你们所有人的帮助!你的所有建议对我都很有用!
我写了一个简单的应用程序,能够部署在非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();
}
这将捕获异常,并告诉我设备无法运行map活动。
发布于 2011-05-25 10:16:33
除了所描述的之外,我还使用了以下解决方案
<uses-library android:name="com.google.android.maps" android:required="false"/>
在另一个答案中:
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);
}
}
发布于 2012-06-05 03:08:23
在尝试任何调用之前,我需要查看该库是否存在,这样我就可以预先填写相关的首选项。这是我想要检查的代码。
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;
}
然后我检查是否安装了com.google.android.maps
。
https://stackoverflow.com/questions/3410475
复制