How to Open an Android App from the Browser
Alex Austin
January 8th, 2018
Opening an installed app from a browser is often referred to as “deep linking”, and with this guide you’ll learn how to deep link into your Android app for yourself. We’ll focus exclusively on how to trigger an app open from a website page, rather than from the click of a link inside other apps. For a more detailed look at all of the different deep linking standards required for complete Android coverage, please see our Android deep linking series: Part 1 , Part 2 , Part 3 , and Part 4 .
Android is, by far, one of the most fragmented platforms that developers have ever had to manage, due to Google’s decision to force device manufacturers to be responsible for porting the OS, which requires backwards compatibility and support of a multitude of devices. In this ecosystem, we, the app developers, are left to pick up the pieces. Deep linking on Android is unfortunately no different—over the years, we’ve seen a plethora of technical requirements that must be used depending on the circumstance and context of the user.
Note that Branch will implement all of this complexity for you, host the deep links, and even give you robust analytics behind clicks, app opens, and down funnel events. You can play around with Branch links for free by signing up here . We highly recommend using our tools instead of trying to rebuild them from scratch, since we give them all away for free.
Overview of Changes
There are two places where changes will need to be made in order to successfully open your Android app: your website and your Android app. You can find the details of each change in the corresponding sections below.
Adding Support for URI Schemes to Your App
A URI scheme can be any string without special characters, such as http , pinterest , fb or myapp . Once registered, if you append :// to the end (e.g. pinterest://) and click this link, the Pinterest app will open up. If the Pinterest app is not installed, you’ll see a ‘Page Not Found’ error.
It is simple to configure your app for a URI scheme. To start, you need to pick an Activity within your app that you’d like to open when the URI scheme is triggered, and register an intent filter for it. Add the following code within the tag within your manifest that corresponds to the Activity you want to open.
You can change your_uri_scheme to the URI scheme that you’d like. Ideally, you want this to be unique. If it overlaps with another app’s URI scheme, the user will see an Android chooser when clicking on the link. You see this often when you have multiple browsers installed, as they all register for the http URI.
Next, you’ll want to confirm that your app was opened from the URI scheme. To handle the deep link in the app, you simply need to grab the intent data string in the Activity that was opened via the click. Below is an example:
From here, you’ll need to do string parsing to read the values appended the URI scheme that will be very specific to your use case and implementation.
Adding Javascript to Your Website to Open Your App
Now that your Android app is ready to be triggered from a URI scheme, the next part is simple. You merely need to add some Javascript to your website that will auto trigger your app open. The function below, triggerAppOpen , will attempt to open your app’s URI scheme once you replace your_uri_scheme with the one you added in the manifest above.
You could call triggerAppOpen into window.onload if you wanted to do it on page load, or you could make it the onclick of a link somewhere on your site. Either works and the you’ll get the intended results.
Android is incredibly complicated, and there are edge cases everywhere. You’ll think everything is going well until you get that one user complaining that his links aren’t working on Facebook while running Android 4.4.4. That’s why you should use a tool like Branch—to save you this nightmare and ensure that your links work everywhere. Be sure to request a Branch demo if you’re interested in learning more.
Источник
How to open browser in android
Tutorial for SiGMobile to create a functional Android browser with Javascript and HTML enabled.
This tutorial will run you through learning the tools you need to create a browser application with a URL navigator
Open up Android Studio and create a new project. Create a Blank Activity. Set the minimum API to KitKat.
Create a WebView in your activity_main.xml the Text way. A WebView is a generic Android view that shows web pages.
in order for your Activity to access the Internet and load web pages in a WebView, you must add the INTERNET permissions to your Android Manifest file:
Instantiate a WebView object in your Main Activity, and find the WebView you created in your activity with the findViewByID Method. Enable Javascript with the setJavaScriptEnabled(boolean x) method, and load a URL.
Now that you have a working web browser, lets set up a URL navigator so you can go to different websites. Go to the Design tab of your activity_main.xml. Locate «Plain Text» under Text Fields on your palette.
Drag it over to your Device Screen and position it above your WebView. Also, delete the Hello World TextView.
Go back to your text tab of your activity_main and locate the EditText that was generated. You want to add two additional lines to specify that the text field will be taking URL’s. One sets it to a URL field, and the other adds an option to set up a reaction when the send button is pressed:
Same as you did with the WebView, instantiate the EditText object and link it with the View in your main activity XML
When you type in a URL and hit enter, you want Android to take the URL and load the website in your WebView. You can do that with an Activity Listener.
You want to create a Listener that will execute an action (loading the webpage) when a button (the enter key) is pressed.
In your listener, you are Overriding the onEditorAction method to listen for the enter key (ActionID=IME_ACTION_SEND), and loading the URL currently typed into the EditText when the key is pressed.
If you run your app at this time, you’ll notice that when you load a new URL, the default Browser app in Android opens. This happens because when the Intent to load the page is created, Android uses the default loader to load the webpage. For the page to load in your WebView, you want to override the intent to open the Browser. To do this, create a new Java Class that extends the WebViewClient class:
In this class, you want to override one method in the WebViewClient class called shouldOverrideUrlLoading(Webview, String) You want to return false if you are creating an Intent to load a webpage, so that it loads in your Webview and not the default Browser:
The last thing you need to do in your Main Activity class is to set the client to your child class instead of the generic WebViewClient class so that your code uses the overriden method you created in your class:
#Step 10 Run your App!
#Step 11 (Optional add-on)
You might notice if you press the back button, the app will exit. Traditionally, when you press the back button in a browser, it should go to the previous page. There is an easy way to set up this functionality in Android
To do this, Override the system method, onBackPressed(). Make it such that if your WebView can go back, it will go to the previous page. Else the method will call its super, or parent method in the Android system
Once you override the method, see Step 10!
#Congratulations, you created a functional Android browser!
Источник
How to open browser in android
Tutorial for SiGMobile to create a functional Android browser with Javascript and HTML enabled.
This tutorial will run you through learning the tools you need to create a browser application with a URL navigator
Open up Android Studio and create a new project. Create a Blank Activity. Set the minimum API to KitKat.
Create a WebView in your activity_main.xml the Text way. A WebView is a generic Android view that shows web pages.
in order for your Activity to access the Internet and load web pages in a WebView, you must add the INTERNET permissions to your Android Manifest file:
Instantiate a WebView object in your Main Activity, and find the WebView you created in your activity with the findViewByID Method. Enable Javascript with the setJavaScriptEnabled(boolean x) method, and load a URL.
Now that you have a working web browser, lets set up a URL navigator so you can go to different websites. Go to the Design tab of your activity_main.xml. Locate «Plain Text» under Text Fields on your palette.
Drag it over to your Device Screen and position it above your WebView. Also, delete the Hello World TextView.
Go back to your text tab of your activity_main and locate the EditText that was generated. You want to add two additional lines to specify that the text field will be taking URL’s. One sets it to a URL field, and the other adds an option to set up a reaction when the send button is pressed:
Same as you did with the WebView, instantiate the EditText object and link it with the View in your main activity XML
When you type in a URL and hit enter, you want Android to take the URL and load the website in your WebView. You can do that with an Activity Listener.
You want to create a Listener that will execute an action (loading the webpage) when a button (the enter key) is pressed.
In your listener, you are Overriding the onEditorAction method to listen for the enter key (ActionID=IME_ACTION_SEND), and loading the URL currently typed into the EditText when the key is pressed.
If you run your app at this time, you’ll notice that when you load a new URL, the default Browser app in Android opens. This happens because when the Intent to load the page is created, Android uses the default loader to load the webpage. For the page to load in your WebView, you want to override the intent to open the Browser. To do this, create a new Java Class that extends the WebViewClient class:
In this class, you want to override one method in the WebViewClient class called shouldOverrideUrlLoading(Webview, String) You want to return false if you are creating an Intent to load a webpage, so that it loads in your Webview and not the default Browser:
The last thing you need to do in your Main Activity class is to set the client to your child class instead of the generic WebViewClient class so that your code uses the overriden method you created in your class:
#Step 10 Run your App!
#Step 11 (Optional add-on)
You might notice if you press the back button, the app will exit. Traditionally, when you press the back button in a browser, it should go to the previous page. There is an easy way to set up this functionality in Android
To do this, Override the system method, onBackPressed(). Make it such that if your WebView can go back, it will go to the previous page. Else the method will call its super, or parent method in the Android system
Once you override the method, see Step 10!
#Congratulations, you created a functional Android browser!
Источник