How to receive a URL from my android app after bei

2019-09-06 10:50发布

问题:

When a user visits the browser page for my app I am forwarding them to a url scheme like so:

intent://share?id=123#Intent;package=com.aerstone.mobile;scheme=myapp;launchFlags=268435456;end;

I've tested this and this works fine. Now, when the user visits my app's home page from the browser and have the app installed, the app automatically opens up.

Relevant part from my manifest is:

    <activity android:configChanges="keyboardHidden|orientation" android:label="@string/app_name"
        android:name=".ui.activity.MainActivity"
        android:launchMode="singleTask" >
        <!-- add the above launchMode attribute -->
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
        <!-- add the below additional 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>

Question

After the app opens up, how can I fetch the URL? I want to fetch the id=123 part of the URL. How can I do this?

回答1:

Call getIntent().getData() in onCreate(), or getData() on the passed-in Intent to onNewIntent(), depending on whether your activity was already created or not. That will return the Uri representing your URL.



回答2:

In onCreate()

Uri uri = getIntent().getData(); //Returns the URI that was used. 
String id = uri.getQueryParameter("id"); //Gives any parameter passed in the URI.