簡體   English   中英

如何檢測Android設備是否支持谷歌地圖API

[英]How to detect an android device whether it supports google maps API

我目前正在開發一個使用谷歌地圖API的Android應用程序。

我想知道所有的Android設備都支持map API,因為這個api是一個optinal api,它是平台的附加組件。

我擔心我的應用程序將無法在該設備上運行。

我需要知道的是以編程方式檢測設備支持映射API,並捕獲異常並執行其他操作。

因為,使用地圖功能只是我的應用程序的功能之一,我想讓那些不支持map api的設備仍然可以下載和運行我的應用程序而不影響我的應用程序的其他功能。

歡迎提出任何意見或建議

除了描述之外,我還使用了以下解決方案

<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);
    }
}

我需要在嘗試任何調用之前查看該庫是否存在,因此我可以事先填寫相關的首選項。 這是我提出要檢查的代碼。

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

你是對的,有些設備,比如Archos 5IT和Some Tablet沒有谷歌地圖,Android Market等......

這是我的代碼。 BelgianMap是一個MapActivity:

try {
    Intent i = new Intent(InfoGare.this, BelgianMap.class);
    startActivityForResult(i, 0);
}
catch(ActivityNotFoundException e) {
    Toast.makeText(context, "Google Map not found", Toast.LENGTH_LONG).show();
}

但也許還有另一種方法可以在創建應用程序時找到它並顯示消息。 那可能會更好。

謝謝你們所有人的幫助! 你的所有建議都對我有用!!

我寫了一個簡單的應用程序,它可以部署在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(); 
}

這將捕獲異常並告訴我該設備無法運行地圖活動。

這對我有用:

在清單中使用(未記錄的?)uses-library android:name =“com.google.android.maps” android:required =“false” 這將允許在maps-api不支持的設備中安裝。

然后查看: http//android-developers.blogspot.com.ar/2009/01/can-i-use-this-intent.html

您仍然可以在應用程序中保留所有map-api代碼(不需要2個版本),只需對UI進行編碼以防止運行它。 (在我的情況下,我灰暗了一個偏好)。 如上所述,上面概述的技術有效地預測了一些不起作用的東西,並允許您響應(沒有FC),而不僅僅是對問題作出反應。

據我所知,可選API僅適用於模擬器。 2個不同版本的API級別僅適用於模擬器,真實設備應該都具有我認為的地圖API

如果您在應用程序中使用googlemaps,則應在應用程序標記內的manifest.xml中包含以下內容

 <uses-library android:name="com.google.android.maps" />

這將阻止用戶安裝您的應用程序或在市場上看到它,不確定哪個,也許兩者都有。

通過使用adb ,要打印所有系統庫:

adb shell cmd package list libraries

如果您的設備支持Google地圖,則會看到以下行:

library:com.google.android.maps

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM