Android - create a schema for my app that will ope

2019-08-13 03:28发布

问题:

I'm trying to create a schema like myapp://somthing/anotherthing that if a webpage or any other app link to that schema will open my app.

I've added this to my AndroidManifest.xml for my main activity:

    <activity
        android:name="com.example.MainActivity"
        android:label="@string/title_activity_main"
        android:configChanges="orientation"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.Light.NoTitleBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:scheme="myapp" />
        </intent-filter>
    </activity>

but when I click a link in a webpage that's linking to myapp://somthing/anotherthing it just gives me a page not found type of error and does not open my app.

Everywhere I look, I see that I need to add the <data> tag inside <intent-filter>. What is missing here?

回答1:

Searching for examples in other related questions i couldn't find one using both <action android:name="android.intent.action.MAIN" /> and <action android:name="android.intent.action.VIEW" /> (the latter seems to be mandatory for this kind of behaviour).

Anyway, it is possible to use both. This is my working example:

<activity
        android:name="com.example.MainActivity"
        android:label="@string/title_activity_main"
        android:configChanges="orientation"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.Light.NoTitleBar" android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.LAUNCHER" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data android:scheme="myapp" />
            </intent-filter>
        </activity>

So, the following tags seem to do the trick:

android:exported="true"
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

This is the anchor i'm using from the web page:

<a href="myapp://somthing/anotherthing">Open the app!</a>

UPDATE: As developer82 mentioned in the comments, if you do this the app icon will dissapear and the user won't be able to open the app. To fix this, you'll need to create a new tag to put the <data> along with default, browsable categories and view action.

The <activity> tag should look like this:

    <activity
            android:name="com.example.MainActivity"
            android:label="@string/title_activity_main"
            android:configChanges="orientation"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Light.NoTitleBar" android:exported="true">
            <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>
            </activity>

Let me know if it helped!