Android Custom Url Scheme..

Android how to create Custom URL scheme with the given format myapp://http://

As CommonsWare said the given URI upon I needed to create a Scheme is not a valid URI thus the scheme didn't not work and the application didn't launch. After this explanation the server side guys were convinced to change the URI to myapp://... and it worked like magic :).

The Activity looks like this now :

 <activity
android:name="com.myapp.test.SplashScreen"
android:exported="true"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

<!-- Test for URL scheme -->
<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="myapp" />
</intent-filter>
<!-- End Test for URL scheme -->
</activity>

How to test custom URL scheme in android

That is easy via adb. Check this command:

adb shell am start -a android.intent.action.VIEW -d "appname://appnamehost" your.package.name

However you should use your package name as schema. So you can be absolutely sure that there is no conflict (or in detail a intent chooser).

If you have multiple devices you need to execute adb devices. This will result a output like this:

List of devices attached
1645e1d0b86b device
S9WM00904386 device

Then you can use that id from above to address a concrete device with the -s parameter. This will result a command line like this:

adb -s S9WM00904386 shell [...]

In case of a browser link this is also easy. You just need to add this line of html:

<a href="appname://appnamehost">Click me</a>

If you want a fallback in your url you could try the intent url:

<a href="intent://apphostname#Intent;scheme=appname;package=your.package.name;S.browser_fallback_url=http%3A%2F%2Fwww.example.com;end">with alternative</a>

This will open your app even if you schema is not unique, and if you app is not installed it will open example.com.

How to implement my very own URI scheme on Android

This is very possible; you define the URI scheme in your AndroidManifest.xml, using the <data> element. You setup an intent filter with the <data> element filled out, and you'll be able to create your own scheme. (More on intent filters and intent resolution here.)

Here's a short example:

<activity android:name=".MyUriActivity">
<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="myapp" android:host="path" />
</intent-filter>
</activity>

As per how implicit intents work, you need to define at least one action and one category as well; here I picked VIEW as the action (though it could be anything), and made sure to add the DEFAULT category (as this is required for all implicit intents). Also notice how I added the category BROWSABLE - this is not necessary, but it will allow your URIs to be openable from the browser (a nifty feature).

android custom url scheme..?

I think the problem is with the Action you defined.
There is a "android.intent.action.VIEW" which is what I think you want.

<activity android:name=".Gallery1" android:label="@string/app_name" android:launchMode="singleTask" android:screenOrientation="portrait">
<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="cedemo" android:host="com.cedemo.scan" />

</intent-filter>
</activity>

Try that and I bet will resolve correctly. I only made this assumption because you included the browsable category which is usually used by the Browser, which does not know of any of your custom actions. If you do want the GALLERY action as you have implied then just create 2 filters

<activity android:name=".Gallery1" android:label="@string/app_name" android:launchMode="singleTask" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.GALLERY" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="cedemo" android:host="com.cedemo.scan" />

</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:scheme="cedemo" android:host="com.cedemo.scan" />

</intent-filter>
</activity>

So within the contents of your activity you can do something like:

// Value should be "toto" as in your example
String value = getData().getQueryParameter("X");

Android Custom URL Scheme Refuses To Work / How to Navigate Back to Android App After OAuth

After something like 64 hours of scouring the internet and countless attempts I have finally found a solution to this issue. Yay me! I must admit I feel a bit of shame as to how long this issue took to resolve.

The Issue: Could not find a way to navigate back to android app after salesforce authorization (custom url scheme was extremely painful with android).

Solution: After searching around I found a code example from salesforce that did what I was attempting to do here:

https://developer.salesforce.com/page/Developing_Mobile_Applications_with_Force.com_and_Appcelerator_Titanium

Here is the github link provided in the above article that has the much needed code:

https://github.com/appcelerator-developer-relations/Force

TLDR: The real key here was to NOT use a custom url scheme with android and titanium. Instead the github code listed above opens the auth page in a web view and then, using listeners, closes that webview after auth is done and opens whatever view you need.

Explanation: to navigate back to my app on IOS all I had to do was add a couple of lines to my tiapp.xml file in order to use a custom url scheme. It worked very quickly and simply without any headache. Naturally I assumed a custom url scheme was the way to go with android, simple right? false. I spent many hours as listed above trying to make a custom url scheme on android to no avail. There seems to be lots of info online about android custom url schemes but none of them worked for me and believe me I tried so many things (after all it was approx. 64 hours). Eventually I just tried to find an example online of code that did what I wanted. This was the one that I found.



Related Topics



Leave a reply



Submit