【问题标题】:Does not have sufficient Geolocation Permissions Android Webview没有足够的地理位置权限 Android Webview
【发布时间】:2018-07-18 23:08:27
【问题描述】:

晚上好。我正在使用 Webview 在 Android 上制作跟踪应用程序。我遇到了问题,因为该系统在该位置旁边运行良好。我尝试了 Grishma Ukani 先生的一些方法(非常感谢)。

package com.example.webview;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.GeolocationPermissions;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/**
* A minimal WebView app with HTML5 geolocation capability
*
* @author David M. Chandler
*/
public class GeoWebViewActivity extends Activity {

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

WebView mWebView;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    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.google.com");
}

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

一切顺利,没有留下任何错误。但仍然无法获得设备位置/权限然后此信息出现在 android studio 上

E/cr_LocationProvider:在从系统注册位置更新时捕获安全异常。该应用程序没有足够的地理定位权限。 E/cr_LocationProvider: newErrorAvailable 应用程序没有足够的地理定位权限。

然后我尝试使用来自Insufficient geolocation permissions 的链接进行修改 并将此修改放在我的公共类 GeoWebViewActivity extends Activity

private static final int REQUEST_FINE_LOCATION = 1;
private String mGeolocationOrigin;
private GeolocationPermissions.Callback mGeolocationCallback;

public class GeoWebChromeClient extends WebChromeClient {
    @Override
    public void onGeolocationPermissionsShowPrompt(String origin,
                                                   GeolocationPermissions.Callback callback) {
        // Geolocation permissions coming from this app's Manifest will only be valid for devices with
        // API_VERSION < 23. On API 23 and above, we must check for permissions, and possibly
        // ask for them.
        String perm = Manifest.permission.ACCESS_FINE_LOCATION;
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M ||
                ContextCompat.checkSelfPermission(GeoWebViewActivity.this, perm) == PackageManager.PERMISSION_GRANTED) {
            // we're on SDK < 23 OR user has already granted permission
            callback.invoke(origin, true, false);
        } else {
            if (!ActivityCompat.shouldShowRequestPermissionRationale(GeoWebViewActivity.this, perm)) {
                // ask the user for permission
                ActivityCompat.requestPermissions(GeoWebViewActivity.this, new String[] {perm}, REQUEST_FINE_LOCATION);

                // we will use these when user responds
                mGeolocationOrigin = origin;
                mGeolocationCallback = callback;
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case REQUEST_FINE_LOCATION:
                boolean allow = false;
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // user has allowed this permission
                    allow = true;
                }
                if (mGeolocationCallback != null) {
                    // call back to web chrome client
                    mGeolocationCallback.invoke(mGeolocationOrigin, allow, false);
                }
                break;
        }
    }
}

但出现了一些错误:

  1. 错误:(67, 66) 错误:找不到符号类 NonNull
  2. 错误:(50, 22) 错误: 包构建不存在
  3. 错误:(66, 9) 错误:方法未覆盖或实现超类型中的方法
  4. 错误:(68, 18) 错误:找不到符号方法 onRequestPermissionsResult(int,String[],int[])。

我们将非常感谢您对其他方法的任何帮助和建议。

【问题讨论】:

    标签: java android webview


    【解决方案1】:

    你需要导入NonNull注解。

    import javax.annotation.Nonnull; 添加到您的导入中,您可以在类的顶部找到它们。
    您的类可能在一个包中,您需要在 GeoWebChromeClient 类中声明该包。请注意,package com.example.webview;GeoWebViewActivity 类的顶部有一行package com.example.webview;,表示该类在../com/example/webview 内。如果 GeoWebChromeClient 不再是内部类,您需要类似的包行。

    【讨论】:

    • 很抱歉我不习惯使用 java。你能解释一下吗?你的意思是我应该把 import javax.annotation.Nonnull;打包后?
    • 不。我的公共类 GeoWebChromeClient 扩展了 WebChromeClient 在公共类 GeoWebViewActivity 中扩展了 Activity。接下来我该怎么办?
    • 好的,在包行后面加上import android.os.Build;import javax.annotation.Nonnull;。这应该消除错误 1 ​​和 2
    • 出现更多错误。 1. Error:(3, 24) 错误:包javax.annotation不存在|| 2. Error:(63, 66) error: cannot find symbol class NonNull || 3. Error:(67, 9) 错误:方法没有覆盖或实现超类型中的方法|| 4. Error:(69, 18) error: cannot find symbol method onRequestPermissionsResult(int,String[],int[])
    • *抱歉重复发帖。
    猜你喜欢
    • 1970-01-01
    • 2022-01-07
    • 2017-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-30
    • 2021-01-31
    • 1970-01-01
    相关资源
    最近更新 更多