Set default browser from within app - android

I'm developing an app that we will put on Android tablets on be used by employees in the field. We want to lock down internet access as much as possible to minimize data usage (so that the employees can't go streaming Netflix movies or something and driving up our data usage). As part of that effort, I'd like to have http(s)?:// links open up in a custom activity I made. Now, I can easily do that by registering the activity with an intent filter on the http/https schemas, but I'd prefer not to have the user have to choose between browsers when opening a link, and I don't want my activity to become the default activity for every link (there may be situations in which the user should be using the Android browser).
Is there a way to set up my activity as the http handler...but only for links launched from my app?

Is there a way to set up my activity as the http handler...but only for links launched from my app?
If the links are from your app, just use startActivity() with an Intent identifying your activity, rather than some generic Intent like ACTION_VIEW.
If your issue is that you are displaying the Web content in a WebView and links clicked there lead to the browser, use setWebViewClient() along with a WebViewClient implementation that handles shouldOverrideUrlLoading().

Related

Should I open an url in WebView inside app or in browser?

I was wondering if there is a best practice in Android about WebView/Browsers. I want to open an url in my app to show a web page but I do not have anything to retrieve from that page so I do not need to show it in a webview inside my app. Should I show that page in a browser or in a webview ?
If you should open an URL in WebView or Browser that totally depends upon your requirements. Still I am adding some points that you can consider:
Browser:
If you have some data like Privacy Policy of your organization, that just for user information. You do not require any inputs from user.
WebView:
If you want to,
Customize content of URL
Get some input from User
Send some information to Server
Thank You!
Quick Answer:
From your context, it looks going the Browser way is good enough.
Details:
When you don't have user inputs to process and it is not part of the user-flow in your app, go about showing it in the Browser. It's easier that way, because you don't have to manage anything explicitly.
When you have something to process or that this window is some part of your in-app flow, you should go the WebView way. It gives you the power to manage things and you really need to code of situations like user pressing "back button", and the like. I mean, you're the owner of the life-cycle management of the WebView and see how it seamlessly fits in your user flow while (s)he is at it.

Retrieve content from custom tab in app

After searching for hours I have to ask: Is there a way to retreive the content from a chrome custom tab from the hosting activity in android?
My prefered way would be to start Chrome Custom Tabs from my App via StartActivityForResult and react in OnActivityResult. Until now I only managed to receive back the requestCode, but data is always null (and resultCode always canceled).
I've learned it would be possible by setting up a way through deep linking and pushing an intend defined via javascript on a website to a specific app handling exactly that deeplink uri schema (e.g. here: Chrome Devs intents).
But this seem rather complicated for passing a success or failed back to the app after solving a task at the server.
So: Is there a way to put some data to the Chrome Tab passed back as result to the hosting Activity?
BTW: A side question: If I would use deep linking instead, wouldn't this mess up the "proper" lifecycles a bit? App->starts ChromeTab->starts Intent->starts App again?
Any hints are very apreciated!

How can I get URL or search query from browsers intent or activity?

Good day
Here's an issue:
Let's assume, that some user enters url or search query in some browser or even clicks on url in some text editor. After this I suppose some intent with ACTION_WEB_SEARCH flag is created and performed. How can I parse such intent and extract required query or url from it?
I've seen a lot of decisions, but all of them work fine if user shares data. It's not suitable for me.
After this I suppose some intent with ACTION_WEB_SEARCH flag is created and performed.
No.
When the user "clicks on url in some text editor", the typical behavior is for the hosting app to use startActivity() with an ACTION_VIEW Intent, supplying the URL.
When the user "enters url or search query in some browser", the browser will do whatever the browser wants. Few, if any, browsers will use ACTION_WEB_SEARCH for this, because ACTION_WEB_SEARCH is designed to bring up a Web browser to do a search, and a Web browser already is a Web browser.
It is conceivable that there are apps that call startActivity() with an ACTION_WEB_SEARCH Intent. However, since this is the first I have heard of this action in a number of years, I suspect that it is not all that popular.
How can I parse such intent and extract required query or url from it?
You are welcome to implement your own activity that supports ACTION_WEB_SEARCH via an <intent-filter> in the manifest. You would retrieve the search terms via getStringExtra(SearchManager.QUERY), as is noted in the documentation.

How to create a web page in Android?

I want my activity to link to a web page which I have created.
I want to create the web page contents in Android.
How is it possible?
A little unclear what your requirements are... But it sounds like WebView is probably what you are looking for.
It allows you to include web pages inside of your app. i.e. linking to a web from within a view, and stay within an app. This also allows you to potentially "create" content inside of an activity, save it to the file system and load it through your WebView. Otherwise, if you want to "create" content you can use your own homegrown web services to send content to a remote server to then accessed by your webview, or through the regular browser.
If you are just trying to open a link from your app in the standard browser try looking using intents. See here for an example.

Find out which browsers are installed?

I'm looking for a way to find out which browsers are installed on the Android Smartphone and their package names.
Why do I need it?
Well basically, my App reacts on certain URLs, i.e. http://bit.ly, so when the click such an he will get an choice in which App to open it. So far everything is working as intended.
If the user sets up this app as default for this kind of links, it will always open in this one without further asking the user. So far so good too. But by doing this, he will be completely unable to open this links in his browser.
So I need a way to send this intent directly to the browser, but to do so I have to know which app the user has set to be default for http/https scheme for example (as user can change it if there is more than 1 browser installed).
Sending the intend with
intent.setComponent(new ComponentName("com.android.browser", "com.android.browser.BrowserActivity"));
should't be a problem I think. The problem is, I can't send a standard intent für URLs, because my App would catch it again if set as default by the user.
should't be a problem I think
Hardwiring in the package and class names of code that is not yours is always a problem.
So I need a way to send this intent directly to the browser, but to do so I have to know which app the user
has set to be default for http/https scheme for example (as user can change it if there is more than 1
browser installed).
Use PackageManager and queryIntentActivityOptions() to filter your activity out and get a list of other activities that the user can choose from.

Categories

Resources