2013-10-25 70 views
0

我試圖建立一個Android應用程序,我相處得很好,但我有一個問題,從我正在通過webview查看的網頁調用javascript函數。Android應用程序和網站javascript

我用這個教程中獲得的網頁流量的地理位置:http://turbomanage.wordpress.com/2012/04/23/how-to-enable-geolocation-in-a-webview-android/

現在我想我的頁面中調用這個函數:

var t=setTimeout("navigator.geolocation.getCurrentPosition(foundLocation);",15000); function foundLocation(position) { var lat = position.coords.latitude; var long = position.coords.longitude; window.location.href=' http://rittenservice.nl/rittensysteem2.php?lat= '+lat+'&long='+long+''; }

當這個函數被調用它應該重新加載頁面的網址,所以我可以保存的位置與PHP

有沒有人有一個想法,爲什麼它不工作?

web視圖代碼:你顯示你的網絡視圖之前

public class GeoWebViewActivity extends Activity { 
public class GeoWebViewClient extends WebViewClient { 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     if (url.startsWith("androidurl://")) { 
      url = url.replaceAll("androidurl://", "http://"); 
     } 
     // 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); 
    } 
} 

WebView mWebView; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_geo_web_view); 
    mWebView = (WebView) findViewById(R.id.webView1); 
    // Brower niceties -- pinch/zoom, follow links in place 
    mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); 
    mWebView.getSettings().setBuiltInZoomControls(true); 
    mWebView.setWebViewClient(new GeoWebViewClient()); 
    // Below required for geolocation 
    mWebView.getSettings().setJavaScriptEnabled(true); 
    mWebView.getSettings().setGeolocationEnabled(true); 
    mWebView.setWebChromeClient(new GeoWebChromeClient()); 
    // Load google.com 
    mWebView.loadUrl("http://www.rittenservice.nl/keypad.php"); 
} 

@Override 
public void onBackPressed() { 
    // Pop the browser back stack or exit the activity 
    if (mWebView.canGoBack()) { 
     mWebView.goBack(); 
    } 
    else { 
     super.onBackPressed(); 
    } 
} 

}

+0

你可以在哪裏調用代碼嗎? –

+0

你是什麼意思? PHP的一部分?我不認爲它的necesarry,因爲它不會重新加載頁面 –

回答

0
mWebView.getSettings().setJavaScriptEnabled(true); 

添加該代碼。這使得javascript可以在webview中運行

+0

它已經這樣做,與 mWebView.getSettings()。setGeolocationEnabled(true); –

+0

你可以添加你的代碼。這稱爲網絡視圖 –

+0

我將它添加到原來的帖子 –