Divyang Panchal
Divyang Panchal

Reputation: 1909

can i Broadcast custom intent through my HTML page link from browser?

I want to broadcast my custom intent when i click a link of my html page from browser. I know android system will broadcast "android.intent.action.VIEW" for this and i can receive this in my application but doing this will list my application for every clickable link so i want to broadcast my custom intent action.

Upvotes: 0

Views: 2354

Answers (2)

PRYM
PRYM

Reputation: 533

I want to broadcast my custom intent when i click a link of my html page from browser.

You can do that in two ways, the exact choice depend on your use case.

  1. Create an <intent-filter> to open selected links (links of your website)
  2. Change your link on the website to <a href="intent://...>

The first method gives you flexibility to leave the links on your website as it is and it will also helps in deeplinking and AppIndex. Where as the second method will make you change all the links in the website.

I know android system will broadcast "android.intent.action.VIEW" for this and i can receive this in my application but doing this will list my application for every clickable link so i want to broadcast my custom intent action.

It will not list your application for every clickable link on the web, it will only open your app for your website links. If you don't want to do that and only open app for one specific link of your website, you should use method 2 above.

Just a note for the second mehtod

Make sure to implement a fallback url, as suggested by Google

When an intent could not be resolved, or an external application could not be launched, then the user will be redirected to the fallback URL if it was given.

The link should look like this

<a href="intent://scan/#Intent;scheme=zxing;package=com.google.zxing.client.android;S.browser_fallback_url=http%3A%2F%2Fzxing.org;end"> 

Notice the S.browser_fallback_url for fallback

Upvotes: 2

Divyang Panchal
Divyang Panchal

Reputation: 1909

I have solved this by following

create test.html file with this single line

<a href="intent://example.com/test?id=12345#Intent;scheme=myapp;package=com.mypackage;end">Open Your Application Directly</a>

Here "example.com/test?id=12345" can be any thing you want this will be passed as intent data in our in our onCreate() method so i have given id for example.

"scheme" can be any string we need to write same scheme in our menifest.xml for intent-filter

"package" is your app package name to differentiate it from other app with same scheme

Note : If app is not installed in device then it will open google playstore from given valid package name

in AndroidMenifest.xml file

            <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="myapp" />
            </intent-filter>

you can add intent filter for any activity i have given to launcher activity note the <data android:scheme="myapp" /> scheme name must be same as given in HTML file before.

Done! open html file in any browser of your device & click on link it will directly open your app.

You can get intent data in your activity's onCreate() method like

        Intent intent = getIntent();
        if (intent != null && intent.getData() != null) {
            Uri data = intent.getData();
            String path = data.getPath();
            String id = data.getQueryParameter("id");
            Log.d("ID", ": " + id);
        }

Upvotes: 1

Related Questions