Check if web app is added to home screen on Android - android

I have a web app and on the Android I would like to display an alert describing how to add my app to the home screen. (Add it to "Bookmarks" and then "Add it to home screen" or "Add to shortcut in Home"). Then a icon will be displayed on the screen that opens my app.
But off course I only want this to show if the app is not added to the home screen.
Does anybody know how to do this?
Any input appreciated, thanks.

Yes, you can.
While technically a page open in Chrome browser tab can't directly check whether a home screen shortcut exists, the page's local data (localStorage, IndexedDB) is shared between home screen instance and browser tabs, so this can be used to communicate the existence of the home screen version.
Detect if the app is running from home screen
If it's ran from home screen, save this fact to localStorage
Profit! (by reading this from localStorage in any tab)
When the app is in standalone view (which is possible only when launched from home screen), the CSS media query (display-mode: standalone) will match. In Javascript you can read it using:
matchMedia('(display-mode: standalone)').matches
(BTW: the non-standard iOS equivalent of this is navigator.standalone, but iOS doesn't share state between home screen and Safari, so you're out of luck there).
However, instead of custom instructions I suggest meeting Chrome's criteria for "progressive web app" and let Chrome do the prompting for you.

You can't check a web app if its added to home screen on android. At least for now. (Chrome 67)
But you can tell if the app is running in standalone mode using display-mode media query.
Add this inside your <style> tag.
#media all and (display-mode: standalone) {
body {
background-color: yellow;
}
}
Then in your <script> tag.
if (window.matchMedia('(display-mode: standalone)').matches) {
console.log('display-mode is standalone');
}
Or this
if (window.navigator.standalone === true) {
console.log('display-mode is standalone');
}
You can check more from here.

The short answer is: from a Web Site you can't.
The longer answer is: from a Web site you might be able to get a hint in Chrome.
Chrome on Android two new features 1) Web App Manifest that describes what should be launched from the home screen and how it should look on the homescreen, and 2) Chrome now has an beforeinstallprompt event that will trigger for Web apps that we think are app-like and can be installed to the homescreen.
There are a number of criteria for the onbeforeinstallprompt event to fire which might make it an "ok" heuristic (although I suspect not).
The event only fires if:
The site has a manifest, is on https and has a service worker. (this can be quite a stretch).
The user has engaged with the site multiple times (right now, twice within at least 5 minutes).
The user has not already added your site to the home-screen.
So, in summary it is complex and full of false positives and false negatives. However if all you want to do is detect if you should display a banner to prompt the user to add your web-app to the homescreen then Chrome already has a solution for you.
We also have a full range of samples on our samples site.

I think you can do it. Simply add query string to start_url in manifest.json and in your javascript check if start url is having that query string. If query string is found then yeah app is installed.

first you get the list of apps on the device
List<ApplicationInfo> packs = pm.getInstalledApplications(0);
then you use getLaunchIntentForPackage()
Now that you've got the list of packages installed on your device,
iterate through them and call getLaunchIntentForPackage() on each
item.
If a valid intent is returned, it exists on the Launcher, else if null
is returned, the package does not launch from the Launcher screen.
Note that Home screen shortcuts are a subset of the Launcher apps.

Related

PWA launch icon update

