Implement deep linking in android from single activity - android

Although I have gone through related document on google for app indexing
https://developers.google.com/app-indexing/webmasters/details
But still have confusion that If I want to receive incoming data on launcher activity and from there If can take control and start relevant activity/fragment depending on some internal parsing over incoming url.

You have to declare the action, category and data in a separate <intent-filter> tag, not in the same one with launcher specs.
<activity>
<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="your.domain.com"
android:scheme="http" />
</intent-filter>
</activity>

Not quite sure if I understood what your asking, so please correct me if I'm not answering your question.
Only the activity (or activities) that you put the intent filter in will catch an intent and start. Therefore, if you only put the intent filter in one activity, only that activity will start, and not any of the others. You can put multiple intent filters in the same activity to catch multiple intents. You can also use the path segment of the url to send more information to your activity, and parse it in your activity.
Put the following in your manifest under the activity you want to launch (the path is optional):
<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="http" android:host="www.yourwebsite.com" />
</intent-filter>
Use a url like http://www.yourwebsite.com/yourstring to open the app.
Then use getIntent().getData() to get the uri that started the activity. You can then parse the uri to get yourstring.

Add Following Intent filter to Activity you want to open via Deep linking in Manifest.
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Then As per Intent data you get in Activity you can perform your action, too.

Related

Intent-Filter: Using two actions along with Launcher and Browsable category

TL;DR Can i use two actions and two categories in one intent-filter?
My app is made up of one activity and five fragments. I have an Intent Filter in this activity.
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Today, I saw a lint message "App is not indexable by Google..." around the application tag in manifest file. So, I did some searching and came to know that you can use this to index your app through google search. If an android user browses web link "www.example.com/myapp" from chrome/systemBrowser, he will be taken to my app instead of web page. right?
Now, I have to add an ActionView to the activity. and, my intent-filter will become,
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" android:host="www.example.com" android:pathPrefix="/myapp" />
</intent-filter>
But, I have read that using two actions is not good. It is a Logical OR operation. Intent-Filter will match only one of them:
an intent with MAIN action and LAUNCHER Category
an intent with MAIN action and BROWSABLE Category
an intent with VIEW action and LAUNCHER Category
an intent with VIEW action and BROWSABLE Category
As i understand it, It should open app on device with first option, and when a user browses the browser with provided "www.example.com/myapp", it should use first and fourth option to open the app from link.
I have looked at this question, but i need to be sure with an example.
It is confusing. I might be entirely wrong, please guide me.
By the help of someone from SO Chat, I was told to use separate intent-filters
<activity
android:name=".activities.MainActivity"
android:label="#string/app_name">
<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.BROWSABLE" />
<data android:scheme="http" android:host="www.example.com" android:pathPrefix="/prefix" />
</intent-filter>
</activity>

Android deeplink two separate activities

I have two activities to deep link
One activity to link with follow url
http://abc.or/deals
Following is intent filter for it
<intent-filter>
<data
android:host="abc.or"
android:path="/deals"
android:scheme="http" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Another activity with following url
http://abc.or/deals?category=Air+Conditioner-Refrigerator-
<intent-filter>
<data
android:host="abc.or"
android:path="/deals"
android:pathPattern="*deals/?category*"
android:scheme="http" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
But on clicking any of the url deep link is working for both activies which is the issue how I can fix this
Unfortunately, that won't be possible.
as the path is same in both the intent filters.
As an alternative, to have only one suggestion for the user,
Remove the intent filter from second activity
Parse the URI data fromintent.getDataString() in first activity and redirect the user to second activity on basis of this data.

NFC tag opening application to specific screen

From the research on NFC and opening applications I understand that they are capable of opening an app. My question is can it open the app to a specific product page?
Like if I am at a store and I have an nfc tag placed in front of two products can I program each tag to open the app and go to the specific page for each product that would contain information about that particular product?
Thanks!
Let's assume you have a tag containing the URI http://www.example.com/myproduct1. Then you could register your product-specific activity "MyProduct1Activity" with the following intent filter:
<activity android:name=".MyProduct1Activity">
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http"
android:host="www.example.com"
android:pathPrefix="/myproduct1" />
</intent-filter>
</activity>
This will cause the activity MyProduct1Activity to be started for tags that contain the URI http://www.example.com/myproduct1.
Alternatively, if multiple products share one activity, you could register a more generic intent filter:
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http"
android:host="www.example.com"
android:pathPrefix="/" />
</intent-filter>
And retieve the intent within your activity using the activity's getIntent() method to parse the URI for the specific product.

Application appears in the “share” list ANDROID

How can I do that my application appears in the “share” list of another application? For example, I want to choose a pdf file and send it to my application.
I can’t find the solution, any idea?
Thanks
You can add an intent filter in your AndroidManifest.xml
for example for a pdf you can indicate the mimeType application/pdf.
<activity name="package.name.MyActivity">
<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:mimeType="application/pdf" />
</intent-filter>
</activity>
Look at the documentation at http://developer.android.com/guide/components/intents-filters.html#Receiving

Matching a URL to an Intent not working

I'm writing an Android app that accompanies a website and I wanted to make the app intercept certain URLs. I found multiple examples through Google and on SO but none of them work. I create my Intent:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://xgn.nl/article/" + String.valueOf(id)));
and then launch it from another Activity (where i is the intent from above)
startActivity(i);
The intent filter for the activity that should receive this is:
<activity android:name=".ArticleActivity" android:theme="#style/XGN.Red">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="http" android:host="xgn.nl" android:pathPrefix="/article/" android:mimeType="text/*" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
I also tried it without the mimeType in the filter. The intent resolver sees the filter but complains that the data does not match with the data in the intent and launches the browser instead.
What am I doing wrong?
I actually solved it 5s after I posted it, the problem was that you need to specify the fully qualified class name for this to work :)
<activity android:name="nl.xgn.ArticleActivity" android:theme="#style/XGN.Red">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="http" android:host="xgn.nl" android:pathPrefix="/article/" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
EDIT: To be more precise, the error I was seeing in logcat was wrong. The data type did match, but it chose the more visible browser class over my lazily defined activity. Specifying the full class name and removing the mimeType made this work.

Categories

Resources