deeplink是什么?
Deeplink,簡單講,就是在你手機上點擊一個鏈接后,可以直接鏈接到app內(nèi)部的某個頁面,而不是app正常打開時顯示的首頁。累似web,一個鏈接就可以直接打開web的網(wǎng)頁,app的內(nèi)頁打開,可以使用deeplink來實現(xiàn)
使用
在清單文件配置

代碼.png
主要代碼BROWSABLE意思是可以被外部瀏覽器打開,host和scheme類似http://xxxx,在跳轉的時候會用
<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="share"
android:scheme="cc" />
</intent-filter>
寫一個html鏈接過去<a href="cc://share/20">嘗試打開deeplink界面</a>要與上面data里面的字段的對應起來
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<a href="cc://share/20">嘗試打開deeplink界面</a>
注意:cc就是自己寫的scheme,share是host可以自定義任意字符串,20是模擬傳遞的值
</body>
</html>
在MainActivity寫一個webview用戶加載剛寫的網(wǎng)頁
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = findViewById(R.id.web_view);
webView.loadUrl("file:///android_asset/" + "deeplink.html");
}
}
在deeplinkActivity獲取傳遞的值
public class DeepLinkActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_deep_link);
TextView tv = findViewById(R.id.tv);
//獲得網(wǎng)頁傳遞過來的
Intent intent = getIntent();
if (intent != null) {
Uri uri = intent.getData();
String scheme = uri.getScheme();//cc
String host = uri.getHost();//share
if ("share".equals(host)) {
// 跳轉到對應詳情頁面
Toast.makeText(this, "host:"+host+"scheme:"+scheme+"值:"+uri.getPathSegments(), Toast.LENGTH_SHORT).show();
tv.setText("host:"+host+"\nscheme:"+scheme+"\n值:"+uri.getPathSegments());
}
}
}
}
效果點擊后就打開我們寫的詳情頁了,我是寫在一個app里的,當然只要是webview就可以打開對應的詳情頁,兩個應用之間的跳轉就完成了
封裝方法用來activity之間的跳轉
/**
* deepLink跳轉
**/
protected void deepLinkJump(String deepLinkUrl) {
Uri uri = Uri.parse(deepLinkUrl);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
點擊事件中調用
deepLinkJump("cc://chat_activity");//記得在清單文件添加host和scheme哦!
如下圖

image.png

image.png

image.png