- 1.指定scheme跳轉規則,關於scheme的協議規則,這裏不做過多解釋,[scheme]://[host]/[path]?[query]。好比暫時是這樣設定的:yilu://link/?page=main。
- 2.被喚起方,客戶端須要配置清單文件activity。關於SchemeActivity注意查看下面代碼:
- 爲何要配置intent-filter,它是針對你跳轉的目標來說的,好比你要去某個朋友的家,就相似於門牌的修飾,他會在門牌上定義上述介紹的那些屬性,方便你定位。當有intent發送過來的時候,就會篩選出符合條件的app來。
- action.VIEW是打開一個視圖,在Android 系統中點擊連接會發送一條action=VIEW的隱式意圖,這個必須配置。
- category.DEFAULT爲默認,category.DEFAULT爲設置該組件可使用瀏覽器啓動,這個是關鍵,從瀏覽器跳轉,就要經過這個屬性。
<!--用於DeepLink,html跳到此頁面 scheme_Adr: 'yilu://link/?page=main',-->
<activity android:name=".activity.link.SchemeActivity"
android:screenOrientation="portrait">
<!--Android 接收外部跳轉過濾器-->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- 協議部分配置 ,要在web配置相同的-->
<!--yilu://link/?page=main-->
<data
android:host="link"
android:scheme="yilu" />
</intent-filter>
</activity>
複製代碼
@Override
public void onCreate(Bundle savesInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent=getIntent();
String action=intent.getAction();
Uri data=intent.getData();
String scheme=data.getScheme();
String host=data.getHost();
String path=data.getPath();
int port=data.getPort();
Set<String> paramKeySet=data.getQueryParameterNames();
String page = uri.getQueryParameter("page");
switch (page) {
case "main":
Intent intent1 = new Intent(this, MainActivity.class);
readGoActivity(intent1, this);
break;
case "full":
Intent intent2 = new Intent(this, TestFullActivity.class);
readGoActivity(intent2, this);
break;
case "list":
Intent intent3 = new Intent(this, TestListActivity.class);
String id = getValueByName(url, "id");
intent3.putExtra("id",id);
readGoActivity(intent3, this);
break;
default:
Intent intent = new Intent(this, MainActivity.class);
readGoActivity(intent, this);
break;
}
}
複製代碼
- 3.喚起方也須要操做
Intent intent=new Intent();
intent.setData(Uri.parse("yilu://link/?page=main"));
startActivity(intent);
複製代碼
- 4.關於問題疑惑點解決方案
- 配置了scheme協議,測試能夠打開app,可是想跳到具體頁面,攜帶參數,又該如何實現呢?
- 好比則能夠配置:yilu://link/?page=car&id=520,則能夠跳轉到汽車詳情頁面,而後傳遞的id參數是520。
- 5.跳轉頁面後的優化
- 經過以上規則匹配上,你點擊跳轉之後,若是用戶結束這個Activity的話,就直接回到桌面了,這個是比較奇怪的。參考一些其餘app,發現不論是跳轉指定的幾級頁面,點擊返回是回到首頁,那麼這個是如何作到的呢?代碼以下所示
public void readGoActivity(Intent intent, Context context) {
if (isAppRunning(context, context.getPackageName())) {
openActivity(intent, context);
} else {
reStartActivity(intent, context);
}
}
public void openActivity(Intent intent, Context context) {
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
public void reStartActivity(Intent intent, Context context) {
Intent[] intents = new Intent[2];
Intent mainIntent = new Intent(context, MainActivity.class);
mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intents[0] = mainIntent;
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intents[1] = intent;
context.startActivities(intents);
}
複製代碼
- 6.短信息竟沒法識別scheme協議?
- 把yilu://link/?page=main以短信息發送出去,而後在短信息裏點擊連接,發如今短信裏面添加的連接自定義的scheme被認爲不是一個scheme……可見終究跳不開的http/https訪問。
- 7.如何將一個http或https連接生成短連接
- 這個很容易,直接找個短連接生成的網站,而後把連接轉化一下就能夠。至於轉化的原理,我暫時也不清楚……
- deeplink的scheme相應分兩種:一種是隻有一個APP能相應,另外一種是有多個APP能夠相應,好比,若是爲一個APP的Activity配置了http scheme類型的deepLink,若是經過短信或者其餘方式喚起這種link的時候,通常會出現一個讓用戶選擇的彈窗,由於通常而言,系統會帶個瀏覽器,也相應這類scheme。這裏就不舉例子了,由於上面已經已經提到呢。固然,若是私有scheme跟其餘APP的重複了,仍是會喚起APP選擇界面(實際上是一個ResolverActivity)。下面就來看看scheme是如何匹配並拉起對應APP的。
- startActivity入口與ResolverActivity
- 不管APPLink跟DeepLink其實都是經過喚起一個Activity來實現界面的跳轉,不管從APP外部:好比短信、瀏覽器,仍是APP內部。經過在APP內部模擬跳轉來看看具體實現,寫一個H5界面,而後經過Webview加載,不過Webview不進行任何設置,這樣跳轉就須要系統進行解析,走deeplink這一套:
<html>
<body>
<a href="yilu://link/?page=main">當即打開一鹿報價頁面(直接打開)>></a>
</body>
</html>
複製代碼
- 點擊Scheme跳轉,通常會喚起以下界面,讓用戶選擇打開方式:
- 經過adb打印log,你會發現ActivityManagerService會打印這樣一條Log:
ActivityManager: START u0 {act=android.intent.action.VIEW dat=yilu:
複製代碼
- 其實看到的選擇對話框就是ResolverActivity
- 不過咱們先來看看究竟是走到ResolverActivity的,也就是這個scheme怎麼會喚起App選擇界面,在短信中,或者Webview中遇到scheme,他們通常會發出相應的Intent(固然第三方APP可能會屏蔽掉,好比微信就換不起APP),其實上面的做用跟下面的代碼結果同樣:
Intent intent = new Intent()
intent.setAction("android.intent.action.VIEW")
intent.setData(Uri.parse("https://yc.com/history/520"))
intent.addCategory("android.intent.category.DEFAULT")
intent.addCategory("android.intent.category.BROWSABLE")
startActivity(intent)
複製代碼
- 那剩下的就是看startActivity,在源碼中,startActivity最後會經過ActivityManagerService調用ActivityStatckSupervisor的startActivityMayWait
final int startActivityMayWait(IApplicationThread caller, int callingUid, String callingPackage, Intent intent, String resolvedType, IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, IBinder resultTo, String resultWho, int requestCode, int startFlags, ProfilerInfo profilerInfo, WaitResult outResult, Configuration config, Bundle options, boolean ignoreTargetSecurity, int userId, IActivityContainer iContainer, TaskRecord inTask) {
...
boolean componentSpecified = intent.getComponent() != null;
intent = new Intent(intent);
ActivityInfo aInfo = resolveActivity(intent, resolvedType, startFlags, profilerInfo, userId);
...
}
複製代碼
- startActivityMayWait會經過resolveActivity先找到目標Activity,這個過程當中,可能找到多個匹配的Activity,這就是ResolverActivity的入口:
ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags, ProfilerInfo profilerInfo, int userId) {
ActivityInfo aInfo;
try {
ResolveInfo rInfo =
AppGlobals.getPackageManager().resolveIntent(
intent, resolvedType,
PackageManager.MATCH_DEFAULT_ONLY
| ActivityManagerService.STOCK_PM_FLAGS, userId);
aInfo = rInfo != null ? rInfo.activityInfo : null;
} catch (RemoteException e) {
aInfo = null;
}
複製代碼
- 能夠認爲,全部的四大組件的信息都在PackageManagerService中有登記,想要找到這些類,就必須向PackagemanagerService查詢
@Override
public ResolveInfo resolveIntent(Intent intent, String resolvedType, int flags, int userId) {
if (!sUserManager.exists(userId)) return null;
enforceCrossUserPermission(Binder.getCallingUid(), userId, false, false, "resolve intent");
List<ResolveInfo> query = queryIntentActivities(intent, resolvedType, flags, userId);
return chooseBestActivity(intent, resolvedType, flags, query, userId);
}
複製代碼
- PackageManagerService會經過queryIntentActivities找到全部適合的Activity,再經過chooseBestActivity提供選擇的權利。這裏分以下三種狀況:
- 僅僅找到一個,直接啓動
- 找到了多個,而且設置了其中一個爲默認啓動,則直接啓動相應Acitivity
- 找到了多個,切沒有設置默認啓動,則啓動ResolveActivity供用戶選擇
- 關於如何查詢,匹配的這裏不詳述,僅僅簡單看看如何喚起選擇頁面,或者默認打開,比較關鍵的就是chooseBestActivity
private ResolveInfo chooseBestActivity(Intent intent, String resolvedType, int flags, List<ResolveInfo> query, int userId) {
<!--查詢最好的Activity-->
ResolveInfo ri = findPreferredActivity(intent, resolvedType,
flags, query, r0.priority, true, false, debug, userId);
if (ri != null) {
return ri;
}
...
}
ResolveInfo findPreferredActivity(Intent intent, String resolvedType, int flags, List<ResolveInfo> query, int priority, boolean always, boolean removeMatches, boolean debug, int userId) {
if (!sUserManager.exists(userId)) return null;
synchronized (mPackages) {
if (intent.getSelector() != null) {
intent = intent.getSelector();
}
<!--若是用戶已經選擇過默認打開的APP,則這裏返回的就是相對應APP中的Activity-->
ResolveInfo pri = findPersistentPreferredActivityLP(intent, resolvedType, flags, query,
debug, userId);
if (pri != null) {
return pri;
}
<!--找Activity-->
PreferredIntentResolver pir = mSettings.mPreferredActivities.get(userId);
...
final ActivityInfo ai = getActivityInfo(pa.mPref.mComponent,
flags | PackageManager.GET_DISABLED_COMPONENTS, userId);
...
}
@Override
public ActivityInfo getActivityInfo(ComponentName component, int flags, int userId) {
if (!sUserManager.exists(userId)) return null;
enforceCrossUserPermission(Binder.getCallingUid(), userId, false, false, "get activity info");
synchronized (mPackages) {
...
<!--弄一個ResolveActivity的ActivityInfo-->
if (mResolveComponentName.equals(component)) {
return PackageParser.generateActivityInfo(mResolveActivity, flags,
new PackageUserState(), userId);
}
}
return null;
}
複製代碼
- 其實上述流程比較複雜,這裏只是本身簡單猜測下流程,找到目標Activity後,不管是真的目標Acitiviy,仍是ResolveActivity,都會經過startActivityLocked繼續走啓動流程,這裏就會看到以前打印的Log信息:
final int startActivityLocked(IApplicationThread caller...{
if (err == ActivityManager.START_SUCCESS) {
Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
+ "} from uid " + callingUid
+ " on display " + (container == null ? (mFocusedStack == null ?
Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
(container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
container.mActivityDisplay.mDisplayId)));
}
複製代碼
- 若是是ResolveActivity還會根據用戶選擇的信息將一些設置持久化到本地,這樣下次就能夠直接啓動用戶的偏好App。其實以上就是deeplink的原理,說白了一句話:scheme就是隱式啓動Activity,若是能找到惟一或者設置的目標Acitivity則直接啓動,若是找到多個,則提供APP選擇界面。
- 以前分析deeplink的時候提到了ResolveActivity這麼一個選擇過程,而AppLink就是自動幫用戶完成這個選擇過程,而且選擇的scheme是最適合它的scheme(開發者的角度)。所以對於AppLink要分析的就是如何完成了這個默認選擇的過程。
- 目前Android源碼提供的是一個雙向認證的方案:在APP安裝的時候,客戶端根據APP配置像服務端請求,若是知足條件,scheme跟服務端配置匹配的上,就爲APP設置默認啓動選項,因此這個方案很明顯,在安裝的時候須要聯網才行,不然就是徹底不會驗證,那就是普通的deeplink,既然是在安裝的時候去驗證,那就看看PackageManagerService是如何處理這個流程的,具體找到installPackageLI方法:
private void installPackageLI(InstallArgs args, PackageInstalledInfo res) {
final int installFlags = args.installFlags;
<!--開始驗證applink-->
startIntentFilterVerifications(args.user.getIdentifier(), replace, pkg);
...
}
private void startIntentFilterVerifications(int userId, boolean replacing, PackageParser.Package pkg) {
if (mIntentFilterVerifierComponent == null) {
return;
}
final int verifierUid = getPackageUid(
mIntentFilterVerifierComponent.getPackageName(),
(userId == UserHandle.USER_ALL) ? UserHandle.USER_OWNER : userId);
mHandler.removeMessages(START_INTENT_FILTER_VERIFICATIONS);
final Message msg = mHandler.obtainMessage(START_INTENT_FILTER_VERIFICATIONS);
msg.obj = new IFVerificationParams(pkg, replacing, userId, verifierUid);
mHandler.sendMessage(msg);
}
複製代碼
- 能夠看到發送了一個handler消息,那麼消息裏作了什麼呢?看一下startIntentFilterVerifications發送一個消息開啓驗證,隨後調用verifyIntentFiltersIfNeeded進行驗證,代碼以下所示:
- 以看出,驗證就三步:檢查、蒐集、驗證。在檢查階段,首先看看是否有設置http/https scheme的Activity,而且是否知足設置了Intent.ACTION_DEFAULT與Intent.ACTION_VIEW,若是沒有,則壓根不須要驗證
case START_INTENT_FILTER_VERIFICATIONS: {
IFVerificationParams params = (IFVerificationParams) msg.obj;
verifyIntentFiltersIfNeeded(params.userId, params.verifierUid,
params.replacing, params.pkg);
break;
}
private void verifyIntentFiltersIfNeeded(int userId, int verifierUid, boolean replacing, PackageParser.Package pkg) {
...
<!--檢查是否有Activity設置了AppLink-->
final boolean hasDomainURLs = hasDomainURLs(pkg);
if (!hasDomainURLs) {
if (DEBUG_DOMAIN_VERIFICATION) Slog.d(TAG,
"No domain URLs, so no need to verify any IntentFilter!");
return;
}
<!--是否autoverigy-->
boolean needToVerify = false;
for (PackageParser.Activity a : pkg.activities) {
for (ActivityIntentInfo filter : a.intents) {
<!--needsVerification是否設置autoverify -->
if (filter.needsVerification() && needsNetworkVerificationLPr(filter)) {
needToVerify = true;
break;
}
}
}
<!--若是有蒐集須要驗證的Activity信息及scheme信息-->
if (needToVerify) {
final int verificationId = mIntentFilterVerificationToken++;
for (PackageParser.Activity a : pkg.activities) {
for (ActivityIntentInfo filter : a.intents) {
if (filter.handlesWebUris(true) && needsNetworkVerificationLPr(filter)) {
if (DEBUG_DOMAIN_VERIFICATION) Slog.d(TAG,
"Verification needed for IntentFilter:" + filter.toString());
mIntentFilterVerifier.addOneIntentFilterVerification(
verifierUid, userId, verificationId, filter, packageName);
count++;
} } } } }
<!--開始驗證-->
if (count > 0) {
mIntentFilterVerifier.startVerifications(userId);
}
}
複製代碼
- 具體看一下hasDomainURLs到底作了什麼?
private static boolean hasDomainURLs(Package pkg) {
if (pkg == null || pkg.activities == null) return false;
final ArrayList<Activity> activities = pkg.activities;
final int countActivities = activities.size();
for (int n=0; n<countActivities; n++) {
Activity activity = activities.get(n);
ArrayList<ActivityIntentInfo> filters = activity.intents;
if (filters == null) continue;
final int countFilters = filters.size();
for (int m=0; m<countFilters; m++) {
ActivityIntentInfo aii = filters.get(m);
if (!aii.hasAction(Intent.ACTION_VIEW)) continue;
if (!aii.hasAction(Intent.ACTION_DEFAULT)) continue;
if (aii.hasDataScheme(IntentFilter.SCHEME_HTTP) ||
aii.hasDataScheme(IntentFilter.SCHEME_HTTPS)) {
return true;
}
}
}
return false;
}
複製代碼
- 檢查的第二步試看看是否設置了autoverify,固然中間還有些是否設置過,用戶是否選擇過的操做,比較複雜,不分析,不過不影響對流程的理解:
public final boolean needsVerification() {
return getAutoVerify() && handlesWebUris(true);
}
public final boolean getAutoVerify() {
return ((mVerifyState & STATE_VERIFY_AUTO) == STATE_VERIFY_AUTO);
}
複製代碼
- 只要找到一個知足以上條件的Activity,就開始驗證。若是想要開啓applink,Manifest中配置必須像下面這樣
<intent-filter android:autoVerify="true">
<data android:scheme="https" android:host="xxx.com" />
<data android:scheme="http" android:host="xxx.com" />
<!--外部intent打開,好比短信,文本編輯等-->
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
複製代碼
- 蒐集其實就是蒐集intentfilter信息,下面直接看驗證過程
@Override
public void startVerifications(int userId) {
...
sendVerificationRequest(userId, verificationId, ivs);
}
mCurrentIntentFilterVerifications.clear();
}
private void sendVerificationRequest(int userId, int verificationId, IntentFilterVerificationState ivs) {
Intent verificationIntent = new Intent(Intent.ACTION_INTENT_FILTER_NEEDS_VERIFICATION);
verificationIntent.putExtra(
PackageManager.EXTRA_INTENT_FILTER_VERIFICATION_ID,
verificationId);
verificationIntent.putExtra(
PackageManager.EXTRA_INTENT_FILTER_VERIFICATION_URI_SCHEME,
getDefaultScheme());
verificationIntent.putExtra(
PackageManager.EXTRA_INTENT_FILTER_VERIFICATION_HOSTS,
ivs.getHostsString());
verificationIntent.putExtra(
PackageManager.EXTRA_INTENT_FILTER_VERIFICATION_PACKAGE_NAME,
ivs.getPackageName());
verificationIntent.setComponent(mIntentFilterVerifierComponent);
verificationIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
UserHandle user = new UserHandle(userId);
mContext.sendBroadcastAsUser(verificationIntent, user);
}
複製代碼
- 目前Android的實現是經過發送一個廣播來進行驗證的,也就是說,這是個異步的過程,驗證是須要耗時的(網絡請求),因此安裝後,通常要等個幾秒Applink才能生效,廣播的接受處理者是:IntentFilterVerificationReceiver
public final class IntentFilterVerificationReceiver extends BroadcastReceiver {
private static final String TAG = IntentFilterVerificationReceiver.class.getSimpleName();
...
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (Intent.ACTION_INTENT_FILTER_NEEDS_VERIFICATION.equals(action)) {
Bundle inputExtras = intent.getExtras();
if (inputExtras != null) {
Intent serviceIntent = new Intent(context, DirectStatementService.class);
serviceIntent.setAction(DirectStatementService.CHECK_ALL_ACTION);
...
serviceIntent.putExtras(extras);
context.startService(serviceIntent);
}
複製代碼
- IntentFilterVerificationReceiver收到驗證消息後,經過start一個DirectStatementService進行驗證,兜兜轉轉最終調用IsAssociatedCallable的verifyOneSource
private class IsAssociatedCallable implements Callable<Void> {
private boolean verifyOneSource(AbstractAsset source, AbstractAssetMatcher target, Relation relation) throws AssociationServiceException {
Result statements = mStatementRetriever.retrieveStatements(source);
for (Statement statement : statements.getStatements()) {
if (relation.matches(statement.getRelation())
&& target.matches(statement.getTarget())) {
return true;
}
}
return false;
}
複製代碼
- IsAssociatedCallable會逐一對須要驗證的intentfilter進行驗證,具體是經過DirectStatementRetriever的retrieveStatements來實現:
Override public Result retrieveStatements(AbstractAsset source) throws AssociationServiceException {
if (source instanceof AndroidAppAsset) {
return retrieveFromAndroid((AndroidAppAsset) source);
} else if (source instanceof WebAsset) {
return retrieveFromWeb((WebAsset) source);
} else {
..
}
}
複製代碼
- AndroidAppAsset好像是Google的另外一套assetlink類的東西,好像用在APP web登錄信息共享之類的地方 ,不看,直接看retrieveFromWeb:從名字就能看出,這是獲取服務端Applink的配置,獲取後跟本地校驗,若是經過了,那就是applink啓動成功:
private Result retrieveStatementFromUrl(String urlString, int maxIncludeLevel, AbstractAsset source) throws AssociationServiceException {
List<Statement> statements = new ArrayList<Statement>();
if (maxIncludeLevel < 0) {
return Result.create(statements, DO_NOT_CACHE_RESULT);
}
WebContent webContent;
try {
URL url = new URL(urlString);
if (!source.followInsecureInclude()
&& !url.getProtocol().toLowerCase().equals("https")) {
return Result.create(statements, DO_NOT_CACHE_RESULT);
}
<!--經過網絡請求獲取配置-->
webContent = mUrlFetcher.getWebContentFromUrlWithRetry(url,
HTTP_CONTENT_SIZE_LIMIT_IN_BYTES, HTTP_CONNECTION_TIMEOUT_MILLIS,
HTTP_CONNECTION_BACKOFF_MILLIS, HTTP_CONNECTION_RETRY);
} catch (IOException | InterruptedException e) {
return Result.create(statements, DO_NOT_CACHE_RESULT);
}
try {
ParsedStatement result = StatementParser
.parseStatementList(webContent.getContent(), source);
statements.addAll(result.getStatements());
<!--若是有一對多的狀況,或者說設置了「代理」,則循環獲取配置-->
for (String delegate : result.getDelegates()) {
statements.addAll(
retrieveStatementFromUrl(delegate, maxIncludeLevel - 1, source)
.getStatements());
}
<!--發送結果-->
return Result.create(statements, webContent.getExpireTimeMillis());
} catch (JSONException | IOException e) {
return Result.create(statements, DO_NOT_CACHE_RESULT);
}
}
複製代碼
- 其實就是經過UrlFetcher獲取服務端配置,而後發給以前的receiver進行驗證:
public WebContent getWebContentFromUrl(URL url, long fileSizeLimit, int connectionTimeoutMillis) throws AssociationServiceException, IOException {
final String scheme = url.getProtocol().toLowerCase(Locale.US);
if (!scheme.equals("http") && !scheme.equals("https")) {
throw new IllegalArgumentException("The url protocol should be on http or https.");
}
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) url.openConnection();
connection.setInstanceFollowRedirects(true);
connection.setConnectTimeout(connectionTimeoutMillis);
connection.setReadTimeout(connectionTimeoutMillis);
connection.setUseCaches(true);
connection.setInstanceFollowRedirects(false);
connection.addRequestProperty("Cache-Control", "max-stale=60");
...
return new WebContent(inputStreamToString(
connection.getInputStream(), connection.getContentLength(), fileSizeLimit),
expireTimeMillis);
}
複製代碼
- 看到這裏的HttpURLConnection就知道爲何Applink需在安裝時聯網纔有效,到這裏其實就能夠理解的差很少,後面其實就是針對配置跟App自身的配置進行校驗,若是經過就設置默認啓動,並持久化,驗證成功的話能夠經過。