Android上webview的地理定位

Android上webview的地理定位,android,webview,Android,Webview,我有一个webview,我想从javascript运行Android.getLocation()以使用json提供用户的位置,我已经完成了以下工作: MainActivity.java [...] import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; public class MainActivity extends Activity {

我有一个webview,我想从javascript运行Android.getLocation()以使用json提供用户的位置,我已经完成了以下工作:

MainActivity.java

[...]
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

    WebView myWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.setContentView(R.layout.activity_main);

        this.myWebView = (WebView) this.findViewById(R.id.webView);
        myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");

        myWebView.loadUrl("file:///android_asset/example.html");

        WebSettings webSettings = myWebView.getSettings();
        webSettings.setDomStorageEnabled(true);
        webSettings.setDatabaseEnabled(true);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setGeolocationEnabled(true);

        myWebView.setWebViewClient(new MyWebViewClient());
    }
[...]
WebAppInterface.java

import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.webkit.JavascriptInterface;
import org.json.JSONException;
import org.json.JSONObject;

public class WebAppInterface extends Activity {

    Context context;

    public WebAppInterface(Context context) {
        this.context = context;
    }
[...]
private LocationManager locManager;

    @JavascriptInterface
    public JSONObject getLocation() throws JSONException {
        locManager = (LocationManager) getSystemService(this.context.LOCATION_SERVICE); #Me da error en esta linea

        Location loc = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        JSONObject location = new JSONObject();
        location.put("latitude", loc.getLatitude());
        location.put("longitude", loc.getLongitude());

        return location;
    }
[...]
很明显我已经把权限

但是,当从javascript执行时,我在getLocation()的第一行中得到了一个错误,我不知道为什么,显然这似乎没有问题

非常感谢

更改此行:

locManager = (LocationManager) getSystemService(this.context.LOCATION_SERVICE);
为了这个

locManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

查看此以了解更多信息,还有另一个…1。活动派生类应具有无参数构造函数2。不要自己创造活动这会有什么帮助?如果我们使用。。。另外,
this.getSystemService
没有意义,因为
WebAppInterface
实例不是有效的
上下文
。。。