Open my app from a link

情到浓时终转凉″ 提交于 2020-01-11 14:36:05

问题


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


来源:https://stackoverflow.com/questions/32761923/open-my-app-from-a-link

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!