Search code examples
androidhtml

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.


Solution

  • 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);
            }