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.

in manifest i got this permission settings already

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>

and in mainactivity.java i got this

public class MainActivity extends AppCompatActivity {

public WebView webView;

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

    webView = (WebView)findViewById(R.id.webView);
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webView.loadUrl("https://www.primaniagatracking.com/slogin.php");
    webView.setWebViewClient(new WebViewClient());
}}

i don't know what missing to make it works may be by always appearing ask permission or something. need big help. thanks. i've searched some template regarding the enable permission but got to much error.

Phantômaxx
  • 37,352
  • 21
  • 80
  • 110

2 Answers2

4

First, the Android manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.webview"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="5" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".GeoWebViewActivity"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.NoTitleBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

main.xml

<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

And finally, here is the complete app, consisting only of GeoWebViewActivity:

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

Hope this helps.

Sufian
  • 6,225
  • 16
  • 65
  • 116
Grishma Ukani
  • 699
  • 4
  • 16
  • i got some error. Error:(14) error: resource drawable/ic_launcher (aka com.example.slate.web:drawable/ic_launcher) not found. Error:(14) resource drawable/ic_launcher (aka com.example.slate.web:drawable/ic_launcher) not found. do you have any idea what happened? – Christian Slate Feb 07 '18 at 10:42
  • yes.. plz update this with: – Grishma Ukani Feb 07 '18 at 10:49
  • got another error. Error:(15, 8) error: class GeoWebViewActivity is public, should be declared in a file named GeoWebViewActivity.java and Error:(49, 25) error: package R does not exist. is there any way to solve this too? – Christian Slate Feb 07 '18 at 10:55
  • replace package in manifest with your package name. package="com.example.webview" change this line. Also in Manifest file and Activity file with your package name. – Grishma Ukani Feb 07 '18 at 10:59
  • done. still eror but it solved by renaming the mainactivity.java to GeoWebViewActivity.java. would it be problem? one error remaining Error:(49, 32) error: cannot find symbol variable main. anyway big big thanks sir for your helps i really appreciate it – Christian Slate Feb 07 '18 at 11:08
  • replace setContentView(R.layout.main); line with your xml file. e.g. setContentView(R.layout.your_xml); Ya.. Thanks. Please accept my answer and upvote it. – Grishma Ukani Feb 07 '18 at 11:10
  • of course i will upvote and accept it!. all the error gone but when i run it on the android device it still cant found the GPS sensor i know it because on the website i validate if the GPS sensor founded or not. do you have any idea? – Christian Slate Feb 07 '18 at 11:23
  • https://medium.com/@xabaras/android-webview-handling-geolocation-permission-request-cc482f3de210 You need like this? Then please do as in this link – Grishma Ukani Feb 07 '18 at 11:33
  • 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. this information appear – Christian Slate Feb 07 '18 at 11:38
  • https://stackoverflow.com/a/39420657/7961132 update method as like this. – Grishma Ukani Feb 07 '18 at 11:43
  • i'm sorry i've tried. can you explain a bit about where i implement that solution? like where to put it and what i need to change? is it inside public class MainActivity extends Activity? or is it totally different from source code you give me before? thanks before – Christian Slate Feb 08 '18 at 10:03
  • i have update my code.. Pls check and update accordingly. – Grishma Ukani Feb 08 '18 at 11:29
  • done it. i got few errors. Error:(67, 66) error: cannot find symbol class NonNull Error:(50, 22) error: package Build does not exist Error:(66, 9) error: method does not override or implement a method from a supertype Error:(68, 18) error: cannot find symbol method onRequestPermissionsResult(int,String[],int[]) – Christian Slate Feb 08 '18 at 12:21
  • I clear the not null and put the function just right after the main task. Solved thanks very much sir. You help me alot – Christian Slate Feb 09 '18 at 05:31
  • hi.. i am not sir.. I am miss Grishma..Your welcome.. :) – Grishma Ukani Feb 09 '18 at 08:42
0
    private boolean isLocationPermissionGrandted() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            return ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                    || ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED
                    || ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_BACKGROUND_LOCATION) == PackageManager.PERMISSION_GRANTED;
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            return ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                    || ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;
        } else {
            return true;
        }
    }


    private void getLocationPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_BACKGROUND_LOCATION}, PERMISSION_LOCATTION_CODE);
        } else {
            ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_LOCATTION_CODE);
        }
    }
if (isLocationPermissionGrandted())
     initializeWebView();
else
     getLocationPermission();