Basic steps to create deep linking in Android - android

I am creating deep linking in my android app.
Can you please tell me how to implement deep linking and Android indexing.
I have already gone through android developer website but could not understand .

Create an activity where you will handle the intent that will come to your app when clicking on the external links. Register your Activity in the AndroidManifest.xml with the scheme/slug for your links so that Android recognize your app can open the links.
<activity
android:name="com.oncall.android.activity.ExternalLinksHandlerActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data
android:host="test.com"
android:pathPrefix="/user"
android:scheme="http"/>
</intent-filter>
</activity>
In your activity onCreate handle the intent that will come. You can call onNewIntent with your Intent and handle it onNewIntent. There you extract the data that you need, in my case I start MainActivity and leave it to operate with the data.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
onNewIntent(getIntent());
}
#Override
protected void onNewIntent(Intent intent) {
String action = intent.getAction();
if (Intent.ACTION_VIEW.equals(action) && data != null) {
startMainActivity(data);
this.finish();
}
}
Here is what you should do with your website. You have to add some tags to it, it's not rocket science.

Related

Links are opening in Android webview creating multiple instances

I am working on an Android webview app and opening www.xyz.com in it.
When the app is running in the background and if I try to open Whatsapp/SMS received message-www.xyz.com/example then it is opening in a new instance and not opening in the already running instance which is in background.
And when I tried using android:launchMode="singleTask" it resumes www.xyz.com only instead of opening www.xyz.com/example.
Below is my AndroidManifest.xml
<activity android:name=".MainActivity" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="www.xyz.com"
android:scheme="https"
/>
</intent-filter>
</activity>
Below is the MainActivity.java code -
protected void onResume() {
super.onResume();
Uri data = getIntent().getData();
if (data != null && data.isHierarchical()) {
String uri = this.getIntent().getDataString();
myWebView.loadUrl(uri);
Log.i("MyApp", "Deep link clicked " + uri);
}
}
Please let me know what I am doing wrong? Thank You.
By using launchMode="singleTask" and according to Android docs (see https://developer.android.com/guide/components/activities/tasks-and-back-stack):
The system creates a new task and instantiates the activity at the
root of the new task. However, if an instance of the activity already
exists in a separate task, the system routes the intent to the
existing instance through a call to its onNewIntent() method, rather
than creating a new instance. Only one instance of the activity can
exist at a time.
Add to your main activity an onNewIntent handler where you should see the new URL being passed in newIntent:
#Override
protected void onNewIntent(Intent newIntent)
{
// reload WebView
}
Additionally, I'm not sure that using onResume to mywebView.loadUrl() is a good or necessary practice, as even simple pause/resume events would lead to the page being reloaded. I'd suggest loading it in onCreate(), and reloading it in onNewIntent().

Android: always got the old intent when opening links from browser through Recent Apps key

I want to achieve "launch App from web" and I have 2 ready-to-use links in my webpage. Opening either of the links did call the activity SchemeRedirectActivity and get the correct intent. The problem is when I store(don't close) the links in phone browser separately and:
open one link from browser > press Recent Apps key(e.g.the right hardware key on device) > open another link,
getIntent() always gives me the old intent except the first time opening each link.
Manifest:
<activity
android:name=".SchemeRedirectActivity"
android:screenOrientation="sensorPortrait">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
SchemeRedirectActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
intent = getIntent(); //always got the old intent here
Uri data = intent.getData();
}
The solutions from internet don't work for my case, even if I add android:launchMode="singleTask" / "singleTop" to manifest file, my onNewIntent() is never launched:
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.d("intent", "onNewIntent "); //never launched
Uri data = intent.getData();
}
What is wrong with the process and how can I fix it please?
*Sorry if my question is not clear, thanks.

email link that opens mobile app is installed

I need a way to include a link in an email which either opens a mobile app or redirects the user to a website depending on whether the mobile app is installed or not. I need a solution for both Android and IOS, it is there a set practice on how to achieve this?
Thanks!
You need a combo of answers here I think.
For iOS, You can replace http:// with itms:// or itms-apps:// to avoid redirects.
How to link to apps on the app store
For Android, I think you'll want to look at the <intent-filter> element of your Mainfest file. Specifically, take a look at the documentation for the <data> sub-element.
Make a link in the Android browser start up my app?
On Android, you need to handle this via Intent Filter:
<activity
android:name="com.permutassep.IntentEvaluator"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="your/url"
android:scheme="http" />
<data
android:host="your/url"
android:scheme="https" />
</intent-filter>
</activity>
And the class you need to handle the intent data should look like this:
public class IntentEvaluator extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = null;
if(getIntent() != null && getIntent().getData() != null){
// Do whatever you want with the Intent data.
}
}
}
Taken from an app I developed: https://raw.githubusercontent.com/lalongooo/permutas-sep/master/app/src/main/java/com/permutassep/IntentEvaluator.java

Android Deep linking using explicit intent

Currently I am having two web pages, home and about. The about is deep link in that ,so when i click on the link , android gives me the option to use a browser to or my own app to open that link. I would like to explicitly open that link in my app.I don't want list of the apps to handle it. Is this possible? Below is my code:
AndroidManifest.xml
<activity
android:name=".AboutPage"
android:label="#string/about_page">
<intent-filter android:label="#string/filter_name">
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="http"
android:host="appindexing.robscv.info"
android:pathPrefix="#string/aboutHTML"/>
</intent-filter>
</activity>
AboutPage.java
public class AboutPage extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.about_page);
// Enable the "Up" button for more navigation options
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
onNewIntent(getIntent());
}
protected void onNewIntent(Intent intent){
String action = intent.getAction();
String data = intent.getDataString();
Log.d(action, data);
}
}
In short. No
But
Its coming.
This is a feature scheduled for release in Android M.
All you have to do is add this to the Manifest
<intent-filter android:label="#string/filter_name" android:autoVerify="true">
//...categories etc
</intent-filter>
More info here
Just FYI what this will actually do is deep link directly to the app and if multiple apps on a users phone autoVerify then it will throw up the prompt.

Android - launching My APP from Hyperlink in the email?

I need to wake up my application while clicking on the hyperlink that i got via email.
Any Idea? Please help on this.
Thanks in Advance.
This can be done by using a custom URI scheme (like those market: urls that are handled by the Market app), or by using a custom action with the intent: scheme.
In both cases you should create an activity that will be started when the user clicks your link.
Let's first go through the first case:
1. A custom scheme
Start by declaring the activity in the manifest:
<activity android:name="LinkHandler">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="SACPK" android:host="www.anyhost.com" />
</intent-filter>
</activity>
In this case, the link should look like SACPK://www.anyhost.com/anything-goes-here.
Your activity will receive the whole link in the intent, so you can process it and decide what to do next, based on query parameters or path:
public class LinkHandler extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Uri uri = getIntent().getData();
// this is the URI containing your link, process it
}
}
2. intent: scheme
This time the link should have the following format:
intent:#Intent;action=com.sacpk.CUSTOM_ACTION;end
And the intent-filter should contain a corresponding action that you will check in your activity:
<intent-filter>
<action android:name="com.sacpk.CUSTOM_ACTION" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
And in your onCreate method:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if ("com.sacpk.CUSTOM_ACTION".equals(getIntent().getAction()) {
// then you really know you got here from the link
}
}
The downside to this method is that you won't get any data with your intent.
This whole answer is based on commonsware's brilliant book, The Busy Coder's Guide to Advanced Android Development.

Categories

Resources