I have a simple native Android app that is a webview of a website, effectively to make the mobile-ready site native-like if you will. The website already has Google Analytics installed.
What might be a good way to track which visitors are using the app?
I could adding Android Native App Tracking, but I presume that would
double track the users. Unless it's smart enough to connect the visits?
I could pass custom get variable to the site that maybe adds a custom
attribute to the tracking for native app users. But that doesn't
sound very clean.
What might be best for tracking? I feel there's got to be an obvious answer I'm missing.
that should help you:
Now getting back to the Analytics tracking of this web app, I used the code provided by Google here.
So the code becomes somewhat like this.
public class myWebApp extends Activity{
Webview mWebview;
GoogleAnalyticsTracker tracker;
protected void onCreate(Bundle savedInstanceState) {
tracker = GoogleAnalyticsTracker.getInstance();
// Start the tracker in manual dispatch mode. The following UA-xxxxxxx-x code must be replaced by //your web property ID.
tracker.startNewSession("UA-xxxxxxx-x", this);
mWebview = new WebView(this);
mWebview .setWebViewClient(new myWebViewClient());
mWebview .loadUrl("file:///android_asset/www/index.html");
private class myWebViewClient extends WebViewClient
{
//After the user visits a particular page, send the tracking notification to GoogleAnalytics.
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
tracker.trackPageView( mWebview.getUrl());
tracker.dispatch();
}
}
}
http://www.the4thdimension.net/2011/11/using-google-analytics-with-html5-or.html
And in stats of google analytics you should get some info at least about operating system android.
Related
I have been working on a hybrid android application. Currently a WebView in our application is pointing to an AngularJS 1.5.7 application. When the user hits a button inside of the application that changes the route I was expecting the shouldOverrideUrlLoading function to be called inside of my WebViewClient. However, this is not the case. It looks like shouldOverrideUrlLoading does not get hit on Angualar route changes.
This being the case I have gone down the following rabbit holes:
onPageFinished - Overriding this function in the WebViewClient works, however, it is not being called until after the new route is getting hit. Which is adding to the application loading time and creating a choppy experience. ` #Override
public void onPageFinished(WebView view, String url) {
if (url.endsWith("/#/")) {
signOut();
} else if (url.endsWith("/login")) {
// TODO: show some sort of failure message?
Log.i("Login Route", "The webview just attempted to go to the login route.");
signOut();
} else if (url.endsWith("/security")) {
Intent intent = new Intent(getApplicationContext(), SecurityActivity.class);
startActivity(intent);
}
}`
shouldInterceptRequest - Overriding this function allows you to watch for requests. However, by the time the requests go out from the AngularJS application the web view is showing a new route once again providing a choppy user experience.
onLoadResource - same
JavaScriptInterface - Currently I have set up a JavaScript interface to watch for window.location changes. This seems to catch the route changes quicker than any of the above options, however, there is still a glimpse quick flicker of the web page I do not want to do go to. You can find how to do Javascript bridging on this post
Any suggestions would be greatly appreciated! Thanks.
I noticed that with the last update of Google System WebView, all the links in my WebViews are opened in the view itself. But according to the documentation from google:
public boolean shouldOverrideUrlLoading (WebView view, String url)
Added in API level 1
Give the host application a chance to take over the control when a new url is about to be loaded in the current WebView. If WebViewClient is not provided, by default WebView will ask Activity Manager to choose the proper handler for the url. If WebViewClient is provided, return true means the host application handles the url, while return false means the current WebView handles the url. This method is not called for requests using the POST "method".
I did not provide custom WebViewClient.
NOTE: The device that I noticed the problem was HTC One with the latest Google System WebView from June 8, 2015
I can reproduce the findings. Android System WebView 43.0.2357.121 exhibits the behavior that you describe, while the version I had on before upgrading to it did not.
I have filed a bug report on this and now need to do more testing and warn the world.
Thanks for pointing this out!
UPDATE
Here is a blog post that I wrote on this subject. Quoting myself from it:
My recommendation at the moment is:
Always attach a WebViewClient to your WebView
Always implement shouldOverrideUrlLoading() on the WebViewClient
Always return true to indicate that you are handling the event
Always do what your app needs to have done, whether that is loading
the URL into the WebView or launching a browser on the URL (rather
than returning false and relying on stock behavior)
Something like this static inner class appears to do the trick —
create an instance and pass it to setWebViewClient() on your
WebView:
private static class URLHandler extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (shouldKeepInWebView(url)) {
view.loadUrl(url);
}
else {
Intent i=new Intent(Intent.ACTION_VIEW, Uri.parse(url))
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
view.getContext().startActivity(i);
}
return(true);
}
private boolean shouldKeepInWebView(String url) {
return(true); // or false, or use regex, or whatever
}
}
(where you would put your business logic in shouldKeepInWebView() to
determine whether or not a given URL should stay in the WebView
or launch a browser)
Seems to me this issue was resolved in 44.0.240.54.
I have started playing with developing apps for the android. I have a site hosted on an IIS server that uses Windows authentication. I want to connect to it through my app. When I access the site from a computer or phone it prompts for the username and password. However, I cannot get the webview to pop up the credentials prompt, nor have I been able to pass the username and password to the site in my Main_Activity when using the emulator (target 4.4 API 19).
I have been working on this for a while looking a tons of examples. It's difficult to relate the code I have seen to my specific situation, I have copied and pasted number examples. I keep getting 401 unauthorized when the page comes up.
I am trying to stick with the android app thing this time, so any help would be greatly greatly appreciated. Below is my last attempt:
Here are 2 methods I have created or modified. There are seperate from one another.
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("URL:8080/");
}
"More code.."
public void onReceivedHttpAuthRequest (WebView view, HttpAuthHandler handler, String host, String realm){
String[] up = view.getHttpAuthUsernamePassword("username", "password");
if( up != null && up.length == 2 ) {
handler.proceed(up[0], up[1]);
}
}
}
My activity has an intent filter to pick up a specific url and open it in a WebView control, which brings user to an auth page (user name/password). After authentication is done user will get a binary stream response (file). Is there a way to handle that response and read data from the stream?
I tried to setup a custom WebViewClient with the overridden shouldOverrideUrlLoading method, but app doesn't get there.
#Override
mMyWebView.setWebViewClient(new CustomWebClient());
mMyWebView.loadUrl("http://xxx.xx.x.xx:xxxx/getCert");
...
private class CustomWebClient extends WebViewClient
{
#Override
public boolean shouldOverrideUrlLoading (WebView view, String urlConection)
{
// break point here doesn't stop debugger
return true;
}
}
neither works
mMyWebView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading (WebView view, String urlConection)
....
});
Server reacts on requests in the same way from both My app and build-in browser. Build-in browser starts to download file received in response, but my app doesn't do anything and doesn't hit breakpoint inside CustomWebClient.
This is a separate app just to test this piece nothing else interferes with it. INTERNET & WRITE_EXTERNAL_STORAGE permissions added.
EDIT: Mar 8
Gave up. Will go with httpClient.
Did you implement this method of WebView?
void setDownloadListener(DownloadListener listener)
Register the interface to be used when content can not be handled by the rendering engine, and should be downloaded instead.
Has anybody had success integrating the Licensing Verification Library (LVL) with a Live Wallpaper? If it were just running an Activity, it'd be crystal clear to just extend my Activity from the Licensing Activity, which in turn extends Activity. But Live Wallpapers are a Service, and I'm not sure how the two are intended to interact.
I'm using code derived from this: http://www.droidforums.net/forum/android-app-developers/69899-market-license-easy-implementation-protect-your-apps.html which seems to be the code that nearly everything I can find on the web refers to.
I notice that wallpaper settings are an activity, and I have those working properly, but for some reason I can't grok the Licensing stuff...
It's actually really quite simple, you don't need to use any Activity class to implement licensing into a WallpaperService.
Make sure you've followed the directions carefully at http://developer.android.com/guide/publishing/licensing.html
Here's how I did it:
Your extended Engine class should include something similar to the following... (code not essential to your question has been removed)
class startYourEngines extends Engine {
public startYourEngines() {
super();
licenseStatus(); //custom license check method (for modularity)
//the rest of your engine would go here
}
public void onDestroy() {
super.onDestroy();
licenseChecker.onDestroy(); //we call this to close IPC connections
}
//prep work
private static final String BASE64_PUBLIC_KEY = //OMITTED//;
private LicenseCheckerCallback licenseCallback;
private LicenseChecker licenseChecker;
private byte[] salt = "rAnd0mStr!ng".getBytes();
private AESObfuscator aes;
private String deviceId;
//our custom license check method
private void licenseStatus() {
deviceId = Secure.getString(getContentResolver(), Secure.ANDROID_ID);
aes = new AESObfuscator(salt, getPackageName(), deviceId);
licenseCallback = new licenseVerification();
licenseChecker = new LicenseChecker(context, new ServerManagedPolicy(context, aes), BASE64_PUBLIC_KEY);
licenseChecker.checkAccess(licenseCallback);
}
//our callback method
private class licenseVerification implements LicenseCheckerCallback {
#Override
public void allow() {
//allow full app use
}
#Override
public void dontAllow() {
//prevent or limit app use
}
#Override
public void applicationError(ApplicationErrorCode errorCode) {
//error handling here
}
}
}
Licensing on the Android platform was created with versatility in mind. Just be sure to read through the documentation, and you shouldn't have any issues.
I have only written applications that start activities, but looking at my source code, it seems that the only reason that you would have to have an Activity do the license check is to show dialogs.
In all of the examples available on line, the LicenseCheckerCallback implementation always shows a dialog in the allow() and dontAllow() methods. Why not just show a toast in dontAllow() and exit your wallpaper service (call stopSelf(YourService.this))?
Let me know if you want more information, because I dont think you are limited to only using an activity for license checking. As an aside, make sure that you dont keep whole strings, etc in your app or in the preferences. Anyone with root access can access your preferences and if your app is decompiled, your strings are visible...
I think I've actually got it working now. I'm extending LicenseCheckActivity to my own Activity class that I'm calling in the manifest file with the usual MAIN action and LAUNCH category. I instantiate my class, do the license check, and then either allow the wallpaper to function or not based on that result (though the best way to do that is still something I need to sort out).
It almost seems too easy that I think I must be missing something. I'd appreciate anybody with experience with selling a licensed live wallpaper on the Android Market to share whatever wisdom they care to.