Issue
I am writing an application that get the user's location and send it to the server. I am using JavaScript and PHP for the server side. I am trying to load this app on android WebView and it not sending the location to JavaScript so that the location can be sent to the server via ajax but when i run it on my mobile device using Chrome, it work correctly. Please i need to know what i am not doing correctly. My android code are pasted below
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebview = (WebView) findViewById(R.id.webview);
WebSettings websettings = myWebview.getSettings();
myWebview.setWebViewClient(new WebViewClient());
websettings.setJavaScriptEnabled(true);
websettings.setJavaScriptCanOpenWindowsAutomatically(true);
websettings.setGeolocationDatabasePath( getFilesDir().getPath() );
myWebview.setWebChromeClient(new WebChromeClient() {
public void onGeolocationPermissionsShowPrompt(String origin,
GeolocationPermissions.Callback callback) {
callback.invoke(origin, true, false);
}
});
websettings.setAppCacheEnabled(true);
websettings.setDatabaseEnabled(true);
websettings.setDomStorageEnabled(true);
myWebview.loadUrl("https://example.com/proj/");
}
I have already grand the necessary permission in the android manifest file.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
The Code below is my JavaScript that process the location
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(reportLocation, showError);
} else {
$$('locationFeedbackText').innerHTML = "Geolocation is not supported by this Devide.";
}
}
function showError(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
$$('locationFeedbackText').innerHTML = "User denied the request for Geolocation.";
break;
case error.POSITION_UNAVAILABLE:
$$('locationFeedbackText').innerHTML = "Location information is unavailable.";
break;
case error.TIMEOUT:
$$('locationFeedbackText').innerHTML = "The request to get user location timed out.";
break;
case error.UNKNOWN_ERROR:
$$('locationFeedbackText').innerHTML = "An unknown error occurred.";
break;
}
}
When i click a button on the user interface, the user location is suppose to be send to the server and the success message displayed but the output message that is displaying is the message from the showError function "Location is Unavailable. Keep in mind that this app works as expected when it is loaded with chrome on my mobile device.
Solution
Try this -- permission has been added
package com.nony.myapplication;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.GeolocationPermissions;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
WebView myWebview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//added this line for permission request
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
myWebview = (WebView) findViewById(R.id.webview);
WebSettings websettings = myWebview.getSettings();
myWebview.setWebViewClient(new WebViewClient());
websettings.setJavaScriptEnabled(true);
websettings.setJavaScriptCanOpenWindowsAutomatically(true);
websettings.setGeolocationDatabasePath(getFilesDir().getPath());
myWebview.setWebChromeClient(new WebChromeClient() {
public void onGeolocationPermissionsShowPrompt(String origin,
GeolocationPermissions.Callback callback) {
callback.invoke(origin, true, false);
}
});
websettings.setAppCacheEnabled(true);
websettings.setDatabaseEnabled(true);
websettings.setDomStorageEnabled(true);
myWebview.loadUrl("https://example.com/proj/");
}
//newly added for permission
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
public boolean checkLocationPermission()
{
String permission = "android.permission.ACCESS_FINE_LOCATION";
int res = this.checkCallingOrSelfPermission(permission);
return (res == PackageManager.PERMISSION_GRANTED);
}
}
Answered By - nonybrighto
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.