Run app with url

I read about intentions in android, but here comes my question. I would like to run the application on my Android phone with the click of a link in a web browser. Example: If the link "mycam: // http://camcorder.com ", "mycam: //" acts like some kind of "tag" to launch my application, but I would like to pass " http://camcorder.com "as a string for this application at startup.

Help me please!

Thank!

+5
source share
2 answers

There is a method in the source code of the browser application:

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

After the URL is clicked and it has not yet started loading:

  • converts URL to target

    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;
    }
    
  • ://( ), 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.
    }
    

! :

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

-:

        <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. android.intent.category.DEFAUL.

, mycam://

+5

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.

+4

All Articles