Skip to main content

How to register some URL namespace (myapp://app.start/) for accessing your program by calling a URL in browser in Android OS?



So I want to create an Android app so it would be registered somewhere in android OS (or just would start on system start) and when phone user clicks on special button on a web page inside a web browser a la:







<a href="myapp://mysettings">Foo</a>







my app would pop up and run using the params sent in that URL.





So how do I do such thing?





I need a tutorial with code!



Source: Tips4all

Comments

  1. You need to follow the standard rules for URIs via the W3C and such, which basically means: do not do this.

    Android defines a Uri syntax for describing a generic Intent. There are methods on Intent for converting to and from this representation, such as: http://developer.android.com/reference/android/content/Intent.html#toUri(int)

    So the way to do this is to use the normal facilities to describe an in your manifest for the kinds of intents you are going to handle with a particular component, especially defining an action name in your own namespace (com.mycompany.myapp.action.DO_SOMETHING or whatever). You can then make an Intent that matches your component, and use Intent.toUri() to get the URI representation of this. This can be placed in your link, and will then when pressed look for something that handles and and thus find your app. Note to be launched from the browser like this, the component's must handle the BROWSABLE category. (You don't need to have this in the Intent you put in the link, the browser will automatically add this in for you.)

    Finally, you may want to set the package of the intent to your app with this: http://developer.android.com/reference/android/content/Intent.html#setPackage(java.lang.String)

    This is a newer feature in the platform, which allows you to direct link intents to only your app so that other applications can not intercept and handle them.

    In summary: read the regular documentation on intents and intent filters (such as the NotePad tutorial, though you won't be using content: URIs here, probably just custom actions) and get your app working that way. Then you can make a browser link to launch your app in the same way, provided your intent-filter handles the BROWSABLE category.

    ReplyDelete
  2. First, to be able to start your app from link with custom scheme 'myapp' in browser / mail,
    set intent filter as follows.

    <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>


    and to parse queries in your link myapp://someaction/?var=str&varr=string
    (the code is over simplified and has no error checking.)

    Intent intent = getIntent();
    // check if this intent is started via custom scheme link
    if (Intent.ACTION_VIEW.equals(intent.getAction())) {
    Uri uri = intent.getData();
    // may be some test here with your custom uri
    String var = uri.getQueryParameter("var"); // "str" is set
    String varr = uri.getQueryParameter("varr"); // "string" is set
    }


    [edit]
    if you use custom scheme to launch your app, one of the problem is that:
    The WebView in another apps may not understand your custom scheme.
    This could lead to show 404 page for those browser for the link with custom scheme.

    ReplyDelete
  3. Here is my cut to the chase contribution.

    Create an intent filter in the activity you want to load in the manifest like this:

    <intent-filter>
    <action android:name="com.bubblebeats.MY_CUSTOM_ACTION" />
    <category android:name="android.intent.category.DEFAULT"/>
    <category android:name="android.intent.category.BROWSABLE"/>
    </intent-filter>


    Your web page URL for this will look like this:

    intent:#Intent;action=com.bubblebeats.MY_CUSTOM_ACTION;end


    The most basic HTML code to launch your apk from a browser would look like this:

    <body>
    <a href="intent:#Intent;action=com.bubblebeats.MY_CUSTOM_ACTION;end">click to load apk</a>
    </body>


    To add variables to your intent

    You can generate the URI from within your Android code like this:

    Intent i = new Intent();

    i.setAction("com.bubblebeats.MY_CUSTOM_ACTION");
    i.putExtra("some_variable", "123456");

    Log.d("ezpz", i.toUri(Intent.URI_INTENT_SCHEME));


    This will produce this:

    04-13 09:47:30.742: DEBUG/ezpz(9098): intent:#Intent;action=com.bubblebeats.MY_CUSTOM_ACTION;S.some_variable=123456;end


    You just want this part:

    intent:#Intent;action=com.bubblebeats.MY_CUSTOM_ACTION;S.some_variable=123456;end


    Take a good look at what occurred here and you can see how you can skip the code step and manually create these URIs yourself.

    Especially this part:

    S.some_variable=123456

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex