Android Deep linking using explicit intent - android

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.

Related

Is it possible to set unique Intent filter?

What I am trying to achieve is to open Activity automatically when URL is clicked on a browser, notes app or messenger.
So I have this URL format:
http://testsite.com/code/AB1234
In AndroidManifest.xml, I have set Intent filter for Activity:
<activity android:name=".HomeActivity">
<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="testsite.com"
android:pathPrefix="/code"
android:scheme="http" />
</intent-filter>
</activity>
In HomeActivity, just getting code from URL and showing it on Toast:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
String code = getIntent().getData().getLastPathSegment();
Toast.makeText(this, code, Toast.LENGTH_LONG).show();
}
When I click on link, system is showing my application and browser application on application chooser. I know that this URL can be handled browser also. I would like to know how to make my intent filter to have priority if it is possible.
Is it possible to open my app automatically? If yes how to make it? How I should change my intent filter?
If you have control over the link, the easiest way to achieve what you want is to set unique scheme for your app.
<data android:host="testsite.com"
android:pathPrefix="/code"
android:scheme="myapp" />

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

Basic steps to create deep linking in 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.

scheme host not working on android lollipop, click on link to open app

I am using this piece of code to launch my app from a link.
<activity
android:name="com.example.myApp.myClass"
android:label="#string/app_name" >
<intent-filter>
<data
android:host="customHostName"
android:scheme="customScheme" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
This is href link, i want to get the key in the end.
customScheme://customHost/49FYTJTF00
It is working fine on all previous versions of android, but is not working on Lollipop.
When I click the link it only shows the list of browsers to launch.
What should I do?
Edit:
After testing and testing, I found that if your scheme contains an uppercase character the browser won't be able to launch it. Your scheme should only contain lowercase characters!
Also note that bug 459156 of the chromium project still doesn't allow you to launch url's directly, you should reference users to a webpage containing this JavaScript code:
<!DOCTYPE html>
<html>
<body>
<script language="javascript">
window.location = 'customscheme://customHost/49FYTJTF00';
</script>
</body>
</html>
Users landing on this page will automatically be prompted with either an Activity chooser dialog or directly send to your Activity.
To try it, open the Android browser go to the url below and copy paste the above snippet in the editor:
http://www.w3schools.com/html/tryit.asp?filename=tryhtml_intro
Gif showing browser + JavaScript opening the Activity
Original post
I tried out your custom URI and it works on Android 5.0
But you should be aware of the following two bugs/issues:
Bug 459156 of the Chromium project This basicly means launching custom schemes from the Android browser does not work unless the URL is applied using JavaScript. For a workaround see this StackOverflow post: OAuth and custom scheme result in a "ERR_UNKNOWN_URL_SCHEME" in Chrome
Bug 80971: URI with custom scheme are not clickable in SMS Text (Linkify?)
My approach
I created two separate Activities, one as intent receiver and the other as intent launcher. The launching activity has an EditText where the full URI can be entered and a button to launch the entered URI.
Main.java onClick (Launching activity)
#Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(input.getText().toString()));
try {
startActivity(intent);
} catch(ActivityNotFoundException e){
Toast.makeText(this, "Auww!! No Activity can open this URI!", Toast.LENGTH_SHORT).show();
}
}
manifest.xml (only the activities)
Note the <data android:pathPattern=".*"/> part. this part is important so anything after the host will be accepted as valid.
<activity
android:name=".Main"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".UriActivity"
android:label="#string/app_name">
<!--
This intent-filter will open any URI with the following forms:
customscheme://customHost/49FYTJTF00
customscheme://customHost
https://www.google.com/something/something
http://www.google.com/
http://google.com/something
-->
<intent-filter>
<data android:scheme="https"/>
<data android:scheme="http"/>
<data android:host="google.com"/>
<data android:host="www.google.com"/>
<data android:scheme="customscheme"/>
<data android:host="customHost"/>
<data android:pathPattern=".*"/>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
</intent-filter>
</activity>
UriActivity.java (Receiving activity)
public class UriActivity extends ActionBarActivity {
private TextView tvText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_uri);
tvText = (TextView) findViewById(R.id.text);
setTextFromIntent();
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setTextFromIntent();
}
private void setTextFromIntent(){
StringBuilder text = new StringBuilder();
Uri data = getIntent().getData();
if(data != null){
text.append("Path:\n");
text.append(data.getPath());
text.append("\n\nScheme:\n");
text.append(data.getScheme());
text.append("\n\nHost:\n");
text.append(data.getHost());
text.append("\n\nPath segments:\n");
text.append(Arrays.toString(data.getPathSegments().toArray()));
} else {
text.append("Uri is null");
}
tvText.setText(text);
}
}
Screenshot:
Test project download:
I made a download for the project if you wan't to try it out yourself.
Please use pathprefix.
android:pathPrefix="/"
It might solve your problem.
Please follow android developer guide URL.
Instead of using a link to customScheme://customHost/49FYTJTF00, have you tried using a link like
<a href="intent://customHostName/49FYTJTF00#Intent;scheme=customScheme;end">
Chrome should be able to open that in your app just fine. At the same time, this might not work on all browsers.
Keep in mind if you want run the app from Google Chrome you should do that in another way .
You wrote:
<data
android:host="customHostName"
android:scheme="customScheme" />
So, use customScheme://customHostName/49FYTJTF00
instead of customScheme://customHost/49FYTJTF00

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