如何打开特定应用程序的特定 activity 单击外部 link?

How to open specific app's specific activity clicking on an external link?

我正在开发一个 android 应用程序,用户必须先登录才能使用它的功能。因此,像往常一样,用户必须需要使用提供的用户凭据登录。

但我不希望用户总是一次又一次地输入用户名和密码,所以我只是与用户共享 URL,如果用户从 link 中的任何地方单击共享 link =51=](通过邮件、WhatsApp、Facebook 等)phone 必须提示选择我的应用程序,应用程序应自动提取 URL 并通过点击直接将应用程序重定向到我的主屏幕如果用户名和密码正确,登录 api。

共享的 url 将如下所示 - http://www.myurl.com/sso?username=Scb56745&password=!l0Aeu0yQazxssx

现在我已经设法在我的 AndroidManifest 文件中使用这段代码来提示对话框 -

 <activity
         android:name="com.my.par.activities.UserActivity"
         android:label="@string/app_name"
         android:screenOrientation="portrait"
         android:windowSoftInputMode="stateHidden">
         <intent-filter android:autoVerify="true" tools:targetApi="m">
                    <action android:name="android.intent.action.VIEW" />
                    <category android:name="android.intent.category.DEFAULT" />
                    <category android:name="android.intent.category.BROWSABLE" />
                    <data
                        android:host="www.myurl.com"
                        android:path="/sso"
                        android:scheme="http" />
                    <data
                        android:host="www.yoururl.com"
                        android:path="/sso1"
                        android:scheme="https" />
         </intent-filter>
 </activity>

我在我的 UserAcitivity 的 getIntent() 方法中点击了 URL link,就像这样 -

Uri uri = getIntent().getData();
String path = uri.getPath();

编码愉快:-)

尝试使用这样的代码:

fun getUsernameFromUrl(url: String): String? {
    return Uri.parse(url).getQueryParameter("username")
}
fun getPasswordFromUrl(url: String): String? {
    return Uri.parse(url).getQueryParameter("password")
}

并称之为

val username = getUsernameFromUrl(path)

等等

更新:

我知道的唯一 "dynamic" 添加 URL 的方法是使用这样的东西: https://developer.android.com/studio/build/manifest-build-variables 您可以根据您的构建变体更改此 URL。

正如这里所说: 您不能直接从代码

更新 Android 清单

Karzel 也对,但它写在 Kotlin,我相信你期待 Java 的解决方案,我会在 Java

中回答同样的问题
Uri uri = getIntent().getData();
String strUsername = "", strPassword = "";
if (uri != null) {
   strUsername = uri.getQueryParameter("username");
   strPassword = uri.getQueryParameter("password");
}
else {
   // Your app will pop up even if http://www.myurl.com/sso is clicked, so better to handle null uri
}

要回答有关动态 URL 的下一部分,您可以为此目的设置多个 intent-filters

<activity
         android:name="com.my.par.activities.UserActivity"
         android:label="@string/app_name"
         android:screenOrientation="portrait"
         android:windowSoftInputMode="stateHidden">
         <intent-filter android:autoVerify="true" tools:targetApi="m">
                    <action android:name="android.intent.action.VIEW" />
                    <category android:name="android.intent.category.DEFAULT" />
                    <category android:name="android.intent.category.BROWSABLE" />
                    <data
                        android:host="www.myurl.com"
                        android:path="/sso"
                        android:scheme="http" />
         </intent-filter>
         <intent-filter android:autoVerify="true" tools:targetApi="m">
                    <action android:name="android.intent.action.VIEW" />
                    <category android:name="android.intent.category.DEFAULT" />
                    <category android:name="android.intent.category.BROWSABLE" />
                    <data
                        android:host="www.myurl.com"
                        android:path="/sso"
                        android:scheme="https" />
         </intent-filter>
         <intent-filter android:autoVerify="true" tools:targetApi="m">
                    <action android:name="android.intent.action.VIEW" />
                    <category android:name="android.intent.category.DEFAULT" />
                    <category android:name="android.intent.category.BROWSABLE" />
                    <data
                        android:host="www.myurl.com"
                        android:path="/debug"
                        android:scheme="http" />
         </intent-filter>
 </activity>

等等。

要通过单击 link 直接打开您的应用程序而不显示选择器对话框,您必须使用自定义 URI,例如:

syn3sthete://myurl.com/sso?username=Scb56745&password=!l0Aeu0yQazxssx

您可以将 syn3sthete 更改为您想要的任何自定义值。 不要忘记为您的自定义 URI 添加 intent-filter

<intent-filter android:autoVerify="true" tools:targetApi="m">
                    <action android:name="android.intent.action.VIEW" />
                    <category android:name="android.intent.category.DEFAULT" />
                    <category android:name="android.intent.category.BROWSABLE" />
                    <data
                        android:host="myurl.com"
                        android:path="/sso"
                        android:scheme="syn3sthete" />
</intent-filter>