這裏是我迄今爲止所發現的不同的方法:
1.直接啓動導航:
這明確意圖將直接啓動谷歌導航:
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("google.navigation:q=my+street+address");
startActivity(intent);
這將直接啓動導航(不幸的是沒有給用戶選擇交通工具)
。
2.讓用戶選擇的交通方式:
這將啓動谷歌導航應用,但讓用戶選擇啓動導航前的運輸方式:
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps/?daddr=my+street+address");
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent);
。
3.啓動谷歌地圖上:
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("geo:0,0?q=my+street+address");
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent);
這將打開地圖,在這裏用戶可以啓動導航(並選擇運輸方式)。
在任何情況下,您都應該使用PackageManager和queryIntentActivities()或異常處理來處理用戶未安裝Google Maps/Navigation的情況。
在我的應用程序中,我使用的方法2工作得很好。希望這可以幫助。
附加組件:這是檢查應用程序是否安裝的方法。我在調用intent.setClassName()之前檢查是否安裝了「com.google.android.apps.maps」。
public static boolean isAppInstalled(String uri) {
PackageManager pm = getContext().getPackageManager();
boolean app_installed = false;
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
app_installed = true;
} catch (PackageManager.NameNotFoundException e) {
app_installed = false;
}
return app_installed;
}
只是一個說明 - 強制街景需要'com.google.android.street'包和'com.google.android.street.Street'活動;意圖在http://stackoverflow.com/questions/3447723/about-android-google-api-streetview中描述 – tomash