C
C
corerambler2018-06-10 00:12:04
Android
corerambler, 2018-06-10 00:12:04

Android Stuido, WebView - why the application does not ask for location permission during installation, how to fix it?

Hello.
I wrote the simplest application in android studio, which simply opens a page from the assets folder where the javascript geolocation function is executed.
The problem is that when installing the application says: "This application does not require special permissions, install it?"
5b1c419d31b94585169755.png
After installation, when we receive a message that the application was installed successfully and there is a "more" button. When you click on this button, we go to the additional permissions section for this application and we see that location permission is required but by default it is set to "no" !!
5b1c421698d96254752934.png
Accordingly, if you do not enter this section and do not check the box, the location in the application is determined and will not be ...
How to do this,as in other normal applications, a confirmation was displayed for the permission of the location?

MainActivity.java
package com.tutorials.hp.webviewer;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebChromeClient;
import android.webkit.GeolocationPermissions;

public class MainActivity extends AppCompatActivity {

    WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        webView= (WebView) findViewById(R.id.myWebview);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webView.getSettings().setGeolocationEnabled(true);
        webView.getSettings().setGeolocationDatabasePath(getApplicationContext().getFilesDir().getPath());
        webView.getSettings().setDomStorageEnabled(true);
        webView.setWebChromeClient(new GeoWebChromeClient());

        webView.loadUrl("file:///android_asset/Campo.html");
    }

    public class GeoWebChromeClient extends WebChromeClient {
        @Override
        public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
            callback.invoke(origin, true, true);
        }
    }

    /*
    LOAD WEBSITE
     */

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.assetsID) {

            //LOAD FROM ASSETS
            webView.loadUrl("file:///android_asset/Campo.html");

            return true;
        }else if (id == R.id.assetszID) {

            //LOAD FROM ASSETS
            webView.loadUrl("file:///android_asset/Campo2.html");

            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tutorials.hp.webviewer">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2018-06-10
@zagayevskiy

Location permission is runtime permission. On Android 6+, it must be requested at runtime, and normal applications do so. Also, a normal application is ready for the user to deny access to the location at an arbitrary point in time.
Read more rtfm. https://developer.android.com/training/permissions...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question