3

Good Evening. I'm on making Tracking Application on Android using Webview. i got problem because the system all works well beside the location. i've tried some method from Mr. Grishma Ukani (Big Thanks).

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();
    }
}
}

Everything goes well got no error left. but still can't got the device location / permission then this information appear on android studio

E/cr_LocationProvider: Caught security exception while registering for location updates from the system. The application does not have sufficient geolocation permissions. E/cr_LocationProvider: newErrorAvailable application does not have sufficient geolocation permissions.

then i tried to modified with links from Insufficient geolocation permissions and put this modification on my public class 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;
        }
    }
}

but got some error :

  1. Error:(67, 66) error: cannot find symbol class NonNull
  2. Error:(50, 22) error: package Build does not exist
  3. Error:(66, 9) error: method does not override or implement a method from a supertype
  4. Error:(68, 18) error: cannot find symbol method onRequestPermissionsResult(int,String[],int[]).

any helps and suggestion for another method will be really appreciated.

1 Answers1

2

You need to import the NonNull annotation.

Add import javax.annotation.Nonnull; to your imports, you can find them at the top of the class.
Your class is likely inside a package, you need to declare that package in GeoWebChromeClient class. Note that you have a line package com.example.webview; at the top of your GeoWebViewActivity class indicating that this class is inside ../com/example/webview. You need a similar package line in GeoWebChromeClient if it is no longer an inner class.

sindhu_sp
  • 749
  • 5
  • 12
  • i'm sorry im not accustomed to use java. can you explain a bit more? do you mean that i should put import javax.annotation.Nonnull; after the package? – Christian Slate Feb 08 '18 at 11:58
  • nope. my public class GeoWebChromeClient extends WebChromeClient is inside public class GeoWebViewActivity extends Activity. what should i do next? – Christian Slate Feb 08 '18 at 12:14
  • Okay, add `import android.os.Build;` and `import javax.annotation.Nonnull;` after the package line. This should get rid of errors 1 & 2 – sindhu_sp Feb 08 '18 at 13:24
  • more error comes out. 1. Error:(3, 24) error: package javax.annotation does not exist || 2. Error:(63, 66) error: cannot find symbol class NonNull || 3. Error:(67, 9) error: method does not override or implement a method from a supertype || 4. Error:(69, 18) error: cannot find symbol method onRequestPermissionsResult(int,String[],int[]) – Christian Slate Feb 08 '18 at 13:44
  • *sorry double post. – Christian Slate Feb 08 '18 at 13:44