Launch app with URL
Asked Answered
S

2

6

I've read about intents in android but here goes my question. I'd like to launch an app on my android phone with the click of a link in the web browser. Example: If the link is "mycam://http://camcorder.com", "mycam://" acts as some kind of "tag" to launch my app but I'd like to pass "http://camcorder.com" as a string to that app on start.

Help please!

Thanks!

Schoenberg answered 14/6, 2010 at 16:28 Comment(0)
T
5

There is a method in the Browser app source code, :

public boolean shouldOverrideUrlLoading(WebView view, String url) { ... }

After a url clicked and it's not yet starting to load:

  1. converts the url to intent

    Intent intent;
    
    // perform generic parsing of the URI to turn it into an Intent.
    try {
        intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
    } catch (URISyntaxException ex) {
        Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
        return false;
    }
    
  2. if it don't start with market:// (or some predefined schemes), try startActivityIfNeeded()

    intent.addCategory(Intent.CATEGORY_BROWSABLE);
    intent.setComponent(null);
    try {
        if (startActivityIfNeeded(intent, -1)) {
            return true;
        }
    } catch (ActivityNotFoundException ex) {
        // ignore the error. If no application can handle the URL,
        // eg about:blank, assume the browser can handle it.
    }
    

It's very useful information! I re-play the situation in some simple code:

Intent intent = Intent.parseUri("mycam://http://camcorder.com", Intent.URI_INTENT_SCHEME);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setComponent(null);
System.out.println(intent);

The result will provide clues for me to write an activity with the intent-filter:

        <activity android:name=".MyCamActivity" 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.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="mycam" />
            </intent-filter>
        </activity>

PS. don't forget the android.intent.category.DEFAULT.

Finally, your Activity can invoke by mycam://yourscheme.

Turro answered 15/6, 2010 at 3:5 Comment(3)
So all I have to do is create my intent-filter? BTW, here's the actual link that I'll be doing the tests with: gunitec.com.do/carlos-portes.html "Cam1" is the actual link I'll be testing with. I've tried the intent-filter and got nothing.Schoenberg
I test it well. could you provide the AndroidManifest.xml ?Turro
Ok, how can I send it to you? Sorry for the late response, I wasn't notified that you wrote back.Schoenberg
D
4

mycam://http://camcorder.com isn't a valid URI, and making up schemes is kind of scary if two apps pick the same one. It would be better for you to register your activity as a handler for a particular URI (for example http://www.example.com/camcorder, substituting your own domain of course). You do that with the <data> tag in your <intent-filter> tag in the AndroidManifest.xml. When the user clicks the link, they'll be taken to your application. That way, you can also put a real page there on the web, instructing people to install your app or whatever.

Dinadinah answered 15/6, 2010 at 4:37 Comment(1)
The application I'm working on is made for specific internal links on a website, not for all links. Why do you say it is not a valid URI?Schoenberg

© 2022 - 2024 — McMap. All rights reserved.