I am new to Phonegap and JqueryMobile. I need to check the internet connection on device. After seeing some solution on net I got confused about approaches. some solutions is suggesting to use PG API like network manager and connection while some using JQMobile method to check connection.
My question is which to prefer ? or what are the target areas for both the implementation ?
PhoneGap have own function to check Internet connection available or not.Here is that official document.
Check Internet is Online document.addEventListener("online", onOnline, false);
Check Internet is Offline document.addEventListener("offline", onOffline, false);
You need to set
<plugin name="NetworkStatus" value="org.apache.cordova.NetworkManager" /> permission in config.xml for android.
You have to add some permission in manifest as below,
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"> </uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"> </uses-permission>
<uses-permission android:name="android.permission.INTERNET" />
Try this
Working example: http://jsfiddle.net/Gajotres/d5XYR/
In this case timer will check internet connection every 100 ms and set final result into a javascript global variable.
Everything depends on this line:
window.navigator.onLine -- it will be false if the user is offline.
Final solution:
var connectionStatus = false;
$(document).on('pagebeforeshow', '#index', function () {
setInterval(function () {
connectionStatus = navigator.onLine ? 'online' : 'offline';
}, 100);
$(document).on('click', '#check-connection', function () {
alert(connectionStatus);
});
});
Related
Hi I'm working on a react native app and I'm using expo-media-library to get user's photos but, only on android, when I try to ask for permissions using the following command I get the status as never_ask_again even if it's the first time I'm asking for permissions and I don't event refuse them because the popup didn't show up.
MediaLibrary.requestPermissionsAsync()
Then I tried to manually give permissions to the app, through settings, but when expo try to access to media library, here's the error that occurs.
Error: Missing MEDIA_LIBRARY permissions.
I also tried to use react-native-permissions to ask for permissions, but the result is the same.
Maybe someone has had this problem before and can help me, thanks.
=> Do you add this in your manifest file or not..?
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
const permission = await Permissions.getAsync(Permissions.CAMERA_ROLL);
if (permission.status !== 'granted') {
const newPermission = await Permissions.askAsync(Permissions.CAMERA_ROLL);
if (newPermission.status === 'granted') {
//its granted.
}
} else {
....your code
}
Ive been playing with .net Maui and had some success doing Bluetooth LE but for reasons I've gone ahead and changed my implementation to use wifi and a restful API. I am building for both android and IoS. I am testing with a real device, running android 12.
I am trying to make a basic http(s) (tried both http and https) request to a local server (an esp32). My url is:
const string url = "https://192.168.4.1/api";
My manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minSdkVersion="28" android:targetSdkVersion="31" />
<application android:allowBackup="true" android:icon="#mipmap/appicon" android:supportsRtl="true"></application>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
My initialisation function, which gets called when my page is loaded - MainPage()
private void init_network()
{
client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
}
and finally, my send function which gets called by a button pressed event
private bool SendCommand(string command)
{
try
{
var msg = new HttpRequestMessage(HttpMethod.Post, url);
msg.Content = JsonContent.Create(command);
HttpResponseMessage response = client.Send(msg); // line where the code throws exception
if (response.IsSuccessStatusCode)
{
return true;
}
}
catch (Exception err)
{
Console.WriteLine(err.Message);
}
return false;
}
I also have a runtime permissions section which asks for location_fine_access, which I've given access for.
When running through the request function, I get an exception thrown with the message:
[DOTNET] Operation is not supported on this platform.
I thought this was a permissions issue, and so android isn't letting me make the request but now I'm not so sure. Anyone else had and resolved this issue?
Looks as though .net maui doesnt hook into the android http client properly and is an issue on github. https://github.com/dotnet/maui/issues/1260
the suggested work around, which I have tested and works for me, is to instantiate your HttpClient with the native android handler like this as a workaround:
new HttpClient(new Xamarin.Android.Net.AndroidMessageHandler()).
I am trying to create a basic calling app on Android with PortSip but get an error from the setUser method (error -60098: CreateTransportFailure). I think I've followed all the steps as their sample app, and I've used the same credentials that work with the sample app. I've tried using a different local port (as suggested in another post), but haven't had any success. These are my steps:
context = this.getApplicationContext();
callingSDK = new PortSipSdk();
callingSDK.setOnPortSIPEvent(this);
callingSDK.CreateCallManager(context);
int initStatus = callingSDK.initialize(PortSipEnumDefine.ENUM_TRANSPORT_UDP,
"0.0.0.0", 5771, PortSipEnumDefine.ENUM_LOG_LEVEL_DEBUG, LogPath, 200,
"PortSIP VoIP Calling App", 0, 0, "", "", false);
// initialize returns ECoreErrorNone
callingSDK.setSrtpPolicy(PortSipEnumDefine.ENUM_SRTPPOLICY_NONE);
int licenseStatus = callingSDK.setLicenseKey("PORTSIP_TEST_LICENSE");
// setLicenseKey return ECoreTrialVersionLicenseKey, as expected
int userStatus = callingSDK.setUser(accountName, displayName, authName, password,
domain, server, 5060, "", 0, null, 5060);
Apparently, another factor in this error condition is assuring the app that the appropriate permissions. Adding these to the manifest gets past the issue with setUser.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
I have a PhoneGap application which immediately sets window.location to be my on-internet site and carries on working from there. The app works great except for the following very strange behaviour.
I'm trying to get a geolocation by calling:
function geoErrorHandler(error) {
console.log("getCurrentPosition failed with error message: " + error.message);
}
function showPosition(position) {
// blah
}
var options = {maximumAge:60000, timeout:5000, enableHighAccuracy:true};
navigator.geolocation.getCurrentPosition(showPosition, geoErrorHander, options);
On iOS it works fine in my PhoneGap app. It also works fine when the page is just loaded in a desktop browser. On my Android PhoneGap app, however, it calls my geoErrorHandler. Which is fine - surely I can find out then what the problem was! Except it calls it with an error which has error.message = "" and error.code = null. Hrhum.
It's doing this on two separate devices and a fair amount of internet searching doesn't seem to reveal anyone else with this behaviour. What am I doing wrong?!
Maybe a permission problem?
Try adding to your manifest ACCESS_COARSE_LOCATION, ACCESS_LOCATION_EXTRA_COMMANDS and ACCESS_FINE_LOCATION permissions
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
http://developer.android.com/reference/android/Manifest.permission.html
I'm developing an app on PhoneGap (Cordova Apache) which make an Jquery Ajax XmlHttpRequest cross-domain request to my server.
Here is the code :
/*
* Create the XMLHttpRequest instance
*/
function getXDomainRequest() {
var xdr = null;
if (window.XDomainRequest) {
xdr = new XDomainRequest();
} else if (window.XMLHttpRequest) {
xdr = new XMLHttpRequest();
} else {
alert("Votre navigateur ne gère pas l'AJAX cross-domain !");
}
return xdr;
}
/*
* Load the data from menu into XMLHttpRequest and call create menu
*/
function sendData() {
var xdr = getXDomainRequest();
xdr.onload = function() {
var dcat = $.parseJSON(xdr.responseText);
createMenu(menu, dcat);
configureMenuScript();
}
xdr.open("GET", "http://preview.********.com/api/ApiProductList");
xdr.send();
}
sendData();
Its working fine on Firefox 28, IE 11 and Chrome 34. It also work fine on the android emulator with an AVD android 4.0.
In the Manifest.xml file I put the following data
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
And in the Config.xml
<param name="android-package" value="org.apache.cordova.core.NetworkManager" />
<access origin="*" />
I'm also allowing the cors in the above JS script
$.support.cors = true;
$.mobile.allowCrossDomainPages = true;
This should allow the Cross-Domain.
-> The issue is that it doesn't work on my device. (Android 4.2.2) I mean, the app load well, but when it come to display the data out of the ajax request, it doesn't work anymore.
Maybe I'm missing an authorization in the manifest or the config file ?
Unfortunately I can't debug live on the device :s
Thanks in advance !
nb: Windows 8.1 - PhoneGap Cordova Apache 3.4
I found out my problem. It was actually not due to an code error but due to the firewall of the company.
So, basically, the code above is working. At least it is a great recap on how to implement a XMLHttpRequest on phonegap :)