Несколько недель назад, когда пользователь нажал кнопку «Показать мое местоположение» на на веб-странице Android отображается запрос на разрешение приложению загрузить текущую позицию, но на данный момент отображается только ошибка: Ошибка геолокации: пользователь отклонил геолокацию.
Я посмотрел здесь, в SO и в Google, чтобы узнать, не устарела или заблокирована ли какая-то часть моего кода, но я не нашел ничего полезного. Единственная информация, которую я нашел, касалась ActionBarActivity, но при использовании AppCompatActivity проблема была той же.
Версия MVP MainActivity следующая (я удалил другие ненужные строки для этого примера):
Код: Выделить всё
package com.vinaysomawat.careerhigh;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.GeolocationPermissions;
import android.webkit.WebChromeClient;
public class MainActivity extends ActionBarActivity {
public String mGeolocationOrigin;
public GeolocationPermissions.Callback mGeolocationCallback;
private static final int REQUEST_FINE_LOCATION=0;
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;
}
}
public class GeoWebChromeClient extends WebChromeClient {
@Override
public void onGeolocationPermissionsShowPrompt(String origin,
GeolocationPermissions.Callback callback) {
String perm = Manifest.permission.ACCESS_FINE_LOCATION;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M ||
ContextCompat.checkSelfPermission(MainActivity.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(MainActivity.this, perm)) {
// ask the user for permission
ActivityCompat.requestPermissions(MainActivity.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;
}
}
WebView mywebview;
@Override
protected void onCreate(Bundle savedInstanceState) {
//Hide TopBar
getSupportActionBar().hide();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mywebview = (WebView)findViewById(R.id.webView);
WebSettings webSettings = mywebview.getSettings();
// Empty Cache
mywebview.clearCache(true);
// Hide Zoom buttons
mywebview.getSettings().setDisplayZoomControls(false);
mywebview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mywebview.getSettings().setBuiltInZoomControls(true);
mywebview.setWebViewClient(new GeoWebViewClient());
// Below required for geolocation
mywebview.getSettings().setJavaScriptEnabled(true);
mywebview.getSettings().setGeolocationEnabled(true);
mywebview.setWebChromeClient(new GeoWebChromeClient());
mywebview.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, android.os.Message resultMsg)
{
WebView.HitTestResult result = view.getHitTestResult();
String data = result.getExtra();
Context context = view.getContext();
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(data));
context.startActivity(browserIntent);
return false;
}
});
mywebview.loadUrl("https://mycurrentlocation.net/");
}
@Override
public void onBackPressed(){
if(mywebview.canGoBack()) {
mywebview.goBack();
} else
{
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
int id = item.getItemId();
if(id == R.id.action_settings){
return true;
}
return super.onOptionsItemSelected(item);
}
}
E /Resource: printErrorResource, возможно, это не ошибка, поскольку модуль имеет
полный доступ к ресурсу, вызываемому
=android.content.res.HwResourcesImpl.printErrorResource:2634 android.content.res.ResourcesImpl.openRawResource:444android.content.res.Resources.openRawResource:1418
android.content.res.Resources.openRawResource:1362
android.support.v4.graphics.TypefaceCompatBaseImpl.createFromResourcesFontFile:304
android.support.v4.graphics.TypefaceCompat.createFromResourcesFontFile:232
I/ResourcesImplEx: Путь к активу apk =
ApkAssets{path=/system/framework/framework-res.apk}
The Путь к активу apk = ApkAssets{path=/system/framework/framework-res-hwext.apk}
Путь к активу apk = ApkAssets{path=/preas/oversea/overlay/GmsConfigOverlay.apk}
The Путь к активу apk = ApkAssets{path=/preas/oversea/overlay/GmsGsaConfigOverlay.apk}
Путь к активу apk = ApkAssets{path=/preas/oversea/overlay/GoogleExtServicesConfigOverlay.apk}
Ресурс apk path = ApkAssets{path=/preas/oversea/overlay/GoogleModuleMetadataConfigOverlay.apk}
Путь к активу APK = ApkAssets{path=/preas/oversea/overlay/GooglePermissionControllerConfigOverlay.apk}
Путь к активу APK = ApkAssets{path=/hw_product/overlay/frameworkResOverlay.apk}
Путь к активу apk = ApkAssets{path=/odm/overlay/frameworkResOverlay.apk}
Путь к активу apk = ApkAssets{path=/data /app/com.vinaysomawat.careerhigh-3JvsuNOhaJTzUZrEdMaQnA==/base.apk}
I/ResourcesImplEx: путь к ресурсу apk =
ApkAssets{path=/data/app/com.google.android.webview -Lw6x1ElnmD4d1UEVSG95iA==/base.apk}
Путь к активу apk = ApkAssets{path=/data/app/com.google.android.webview-Lw6x1ElnmD4d1UEVSG95iA==/split_config.it.apk}
The Путь к активу apk = ApkAssets{path=/data/app/com.google.android.webview-Lw6x1ElnmD4d1UEVSG95iA==/split_weblayer.apk}
Путь к активу APK = ApkAssets{path=/data/app/com.google .android.trichromelibrary_489608833-ndmJcgRaxXQPmZ7r_KqT-w==/base.apk
и затем отображается этот журнал (я думаю, это баннер ошибки, в котором говорится который не может загрузить позицию):
D/HiTouch_PressGestureDetector: onAttached,
package=com.vinaysomawat.careerhigh, windowType=2,mHiTouchRestricted=false D/mali_winsys: EGLint
new_window_surface(egl_winsys_display *, void *, EGLSurface,
EGLConfig, egl_winsys_surface **, EGLBoolean) возвращает 0x3000
Подробнее здесь: https://stackoverflow.com/questions/719 ... on-anymore