当已安装应用的用户点击 Adjust 链接时,直接深度链接可确保将用户直接转到应用内的特定内容。
Android 提供两种接收直接深度链接的方法。在每个方法中,您将使用下列其中一种方法将深度链接传递给 Adjust SDK:
设置
处理并解析深度链接 (推荐)
使用 processDeeplink 方法,该方法会执行以下操作:
- 记录深度链接点击的归因
- 将品牌化短链接解析为其等效的品牌化长链接
- 按原样透传所有其他链接
然后,应用可以通过解析已解析的链接并跳转至相应的界面来处理该链接。可以使用此方法处理所有深度链接,包括 Adjust 品牌化长链接、其他 Android 应用链接以及应用 Scheme 深度链接。
public static void processDeeplink(Uri url, Context context, OnDeeplinkResolvedListener callback)处理深度链接 (旧版)
如果您使用的不是短品牌化链接,那么 appWillOpenUrl 方法就会记录来自深度链接点击的归因。
public static void appWillOpenUrl(Uri url, Context context)processDeeplink 方法会取代该遗留方法。
实施
将以下代码添加至您的主要活动以处理深度链接。
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 /*13 * Send deep link to Adjust's servers for attribution.14 * If short branded link, receive long link.15 * Otherwise, receive original link.16 */17 Adjust.processDeeplink(incomingLink, this) { resolvedLink ->18 if (resolvedLink == null) return@processDeeplink19 /*20 * Possible resolvedLink formats:21 *22 * 1. https://brandname.go.link/?adj_t=def456&23 * adj_link=https%3A%2F%2Fexample.com%2Fsummer-clothes%3Fpromo%3Dbeach24 * -> extract and decode the deep link from adj_link25 *26 * 2. https://brandname.go.link/summer-clothes?promo=beach&adj_t=def45627 * -> extract path from the URL28 *29 * 3. https://brandname.go.link/?adj_t=def456&30 * adj_deep_link=example%3A%2F%2Fsummer-clothes%3Fpromo%3Dbeach31 * -> extract and decode the deep link from adj_deep_link32 * (used when Android app scheme path differs from iOS)33 *34 * 4. example://summer-clothes?promo=beach&adj_t=def45635 * -> extract path from the URL36 *37 * TODO: Handle the deep link and navigate to the appropriate screen.38 */39 }40 // Optional: prevent handling the same deep link multiple times41 intent.data = null42 }43
44 // Receive deep link in an existing Activity instance.45 override fun onNewIntent(intent: Intent) {46 super.onNewIntent(intent)47 setIntent(intent)48
49 // Get deep link from intent50 val incomingLink = intent?.data51 Log.d("IncomingLink", "onNewIntent: url=$incomingLink")52
53 if (incomingLink == null) return54
55 /*56 * Send deep link to Adjust's servers for attribution.57 * If short branded link, receive long link.58 * Otherwise, receive original link.59 */60 Adjust.processDeeplink(incomingLink, this) { resolvedLink ->61 if (resolvedLink == null) return@processDeeplink62 /*63 * Possible resolvedLink formats:64 *65 * 1. https://brandname.go.link/?adj_t=def456&66 * adj_link=https%3A%2F%2Fexample.com%2Fsummer-clothes%3Fpromo%3Dbeach67 * -> extract and decode the deep link from adj_link68 *69 * 2. https://brandname.go.link/summer-clothes?promo=beach&adj_t=def45670 * -> extract path from the URL71 *72 * 3. https://brandname.go.link/?adj_t=def456&73 * adj_deep_link=example%3A%2F%2Fsummer-clothes%3Fpromo%3Dbeach74 * -> extract and decode the deep link from adj_deep_link75 * (used when Android app scheme path differs from iOS)76 *77 * 4. example://summer-clothes?promo=beach&adj_t=def45678 * -> extract path from the URL79 *80 * TODO: Handle the deep link and navigate to the appropriate screen.81 */82 }83 // Optional: prevent handling the same deep link multiple times84 intent.data = null85 }86
87}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 /*17 * Send deep link to Adjust's servers for attribution.18 * If short branded link, receive long link.19 * Otherwise, receive original link.20 */21 Adjust.processDeeplink(incomingLink, this, new OnDeeplinkResolvedListener() {22 @Override23 public void onDeeplinkResolved(String resolvedLink) {24 if (resolvedLink == null) {25 return;26 }27 /*28 * Possible resolvedLink formats:29 *30 * 1. https://brandname.go.link/?adj_t=def456&31 * adj_link=https%3A%2F%2Fexample.com%2Fsummer-clothes%3Fpromo%3Dbeach32 * -> extract and decode the deep link from adj_link33 *34 * 2. https://brandname.go.link/summer-clothes?promo=beach&adj_t=def45635 * -> extract path from the URL36 *37 * 3. https://brandname.go.link/?adj_t=def456&38 * adj_deep_link=example%3A%2F%2Fsummer-clothes%3Fpromo%3Dbeach39 * -> extract and decode the deep link from adj_deep_link40 * (used when Android app scheme path differs from iOS)41 *42 * 4. example://summer-clothes?promo=beach&adj_t=def45643 * -> extract path from the URL44 *45 * TODO: Handle the deep link and navigate to the appropriate screen.46 */47 }48 });49 // Optional: prevent handling the same deep link multiple times50 appIntent.setData(null);51 }52 }53
54 // Receive deep link in an existing Activity instance.55 @Override56 protected void onNewIntent(Intent intent) {57 super.onNewIntent(intent);58 setIntent(intent); // Update the current intent with the new one59
60 Uri incomingLink = null;61 if (intent != null) {62 incomingLink = intent.getData();63 }64 Log.d("IncomingLink", "onNewIntent: url=" + incomingLink);65
66 if (incomingLink != null) {67 /*68 * Send deep link to Adjust's servers for attribution.69 * If short branded link, receive long link.70 * Otherwise, receive original link.71 */72 Adjust.processDeeplink(incomingLink, this, new OnDeeplinkResolvedListener() {73 @Override74 public void onDeeplinkResolved(String resolvedLink) {75 if (resolvedLink == null) {76 return;77 }78 /*79 * Possible resolvedLink formats:80 *81 * 1. https://brandname.go.link/?adj_t=def456&82 * adj_link=https%3A%2F%2Fexample.com%2Fsummer-clothes%3Fpromo%3Dbeach83 * -> extract and decode the deep link from adj_link84 *85 * 2. https://brandname.go.link/summer-clothes?promo=beach&adj_t=def45686 * -> extract path from the URL87 *88 * 3. https://brandname.go.link/?adj_t=def456&89 * adj_deep_link=example%3A%2F%2Fsummer-clothes%3Fpromo%3Dbeach90 * -> extract and decode the deep link from adj_deep_link91 * (used when Android app scheme path differs from iOS)92 *93 * 4. example://summer-clothes?promo=beach&adj_t=def45694 * -> extract path from the URL95 *96 * TODO: Handle the deep link and navigate to the appropriate screen.97 */98 }99 });100 // Optional: prevent handling the same deep link multiple times101 intent.setData(null);102 }103 }104}专用深度链接活动
如果您使用的是专用深度链接活动方法,那么还需要创建下列文件:
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_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)20 }21 startActivity(mainActivityIntent)22 finish()23 }24
25}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_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);22
23 startActivity(mainActivityIntent);24 finish();25 }26}