I can ask WebView.getWebSettings().getUserAgentString() to get the user
agent, but this doesn't work all that well for my app as I need to
instantiate a WebView first even though I don't need.
Is there another way to get to the User Agent without using a WebView.getSetting, because in my application, I don't need a webView?
Help me please
You either initialize a Webview and then destroy it, or just hardcode the user agent as a string (and replace at run-time language etc).
You can see how it's determined by looking at the source
http://www.google.com/codesearch/p?hl=en#uX1GffpyOZk/core/java/android/webkit/WebSettings.java&q=getCurrentUserAgent&sa=N&cd=1&ct=rc&l=370
According to the documentation, you can't get a WebSettings object without a WebView.
Do you need it to be exactly the one that the phone would send? If not, just pick up a standard Android User Agent (not build/version specific)
The information of user agent is obtained from the HTTP headers, which depends on what browser is used to initiate the WebView object. Therefore, it doesn't make sense if you only want to get the user agent string without creating a WebView.
A better way might be create a WebView and set its visibility to GONE. After getting the user agent string, destroy it.
From android Source code.
public static String getDefaultUserAgent() {
StringBuilder result = new StringBuilder(64);
result.append("Dalvik/");
result.append(System.getProperty("java.vm.version")); // such as 1.1.0
result.append(" (Linux; U; Android ");
String version = Build.VERSION.RELEASE; // "1.0" or "3.4b5"
result.append(version.length() > 0 ? version : "1.0");
// add the model for the release build
if ("REL".equals(Build.VERSION.CODENAME)) {
String model = Build.MODEL;
if (model.length() > 0) {
result.append("; ");
result.append(model);
}
}
String id = Build.ID; // "MASTER" or "M4-rc20"
if (id.length() > 0) {
result.append(" Build/");
result.append(id);
}
result.append(")");
return result.toString();
}
Related
With normal installed apps it's possible to use the technique of Deep Linking in order to not only open a specific application from an URL but also to redirect it to a specific section/function such as a specific Facebook post or specific coordinates on a map.
Since I've read that with Instant Apps this won't be possible because links already point to the specific module to download and run, how would it be possible to access not only the said module but also pass it some parameters?
For example:
This is the link from which the view-only module of my map application will be downloaded: "www.myinstantappexample.com/onlyviewmap"
If I want it to point to a specific set of coordinates how would the link be composed?
Will it be like this: "www.myinstantappexample.com/onlyviewmap/?x=0.000&y=0.000" ?
From what I've been able to find google doesn't cover this aspect and I really can't wrap my head around it.
If I want it to point to a specific set of coordinates how would the link be composed?
It's up to you how to include any additional info in the URL. It could be via URL parameters or in the path itself. Eg.
https://www.myinstantappexample.com/location/2/user/5
https://www.myinstantappexample.com/onlyviewmap/?x=1.2&y=3.4
You then parse the URL in the receiving Activity. The Uri class includes a number of helper methods such as getQueryParameter() and getPathSegments() to make this easier.
For example, to parse this URL:
https://www.myinstantappexample.com/onlyviewmap/?x=1.2&y=3.4
You would do something like this in your Activity:
Uri uri = getIntent().getData();
String x;
String y;
if (uri != null) {
x = uri.getQueryParameter("x"); // x = "1.2"
y = uri.getQueryParameter("y"); // y = "3.4"
}
if (x != null && y != null) {
// do something interesting with x and y
}
Instant Apps and Deep Linking
Instant Apps rely on App Links to work, and App Links are just one type of deep link. So deep linking is still possible for Instant Apps, and is in fact absolutely critical to how they function. However, URI scheme deep linking (which is still very prevalent in Android apps) is not supported.
The difference between a regular app and an Instant App is that the device will only load a single Activity in response to the App Link the user clicks, instead of needing to download the full package through the Play Store. It's a more seamless experience for the user, but the underlying technology works the same way.
Passing Custom Parameters
If the user clicks an App Links-enabled URL like http://www.myinstantappexample.com/onlyviewmap/?x=0.000&y=0.000, you will get that entire string back inside the app after it opens. You'll have to parse out the x and y variables yourself, but they will be available to you. Something like this:
Uri data = this.getIntent().getData();
if (data != null && data.isHierarchical()) {
String uri = this.getIntent().getDataString();
Log.i("MyApp", "Deep link clicked " + uri);
}
You'll just need to manipulate the uri value to find what you need.
Alternative Approach to Custom Parameters
Alternatively, you can use Branch.io (full disclosure: I'm on the Branch team) to power your links. We have full support for Instant Apps, and this allows you to work with a much more friendly data format. We let you create links like this, to control every part of the behavior:
branch.link({
tags: [ 'tag1', 'tag2' ],
channel: 'facebook',
feature: 'dashboard',
stage: 'new user',
data: {
x: '0.000',
y: '0.000',
'$desktop_url': 'http://myappwebsite.com',
'$ios_url': 'http://myappwebsite.com/ios',
'$ipad_url': 'http://myappwebsite.com/ipad',
'$android_url': 'http://myappwebsite.com/android',
'$og_app_id': '12345',
'$og_title': 'My App',
'$og_description': 'My app\'s description.',
'$og_image_url': 'http://myappwebsite.com/image.png'
}
}, function(err, link) {
console.log(err, link);
});
In return you get a URL like http://myappname.app.link/iDdkwZR5hx, and then inside the app after the link is clicked, you'll get something that looks like this:
{
tags: [ 'tag1', 'tag2' ],
channel: 'facebook',
feature: 'dashboard',
stage: 'new user',
data: {
x: '0.000',
y: '0.000'
}
}
In order to do that, you have to use the "app links assistant" in
Tools->App Links Assistant
Then check your links and, in the Path selector, check that the "pathPrefix" option is selected.
Then at the bottom of the OnCreate method of your activity (which is related to the link you recently edited) add this code:
Intent appLinkIntent = getIntent();
String appLinkAction = appLinkIntent.getAction();
Uri appLinkData = appLinkIntent.getData();
// then use appLinkData.getQueryParameter("YourParameter")
You can test and debug this, using the "editConfigurations" option, just open that window and edit your instantApp module (the one launched with the Link you recently edited) and in the URL field add the URL parameters that you need. (then just run that module :D )
Hope this to be helpful.
I'm working on an Ionic 2 app which requires logging in using external link. For security reason I would prefer to open the link in system browser. After I open the link and login with username & password, a token will be returned in the body of the html. Is there a way to pass that token to my app? Or are there any other solutions to tackle this problem? Thank you.
This is extremely conceptual, but you could use the cordova plugin inappbrowser to open a browser inside the app, listen to the exit event to wait for the user to close the in-app browser, and there inject a script to retrieve the token from the html, something like:
inAppBrowserRef = undefined;
showLogin(url) {
var target = "_blank";
var options = "location=yes,hidden=yes";
this.inAppBrowserRef = cordova.InAppBrowser.open(url, target, options);
with (this.inAppBrowserRef) {
addEventListener('exit', this.exitCallBack);
}
}
exitCallBack(params) {
captureToken = "return token = [[jquery to retrieve the token]]";
this.inAppBrowserRef.executeScript({ code: captureToken}, this.executeScriptCallBack);
this.inAppBrowserRef.close();
this.inAppBrowserRef = undefined;
}
executeScriptCallBack(token) {
this.token=token;
}
im working on an android app with some server-side business logic. Im using android studio and im creating that kind of app for a first time.
I am trying to use server-side application to login to a different system and return me a cookie, so my android application can tell, whether the set credentials are correct.
Here's my endpoint provided method.
/** Returns user with cookie set either to null or actual cookie from AIS */
#ApiMethod(name = "login")
public User login(#Named("loginName") String name, #Named("password") String password) {
AISCommunicator aisCommunicator = new AISCommunicator();
String cookieVal = aisCommunicator.login(password,name);
User user = new User();
user.setCookie(cookieVal);
//user.setCookie("asdasdasd");
return user;
}
AISCommunicator is a serverside bean. At the moment it's part of a code
CookieManager manager = new CookieManager();
manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(manager);
is marked as incorrect by Android studio, which tells me, that to use it, i need to declare minSdk level 9, while currently i have 1. How can i do that? I have set minSdk in my client's app, but it seems like it does not influence the serverside bean.
Anyway, the code is still runnable for some reason and the endpoint Bean returns 404 not found error at the moment.
Ignore Android Studio's error. This is one of its known and unfixed bugs.
I have three buttons on my website, that link to Facebook, Twitter & vk.com pages. I want to open native app, if it is installed on user device. Otherwise, I want URL fallback to be opened.
First of all, I tried to use native app schemes directly with deep-link.js plugin. But, when I tried to open native app URL scheme, when native app was not installed, Safari has shown an error, but opened URL fallback page finally. Default Android browser said that he does not know how to handle such URL scheme:
<a class="btn btn-primary" href="https://www.facebook.com/warpcompany" data-app-ios="fb://profile/838619192839881" data-app-android="fb://page/838619192839881">Facebook</a>
Then I tried to use App Links "standard", that that has so much promotion from Facebook. I even tried to use their hosted app links, to make sure I've generated everything right way. It does not work, it always redirect to website fallback. You can easily test it by yourself from https://fb.me/746134728830806
Is it possible to provide deep link on website, that will open native app without errors at least in default os browsers, or fallback silently to URL?
It is still possible, but on newer versions of the Android default browser you have to use intents instead of just trying to open the deep link. For example replace your fb://page/838619192839881 with
intent://page/838619192839881#Intent;scheme=fb;package=com.facebook.katana;end
This will fallback to Google play by default, but you can override the fallback adding a S.browser_fallback_url:
intent://page/838619192839881#Intent;scheme=fb;package=com.facebook.katana;S.browser_fallback_url=http%3A%2F%2Fwww.facebook.com%2F;end
The fallback should be url encoded.
Of course you'll have issues if the user is not on an Android phone or with an old version of the default browser (or strange browser). You can setup a bunch of conditions and replace your HTML with the correct code for each case.
The accepted answer will only work on Chrome v28 and default browsers for Android 5.0. If you want this to work on other browser like Facebook/Twitter webviews, Firefox, UC and older default browsers than 5.0, you'll need to use some code that's a little more complicated.
Add this function to your JS snippet:
var openSesame = function() {
var method = 'iframe';
var fallbackFunction = function() {
if (method == 'iframe') {
window.location = "market://details?id=com.facebook.katana";
}
};
var addIFrame = function() {
var iframe = document.createElement("iframe");
iframe.style.border = "none";
iframe.style.width = "1px";
iframe.style.height = "1px";
iframe.src = "fb://page/838619192839881";
document.body.appendChild(iframe);
};
var loadChromeIntent = function() {
method = 'intent';
document.location = "intent://page/838619192839881#Intent;scheme=fb;package=com.facebook.katana;end";
};
if (navigator.userAgent.match(/Chrome/) && !navigator.userAgent.match("Version/")) {
loadChromeIntent();
}
else if (navigator.userAgent.match(/Firefox/)) {
window.location = "fb://page/838619192839881";
}
else {
addIFrame();
}
setTimeout(fallbackFunction, 750);
};
Then your button will look like this:
Open the App
Or you can use a service like branch.io which does exactly what you're looking for automatically.
Yes it is! Have a look to this training:
https://developer.android.com/training/app-indexing/deep-linking.html
Is this the information you needed? lmk
I'm able to start the application, using the custom URL Scheme
Start My App
The application lunches as usual, but i did need the key user_token=12345. I had less experience with this framework, so don't get any work around. Need help.
Secondly, Can i pass multiple keys using the Custom Schema ?
You need the Android WebIntent Plugin for this.
You can send multiple key
Start My App
Use this as like
document.addEventListener("deviceready", GetCustomUrl, false);
Then the function would be
function GetCustomUrl() {
window.plugins.webintent.getUri(function(url) {
if(url !== "") {
// Here you need to first split with "?" then later with "&"
var link = url.split("?");
var keysPair = link[1].split("&");
// use as per your need
}
});
}
You can check the plugin site for more details.