Problem: my PWA is already installed on many clients, Android and iOS, and I want to update the launch icon and splash screen.
Android: according to Google documentation:
When the PWA is launched, Chrome determines the last time the local
manifest was checked for changes. If the manifest hasn't been checked
in the last 24 hours, Chrome will schedule a network request for the
manifest, then compare it against the local copy.
If select properties in the manifest have changed (see list below),
Chrome queues the new manifest, and after all windows of the PWA have
been closed, the device is plugged in, and connected to WiFi, Chrome
requests an updated WebAPK from the server. Once updated, all fields
from the new manifest are used.
Which properties will trigger an update? background_color, display, orientation, scope, shortcuts, start_url, theme_color, web_share_target
In my tests, when I update one of these fields in the manifest (I am updating background_color for Chrome to pick up the change and the images specified in icons\src with the new icon), it will indeed be picked up after some time (I can see it by navigating to chrome://webapks) but the launch icon will NOT be updated. I have tried changing the icon name in addition to the icon content but that does not help.
Notes:
if I follow the instructions in Testing manifest updates then the icon will indeed be updated after some time, but obviously, I can't ask this to my users...
My PWA is listed in the installed apps in Android's settings
iOS: here the icon is specified in an HTML tag apple-touch-icon and I could not find anything except this very old answer on SO:
In newer iterations of iOS, the Apple Touch Icon that is displayed on
the home screen is cached just like any other piece of content from the
website. By simply changing the name of the image, it will force the
home screen shortcut to refresh the icon the next time the shortcut is
launched.
<link rel="apple-touch-icon" href=".\icon.ico">
Again this is not what I am seeing, the icon will not change even if I modify the name of the referenced image.
What am I missing???

Appium - open in browser

Testing mobile application using appium. When I run to test, my application opens, I can do some action, and then when I login, app takes me to browser to login to the cloud which is OK, but my problem is in the emulator. I have a question that pops-up, Open with Chrome or other app (JUST ONCE / ALWAYS). How can I avoid this dialog?
added below code but this does not help
caps.setCapability("appPackage", "com.android.browser");
I expect that when app switch me to browser then I can paste my credentials and switch back to my application and I can continue my testing
I experienced similar popups too when testing apps with Appium. The only solution that works for me is extracting the xpath (UIAutomatorViewer) and perform a click on the popup. There must be a OK or cancel button somewhere. (I do this for app crash popups, chrome privacy stuff, ...)
I perform something like:
if(element.isDisplayed()){
element.click();
}
//proceed with your test
It is not optimal but it works. Remember if you would test this app manually you would also have to click the popup away, so I think this is OK.
You can trigger KeyEvent to put apps in the background and see the home screen. Then you search for browser icon and click it. Once in browser, you can use the context commands to automate the webview.
After you done with browser, you can resume the native app.
Sometimes there are some testing that makes us out of the application, Appium provides features for us to switch to the application we want. Please try this method :
driver.startActivity(new Activity("com.example", "ActivityName"));
You must know the name of your application bundle and its main activity.
You can read the documentation here.

Android URL intent deep linking like iOS

The iOS OS provides this back button on the top of an app that was opened via custom URL schemes. This button generally displays the name of the app that launched the custom URL scheme. This could be seen in the screenshot, "Return to Search". Search is the app that opened the app in foreground now.
I was wondering if we could customise the android action bar, to have a similar effect when my app is opened thorough custom URL intents.
NOTE - I know iOS do not have a back button, and hence this was developed. But displaying the app name, makes it explicit that my app was opened via URL intent by this app. It provides higher visibility to the users.
I was wondering if we could customise the android action bar, to have a similar effect when my app is opened thorough custom URL intents.
No, insofar as you have no way of knowing who started your activity.
The is no way of doing it automatically, but:
Android API lvl 22 introduced a new method getReferrer() to return information about who launched this activity.
By default, it will return a android-app: referrer URI.
For instance, Chrome beta will use android-app//:com.chrome.beta as referrer when launching an app.
You could therefore use this value (it might not be 100% trusted) and query the package manager to get the application label.
You can collect some data that manages user experience, but only in your own environment. For example N apps that interact with each other. If you think closely you will see that knowing who wakes your app up doesn't make sense since the caller can use a broadcast, for instance.

How to set my app as default home android?

I'm developing a Android application, and I would like that when it first starts it set itself as default action for Home button.
Currently this option is given to the user by the Android system only when he first press the Home button after running my app.
User don't want to click on check box as "Use by default for this action" it should be set by default my apps.
Thanks in advance.
This is not possible, and it is a good thing too.
Android allows users to choose their own defaults to prevent malicious apps from making themselves the default app to deal with an action, and hence protecting the user's data and device. If an app could set itself as the default homescreen or browser or call app etc. then it could steal the user's personal data and also potentially spoil or deny access to the device itself.
Due to the above reasons, this is not possible why the Android SDK, and I sincerely doubt that an unofficial means to do this exists.

Clear Android Browser History

I am writing an application for a client that will have several devices that are open to customers to look at and play with. They want to be able to clear the browser history on a regular basis so that if a customer opens the browser to an inappropriate web site the next customer to come along will not see this.
I am currently using this to clear the history and searches:
Browser.clearHistory(getContentResolver());
Browser.clearSearches(getContentResolver());
This correctly clears the history. But any windows(tabs) that were open in the browser remain open. How can I tell the browser to close all of these tabs so that the next time someone opens the browser it will load the start page only?
I've noticed that killing the browser with the TaskManager that comes pre-loaded on the device works. Is killing the browser task the only way that I can get it to close any open tabs? If so how can I go about killing/restarting the browser process? I've tried this:
am = (ActivityManager) getApplicationContext().getSystemService(Activity.ACTIVITY_SERVICE);
am.killBackgroundProcesses("com.android.browser");
But this method doesn't seem to be doing anything. I do have
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"></uses-permission>
In my manifest but after I run this and go back to the browser it is still sitting on whatever tabs were left open. What is the proper way to use the permission that is granted by this?
Id rather find some way other than Task Kill to make it work, but at this point I am coming up empty on all fronts.
If your device is rooted, you can use the command below, to clear all data of the application. I don't know if this is applicable for each device, as it depends on the package manager
Runtime.getRuntime().exec("su -c /system/bin/pm clear com.android.browser");
I tried to close the browser via the ActivityManager and it works fine. I tested this on an HTC 2.2 and it works perfect.
Maybe you should also try this:
am.killBackgroundProcesses("com.google.android.browser");
in case the browser on your phone is not at com.android.browser
or even try to finish the activity: com.android.browser.BrowserActivity
Hope this helps!
Much like a computer browser, Android browsers will keep all records that you search onto the device. If you’re worried about browser history or don’t want any records in your Android, you can remove all browser history within minutes. After delete your browser records will delete all cache data where you’re login into Chrome.

Categories

Resources