Open my app from a link

2019-03-06 17:38发布

问题:

In my app the server send this link: appname://veryfy?email=test@mail.com&token=asdf-asdf-asdf-xcfghfgh but is it possible to get the values like email= test@mail.com and the token = sdfdkgdkgjgfd.

So far I've only added this intent filter inside my manifest but the app is not called:

<intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
                <action android:name="android.intent.action.SEND"/>
                <category android:name="android.intent.category.DEFAULT"/>

                <data android:mimeType="text/plain" android:scheme="appname://"/>
            </intent-filter>

Note this should open my app when the link is clicked in the browser

回答1:

You can get the email and token by getting the Activity's intent like this:

    <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                    <action android:name="android.intent.action.SEND"/>
                    <category android:name="android.intent.category.DEFAULT"/>

                    <data android:mimeType="text/plain" android:scheme="https" 
                     android:host="api.myapp.com"
                     android:pathPrefix="/api/v2/verify"/>
       </intent-filter>

Intent intent = getIntent();
Uri data = intent.getData();
String email = data.getQueryParameter("email");
String token = data.getQueryParameter("token");


回答2:

The scheme in your filter should just be appname, not appname://

android:scheme="appname"


回答3:

try this

    <activity
          android:name=".MainActivity"
          android:label="@string/title_activity_main" 
          android:exported="true">
        <intent-filter>
                        <action android:name="android.intent.action.MAIN" />
                        <category android:name="android.intent.category.LAUNCHER" />
                        <action android:name="android.intent.action.SEND"/>
                        <category android:name="android.intent.category.DEFAULT"/>

                        <data android:mimeType="text/plain" android:scheme="appname"/>
           </intent-filter>
</activity>

To get your url parameter write this in your activity

Uri data = getIntent().getData();
String scheme = data.getScheme(); 
String host = data.getHost(); 
List<String> params = data.getPathSegments();
String first = params.get(0); 
String second = params.get(1); 


回答4:

Try this:

<intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:mimeType="text/plain" android:scheme="appname"/>
</intent-filter>

and the link on the web:

<a href="appname://veryfy?email=test@mail.com&token=asdf-asdf-asdf-xcfghfgh">

Getting data in your Activity:

// check if this intent is started via custom scheme link
Intent intent = getIntent();
String action = intent.getAction();
if (action != null && action.equals(Intent.ACTION_VIEW) {
   Uri uri = intent.getData();
   String scheme = uri.getScheme();
   if (scheme.equals("appname") {
      String email = uri.getQueryParameter("email");
      String token = uri.getQueryParameter("token");
   }
}