Start App from URI in email

stevel05

Expert
Licensed User
I thought I'd like to share the results of investigations I have carried out today in the hope that some may find it useful. I have an app that stores data as predominantly text and is potentially sharable between a few close groups of people. Rather than create a server and associated comms, I saw a cool way of doing this on an other OS app that used schemes.

So you create a link in an email which contains the data you want to transfer, when it's clicked on a device that recognizes the scheme an intent is generated and an app can be opened in the normal way.

The app I saw used a high level scheme and defined its own which would have been something like 'myapp://" and the data. Apparently this is frowned on by the community as it like creating your own domain name which may cause issues. Added to that, GMail and probably other mail clients actively filter out unrecognized schemes. The next option is to use an existing scheme 'http' and specify a host name which when matched will generate an intent.

To do this, the manifest has to contain similar to the following (as entered with the editor):

B4X:
AddActivityText("main",
<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT"></category>
    <category android:name="android.intent.category.BROWSABLE"></category>
   <data android:scheme="http" />
   <data android:host="mydomain.myd" />
</intent-filter> )

The host name can be pretty much anything, but I tried to choose one that wouldn't cause problems i.e. avoided .com etc.

The attached example app simply generates an email that contains a link, when you click on the link the app runs and gets the Intent data into the URI string from where you can parse it as required.

If you want to try the attached app, you need to edit the gmail account and password in SMTP.initialize, add a target valid email address that you can access from the device the app is running on, and amend the mydomain.myd in the Body String to reflect what you have put in the manifest.

I have tested this on Android 4.0 on HTC and Bluestacks.

It is also possible to use links on web pages to transfer data, this will require internet permission and does not work on all browsers, in fact, the only one I have managed to get it to work on is the default browser.

This requires the use of Libraries:
Net
Reflection

It also borrows Erel's SMTP demo verbatim.
 

Attachments

  • IntentTest.zip
    6.8 KB · Views: 649
Last edited:

NJDude

Expert
Licensed User
I could be wrong, but I don't think Reflection is necessary, I did something similar HERE, unless I'm missing something.
 
Upvote 0

stevel05

Expert
Licensed User
Reflection is used to get the URI that started the app, and in this case the data with it that is read into the app. But predominantly the same technique, yes.

I searched for scheme in the forum before I started and before I posted, but missed this one. Still, more info is better than none.
 
Last edited:
Upvote 0

NJDude

Expert
Licensed User
Which is why NJDude needs to index his samples on the wiki, otherwise they get lost in the meaningless title threads.

I have no Wiki :( but you can snatch that link and add it to your collection.

stevel05 said:
...Still, more info is better than none.

Yup, the more the better.
 
Upvote 0

bluedude

Well-Known Member
Licensed User
I think there is an issue with this method if you fully want to automate it. Android asks you to open with which app. in this case???

Not sure, any suggestions. I rather would use my own scheme like myapp:// Outside Gmail (inside a webview) I don't think that gets blocked.

I need to use this because I want to send confirmations back from payment pages inside a Webview. The return URL would be my scheme and when discovered it would deeplink into my app. again.

Cheers.
 
Upvote 0

NJDude

Expert
Licensed User
If you are going to display that link inside a webview then you could do something like the attached sample. Note that that will work only if you use a WebView, if the user reads the email on the browser it won't work and you will have to do something like is indicated on post #3 above.
 

Attachments

  • LinkSample.zip
    11.8 KB · Views: 404
Upvote 0

bluedude

Well-Known Member
Licensed User
Thanks NJDude, not exactly what I need. I need to bridge a payment done in a webview and the payment gateway pushes results back to a URI.

Is there a way to fully automate deeplinking without getting a choice with which app. you want to open it?
 
Upvote 0

NJDude

Expert
Licensed User
The example does that, instead of opening the "secret" activity, do what you want. The sample illustrates the procedure.
 
Upvote 0
Top