农村四月闲人少,勤学苦攻把名扬。这篇文章主要讲述我错过了从Android webview打开谷歌地图的东西相关的知识,希望能为你提供帮助。
我是android编程环境的新手,正在创建我的第一个应用程序。我想在webview中点击谷歌地图链接时让webview打开谷歌地图。我错过了为什么这仍然打开webview中的链接而不是执行谷歌地图?当它在webview中打开时,它显示目的地正常,但它没有显示我的位置,并说明它无法确定我在显示的页面底部的位置
这是MainActivity
package com.example.micha.myapplication;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.CookieManager;
import android.webkit.DownloadListener;
import android.webkit.GeolocationPermissions;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {/**
* WebViewClient subclass loads all hyperlinks in the existing WebView
*/
public class GeoWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// When user clicks a hyperlink, load in the existing WebView
view.loadUrl(url);
return true;
}
}/**
* WebChromeClient subclass handles UI-related calls
* Note: think chrome as in decoration, not the Chrome browser
*/
public class GeoWebChromeClient extends WebChromeClient {
@Override
public void onGeolocationPermissionsShowPrompt(String origin,
GeolocationPermissions.Callback callback) {
// Always grant permission since the app itself requires location
// permission and the user has therefore already granted it
callback.invoke(origin, true, false);
}
}private WebView myWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = findViewById(R.id.webView);
WebView myWebView = findViewById(R.id.webView);
WebSettings webSettings = myWebView.getSettings();
myWebView.setWebViewClient(new GeoWebViewClient());
webSettings.setjavascriptEnabled(true);
myWebView.getSettings().setGeolocationEnabled(true);
CookieManager.getInstance().setAcceptCookie(true);
//webSettings.setDomStorageEnabled(true);
myWebView.clearHistory();
myWebView.clearFormData();
myWebView.clearCache(true);
myWebView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if( url.startsWith("http:") || url.startsWith("https:") ) {
return false;
}if(url.contains("https://www.google.com/maps/"))
{
Uri gmmIntentUri = Uri.parse(url);
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
if (mapIntent.resolveActivity(getPackageManager()) != null) {
startActivity(mapIntent);
}
return true;
}// Otherwise allow the OS to handle things like tel, mailto, etc.
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity( intent );
return true;
}});
myWebView.loadUrl("http://www.example.com");
//THIS IS TO DOWNLOAD THE PDF FILES OR OTHER DOWNLOAD LINKS
myWebView.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse(url)));
}
});
}@Override
public void onBackPressed() {
if (myWebView.canGoBack()) {
myWebView.goBack();
return;
}
super.onBackPressed();
}}Here is the AndroidManifest.XML<
?xml version="1.0" encoding="utf-8"?>
<
manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.micha.myapplication">
<
uses-permission android:name="android.permission.INTERNET" />
<
uses-permission android:name="android.permission.ACTION_DIAL"/>
<
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<
uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER"/>
<
uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<
uses-permission android:name="android.permission.ACCESS_GPS" />
<
uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" />
<
uses-permission android:name="android.permission.ACCESS_LOCATION" />
<
uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<
uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<
application
android:allowBackup="true"
android:icon="@mipmap/ic_principal"
android:label="Admin Tools"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.NoActionBar">
<
activity android:name=".MainActivity"
android:screenOrientation="portrait">
<
intent-filter>
<
action android:name="android.intent.action.MAIN" />
<
category android:name="android.intent.category.LAUNCHER" />
<
/intent-filter>
<
/activity>
<
/application>
<
/manifest>
答案发现了问题。
【我错过了从Android webview打开谷歌地图的东西】
if( url.startsWith("http:") || url.startsWith("https:") ) {
阻止谷歌地图打开,因为它强制任何带https的网址都保留在网页浏览量中。将其修改为
if( url.startsWith("http:")) {
现在一切都很好。
推荐阅读
- Appcelerator Webview滚动到底部()
- Android WebView是否需要打开外部URL的权限()
- Android蓝牙打印
- 如何为wpf c#application创建静默安装程序exe()
- Musical React App,我使用EC2,Lightsail还是S3
- Android Google+登录问题
- 如何测试包含Application.Current.Properties.ContainsKey(“token”)的方法
- 如何从客户端(android-java app)发送数据到解析服务器,处理它,将其保存在parse-dashboard中并将结果发送给客户端()
- Parse-platform使用Android加入