skip to Main Content

I am developing an android app, in which user must need to login first to use it’s features. So as usual user must need to login using provided user credential.

But I don’t want user always enter username and password again and again, so that I am just sharing a URL with user and if user click on shared link from anywhere in phone (via mail, or WhatsApp, or Facebook, etc.) phone must prompt to choose my app and app should automatically extract the URL and directly redirect the app to my Home Screen by hitting the login api if username and password are correct.

The shared url will be look like this – http://www.myurl.com/sso?username=Scb56745&password=!l0Aeu0yQazxssx

For now I have managed to prompt the dialog using this code in my AndroidManifest file –

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

I am getting this clicked URL link in my UserAcitivity’s on getIntent() method, like this –

Uri uri = getIntent().getData();
String path = uri.getPath();
  • So my real question is how to extract the username and password separately and so that I can call my login api by sending this username and password from this url (for ex. http://www.myurl.com/sso?username=Scb56745&password=!l0Aeu0yQazxssx). – Done

  • And how can I put a dynamic URL or host name, path and scheme in AndroidMenifest file? – Done

  • How can we force our app to always directly open the app,(don’t need to show the chooser dialog) because no use of it even user click on browser nothing gonna happen.

Happy coding 🙂

2

Answers


  1. Try using code like this :

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

    And call it

    val username = getUsernameFromUrl(path)
    

    etc.

    UPDATE:

    The only “dynamic” way of adding URL I know is using something like this :
    https://developer.android.com/studio/build/manifest-build-variables
    you can change this URL depending on your Build Variant.

    As is said here :
    https://stackoverflow.com/a/43339476/8713068
    you cannot update Android Manifest directly from code

    Login or Signup to reply.
  2. Karzel is also right but it’s written in Kotlin, I believe you are expecting the solution for Java I’ll answer the same in 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
    }
    

    To answer your next part about dynamic URL, you can have multiple intent-filters for that purpose

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

    and so on.

    To open your app directly by clicking the link and not show a chooser dialog you’ll have to use a custom URI, for example:

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

    You can change syn3sthete to any custom value you want.
    Don’t forget to add an intent-filter for your custom URI.

    <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>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search