代码之家  ›  专栏  ›  技术社区  ›  Bajrang Hudda

如何点击外部链接打开特定应用的特定活动?

  •  1
  • Bajrang Hudda  · 技术社区  · 6 年前

    我正在开发一个android应用程序,用户必须首先登录才能使用它的功能。所以像往常一样,用户必须使用提供的用户凭证登录。

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

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

     <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>
    

    我在useractivity的on getIntent()方法中得到了这个单击的URL链接,如下所示-

    Uri uri = getIntent().getData();
    String path = uri.getPath();
    
    • 所以我真正的问题是如何分别提取用户名和密码,这样我就可以通过从这个url发送用户名和密码来调用我的登录api(例如。 http://www.myurl.com/sso?username=Scb56745&密码=!l0Aeu0yQazxssx公司 ). - 多恩

    • 如何在AndroidMenifest文件中放置动态URL或主机名、路径和方案- 多恩

    • 我们怎么能强迫我们的应用程序总是直接打开应用程序,(不需要显示选择器对话框),因为没有使用它甚至用户点击浏览器什么都不会发生。

    快乐编码:-)

    2 回复  |  直到 6 年前
        1
  •  3
  •   Syn3sthete    6 年前

    卡泽尔也是对的,但这是用英语写的 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
    }
    

    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>
    

    等等。

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

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

    你可以改变 syn3sthete 任意自定义值。 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>
    
        2
  •  1
  •   Karzel    6 年前

    尝试使用以下代码:

    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)
    

    等。

    我所知道的添加URL的唯一“动态”方法是使用以下内容: https://developer.android.com/studio/build/manifest-build-variables 您可以根据您的构建变体更改此URL。

    https://stackoverflow.com/a/43339476/8713068 不能直接从代码更新Android清单