当已安装应用的用户点击 Adjust 链接时,直接深度链接可确保将用户直接转到应用内的特定内容。
Android 提供两种接收直接深度链接的方法。在每个方法中,您将使用下列其中一种方法将深度链接传递给 Adjust SDK:
设置
处理并解析深度链接 (推荐)
使用 processAndResolveDeeplink 方法,该方法会执行以下操作:
- 记录深度链接点击的归因
- 将品牌化短链接解析为其等效的品牌化长链接
- 按原样透传所有其他链接
然后,应用可以通过解析已解析的链接并跳转至相应的界面来处理该链接。可以使用此方法处理所有深度链接,包括 Adjust 品牌化长链接、其他 Android 应用链接以及应用 Scheme 深度链接。
public static void processAndResolveDeeplink(AdjustDeeplink adjustDeeplink, Context context, OnDeeplinkResolvedListener callback)处理深度链接 (旧版)
如果您使用的不是短品牌化链接,那么 processDeeplink 方法就会记录来自深度链接点击的归因。
public static void processDeeplink(AdjustDeeplink adjustDeeplink, Context context)processAndResolveDeeplink 方法会取代该遗留方法。
实施
将以下代码添加至您的主要活动,以处理深度链接。
1class MainActivity : AppCompatActivity() {2 // Receive deep link when the Activity launches.3 override fun onCreate(savedInstanceState: Bundle?) {4 super.onCreate(savedInstanceState)5
6 // Get deep link from intent7 val incomingLink = intent?.data8 Log.d("IncomingLink", "onCreate: url=$incomingLink")9
10 if (incomingLink == null) return11
12 // Create deep link object13 val deeplink = AdjustDeeplink(incomingLink)14
15 /*16 * Send deep link to Adjust's servers for attribution.17 * If short branded link, receive long link.18 * Otherwise, receive original link.19 */20 Adjust.processAndResolveDeeplink(deeplink, this) { resolvedLink ->21 if (resolvedLink == null) return@processAndResolveDeeplink22 /*23 * TODO: Handle the deep link and navigate to the appropriate screen.24 *25 * Possible resolvedLink formats:26 *27 * 1. https://brandname.go.link/?adj_t=def456&28 * adj_link=https%3A%2F%2Fexample.com%2Fsummer-clothes%3Fpromo%3Dbeach29 * -> extract and decode the deep link from adj_link30 *31 * 2. https://brandname.go.link/summer-clothes?promo=beach&adj_t=def45632 * -> extract path from the URL33 *34 * 3. https://brandname.go.link/?adj_t=def456&35 * adj_deep_link=example%3A%2F%2Fsummer-clothes%3Fpromo%3Dbeach36 * -> extract and decode the deep link from adj_deep_link37 * (used when Android app scheme path differs from iOS)38 *39 * 4. example://summer-clothes?promo=beach&adj_t=def45640 * -> extract path from the URL41 */42 }43 // Optional: prevent handling the same deep link multiple times44 intent.data = null45 }46
47 // Receive deep link in an existing Activity instance.48 override fun onNewIntent(intent: Intent) {49 super.onNewIntent(intent)50 setIntent(intent)51
52 // Get deep link from intent53 val incomingLink = intent?.data54 Log.d("IncomingLink", "onNewIntent: url=$incomingLink")55
56 if (incomingLink == null) return57
58 // Create deep link object59 val deeplink = AdjustDeeplink(incomingLink)60
61 /*62 * Send deep link to Adjust's servers for attribution.63 * If short branded link, receive long link.64 * Otherwise, receive original link.65 */66 Adjust.processAndResolveDeeplink(deeplink, this) { resolvedLink ->67 if (resolvedLink == null) return@processAndResolveDeeplink68 /*69 * TODO: Handle the deep link and navigate to the appropriate screen.70 *71 * Possible resolvedLink formats:72 *73 * 1. https://brandname.go.link/?adj_t=def456&74 * adj_link=https%3A%2F%2Fexample.com%2Fsummer-clothes%3Fpromo%3Dbeach75 * -> extract and decode the deep link from adj_link76 *77 * 2. https://brandname.go.link/summer-clothes?promo=beach&adj_t=def45678 * -> extract path from the URL79 *80 * 3. https://brandname.go.link/?adj_t=def456&81 * adj_deep_link=example%3A%2F%2Fsummer-clothes%3Fpromo%3Dbeach82 * -> extract and decode the deep link from adj_deep_link83 * (used when Android app scheme path differs from iOS)84 *85 * 4. example://summer-clothes?promo=beach&adj_t=def45686 * -> extract path from the URL87 */88 }89 // Optional: prevent handling the same deep link multiple times90 intent.data = null91 }92}1public class MainActivity extends AppCompatActivity {2 // Receive deep link when the Activity launches.3 @Override4 protected void onCreate(Bundle savedInstanceState) {5 super.onCreate(savedInstanceState);6
7 // Get deep link from intent8 Intent appIntent = getIntent();9 Uri incomingLink = null;10 if (appIntent != null) {11 incomingLink = appIntent.getData();12 }13 Log.d("IncomingLink", "onCreate: url=" + incomingLink);14
15 if (incomingLink != null) {16 // Create deep link object17 AdjustDeeplink deeplink = new AdjustDeeplink(incomingLink);18
19 /*20 * Send deep link to Adjust's servers for attribution.21 * If short branded link, receive long link.22 * Otherwise, receive original link.23 */24 Adjust.processAndResolveDeeplink(deeplink, this, new OnDeeplinkResolvedListener() {25 @Override26 public void onDeeplinkResolved(String resolvedLink) {27 if (resolvedLink == null) {28 return;29 }30 /*31 * TODO: Handle the deep link and navigate to the appropriate screen.32 *33 * Possible resolvedLink formats:34 *35 * 1. https://brandname.go.link/?adj_t=def456&36 * adj_link=https%3A%2F%2Fexample.com%2Fsummer-clothes%3Fpromo%3Dbeach37 * -> extract and decode the deep link from adj_link38 *39 * 2. https://brandname.go.link/summer-clothes?promo=beach&adj_t=def45640 * -> extract path from the URL41 *42 * 3. https://brandname.go.link/?adj_t=def456&43 * adj_deep_link=example%3A%2F%2Fsummer-clothes%3Fpromo%3Dbeach44 * -> extract and decode the deep link from adj_deep_link45 * (used when Android app scheme path differs from iOS)46 *47 * 4. example://summer-clothes?promo=beach&adj_t=def45648 * -> extract path from the URL49 */50 }51 });52 // Optional: prevent handling the same deep link multiple times53 appIntent.setData(null);54 }55 }56
57 // Receive deep link in an existing Activity instance.58 @Override59 protected void onNewIntent(Intent intent) {60 super.onNewIntent(intent);61 setIntent(intent); // Update the current intent with the new one62
63 Uri incomingLink = null;64 if (intent != null) {65 incomingLink = intent.getData();66 }67 Log.d("IncomingLink", "onNewIntent: url=" + incomingLink);68
69 if (incomingLink != null) {70 // Create deep link object71 AdjustDeeplink deeplink = new AdjustDeeplink(incomingLink);72
73 /*74 * Send deep link to Adjust's servers for attribution.75 * If short branded link, receive long link.76 * Otherwise, receive original link.77 */78 Adjust.processAndResolveDeeplink(deeplink, this, new OnDeeplinkResolvedListener() {79 @Override80 public void onDeeplinkResolved(String resolvedLink) {81 if (resolvedLink == null) {82 return;83 }84 /*85 * TODO: Handle the deep link and navigate to the appropriate screen.86 *87 * Possible resolvedLink formats:88 *89 * 1. https://brandname.go.link/?adj_t=def456&90 * adj_link=https%3A%2F%2Fexample.com%2Fsummer-clothes%3Fpromo%3Dbeach91 * -> extract and decode the deep link from adj_link92 *93 * 2. https://brandname.go.link/summer-clothes?promo=beach&adj_t=def45694 * -> extract path from the URL95 *96 * 3. https://brandname.go.link/?adj_t=def456&97 * adj_deep_link=example%3A%2F%2Fsummer-clothes%3Fpromo%3Dbeach98 * -> extract and decode the deep link from adj_deep_link99 * (used when Android app scheme path differs from iOS)100 *101 * 4. example://summer-clothes?promo=beach&adj_t=def456102 * -> extract path from the URL103 */104 }105 });106 // Optional: prevent handling the same deep link multiple times107 intent.setData(null);108 }109 }110}专用深度链接活动
如果您使用的是专用深度链接活动方法,那么还需要创建下列文件:
1class DedicatedDeepLinkActivity : AppCompatActivity() {2 override fun onCreate(savedInstanceState: Bundle?) {3 super.onCreate(savedInstanceState)4 handleIntent(intent)5 }6
7 override fun onNewIntent(intent: Intent) {8 super.onNewIntent(intent)9 handleIntent(intent)10 }11
12 private fun handleIntent(intent: Intent) {13 Log.d("IncomingLink", "DedicatedDeepLinkActivity: url=${intent.data}")14
15 // Create intent to forward to MainActivity16 val mainActivityIntent = Intent(this, MainActivity::class.java).apply {17 data = intent.data18 putExtras(intent)19 addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)20 }21 startActivity(mainActivityIntent)22 finish()23 }24}1public class DedicatedDeepLinkActivity extends AppCompatActivity {2 @Override3 protected void onCreate(Bundle savedInstanceState) {4 super.onCreate(savedInstanceState);5 handleIntent(getIntent());6 }7
8 @Override9 protected void onNewIntent(Intent intent) {10 super.onNewIntent(intent);11 handleIntent(intent);12 }13
14 private void handleIntent(Intent intent) {15 Log.d("IncomingLink", "DedicatedDeepLinkActivity: url=" + intent.getData());16
17 // Create intent to forward to MainActivity18 Intent mainActivityIntent = new Intent(this, MainActivity.class);19 mainActivityIntent.setData(intent.getData());20 mainActivityIntent.putExtras(intent);21 mainActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);22
23 startActivity(mainActivityIntent);24 finish();25 }26}