Is there a way I can detect if the user is accessing an application through a mobile browser or with a PhoneGap app using Java? I have seen some examples of how to do this using Javascript but not using Java. I've read that you can read the URL address and determine if users are accessing the application using a mobile browser or using the PhoneGap app, but I am not quite sure how to implement this and do not understand how the URL addresses will differ. Any suggestions to point me in the right direction would be greatly appreciated. Thank you so much.
Asked
Active
Viewed 92 times
2 Answers
2
As you mention checking the url which application is running on is the best way , it checks if the application is running on http if true the second block of code runs else it is for sure a cordova or phonegap platform .
var app = document.URL.indexOf( 'http://' ) === -1 && document.URL.indexOf( 'https://' ) === -1;
if ( app ) {
// PhoneGap application
} else {
// Web page
}
based on :
http://stackoverflow.com/a/10566220/4010758

Ali Esmaeili
- 542
- 1
- 4
- 14
-
Thanks @ Ali Esmaeli, this solution is using JavaScript, is there a way to implement this using Java instead? Thank you. – user2970660 Jan 28 '16 at 17:08
-
1Sorry I didn't notice you need java code , but thanks to Gilbert the problem solved . – Ali Esmaeili Jan 28 '16 at 20:06
2
Seriously?
Here's one translation of Ali Esmaeili's answer.
public boolean isPhoneGap(URL url) {
String urlString = url.toExternalForm().toLowerCase();
int isHTTP = urlString.indexOf("http://");
int isHTTPS = urlString.indexOf("https://");
return (isHTTP == -1) && (isHTTPS == -1);
}

Community
- 1
- 1

Gilbert Le Blanc
- 50,182
- 6
- 67
- 111