Android

Android 自定義scheme 跳轉到指定的Activity Deep Link

1.在Manifests聲明指定的scheme

//註冊(test://register)
<activity android:name=".RegisterActivity">
    <intent-filter>
         <action android:name="android.intent.action.VIEW" />

         <category android:name="android.intent.category.DEFAULT" />
         <category android:name="android.intent.category.BROWSABLE" />

         <data
              android:host="register"
              android:scheme="test" />
    </intent-filter>
</activity>
//登入(test://login)
<activity android:name=".LoginActivity">
     <intent-filter>
           <action android:name="android.intent.action.VIEW" />

           <category android:name="android.intent.category.DEFAULT" />
           <category android:name="android.intent.category.BROWSABLE" />

           <data
                android:host="login"
                android:scheme="test" />
      </intent-filter>
</activity>
//首頁(test://home)
<activity android:name=".MainActivity">
       <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
       </intent-filter>

       <intent-filter>
             <action android:name="android.intent.action.VIEW" />

             <category android:name="android.intent.category.DEFAULT" />
             <category android:name="android.intent.category.BROWSABLE" />

             <data
                  android:host="home"
                  android:scheme="test" />
       </intent-filter>
</activity>

2.取得url傳過來的參數

test://login?account=tony123
val intent = intent
val uri = intent.data

uri?.also {
    host.text = it.host             //login
    data.text = intent.dataString   //test://login?account=tony123
    account.text = it.getQueryParameter("account")  //tony123
}

另一種方式

1.返回自己APP不是瀏覽器(設置parentActivityName)

<activity android:name=".RegisterActivity"
    android:parentActivityName=".MainActivity"/>

<activity android:name=".LoginActivity"
    android:parentActivityName=".MainActivity"/>

<activity android:name=".MainActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

    <intent-filter>
         <action android:name="android.intent.action.VIEW" />

         <category android:name="android.intent.category.DEFAULT" />
         <category android:name="android.intent.category.BROWSABLE" />

         <data
                    android:scheme="test" />
     </intent-filter>
</activity>

2.MainActivity控制跳轉的概念

val host = intent.data?.host


if (host.equals("login")) {
      //header("Location: test://login");
      TaskStackBuilder.create(this)
            .addParentStack(LoginActivity::class.java)
            .addNextIntent(Intent(this, LoginActivity::class.java))
            .startActivities()
} else if (host.equals("register")) {
       //header("Location: test://register");
       TaskStackBuilder.create(this)
            .addParentStack(RegisterActivity::class.java)
            .addNextIntent(Intent(this, RegisterActivity::class.java))
            .startActivities()
}

發表迴響