我在我的应用中支持了深层链接
<activity android:name=".DeepLinkActivity" android:noHistory="true"></activity>
<activity-alias
android:name="com.example.Launcher"
android:targetActivity=".DeepLinkActivity">
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" />
<data android:scheme="http" />
<data android:scheme="@string/SCHEMA" />
<data android:host="@string/WEB_DOMAIN" />
<data android:host="@string/WWW_WEB_DOMAIN" />
<data android:path="/" />
<data android:path="/x" android:pathPattern="/x/.*" />
</intent-filter>
</activity-alias>
因此,每当用户点击www.example.com时,Android应用程序会要求打开应用程序或网络,但我不希望当我的用户在移动网站上时,应该要求他们在应用程序中打开。我已经浏览了很多stackoverflow帖子但是每个人都说它不可能,但仍有很多网站正在处理这种情况。
根据此Article,行为取决于用户的手势,如果用户点击任何链接,则Chrome会显示选择器,而如果用户在浏览器上输入网址,那么它就不会。
答案 0 :(得分:6)
经过大量的研究,我已经解决了这个问题。你可以使用任何一种方式。
移动网站处理:因此,如果您想要将用户定位到Chrome浏览器上,那么您可以通过将所有网址重定向到
来实现此目的。处理应用
创建单个透明活动来处理所有深层链接
使用pathpattern ='。*'
将您不想在应用中处理的网址重定向到Chrome。
我不知道这背后的确切逻辑,但它有效。
<强>的AndroidManifest.xml 强>
<activity
android:name=".DeepLinkActivity"
android:noHistory="true"
android:theme="@style/Theme.Transparent">
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" />
<data android:scheme="http" />
<data android:scheme="@string/SCHEMA" />
<data android:host="@string/WEB_DOMAIN" />
<data android:host="@string/WWW_WEB_DOMAIN" />
<data android:pathPattern="/.*" />
</intent-filter>
</activity>
<强> DeepLinkActivity 强>
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
try {
if (Intent.ACTION_VIEW.equals(getIntent().getAction())) {
Uri uri = getIntent().getData();
if (uri == null) throw new IllegalArgumentException("Uri can not be null");
Intent intent = null;
if (getString(R.string.SCHEMA).equals(uri.getScheme()) || uri.toString().matches(Links.REGEX)) {
intent = linkIntent(uri);
}
if (intent == null) SystemUtil.launchUrlInDefaultBrowser(uri, this); else startActivity(intent);
}
} catch (IllegalArgumentException e) {
Toast.makeText(this, R.string.can_not_open_url, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(this, R.string.can_not_open_url, Toast.LENGTH_SHORT).show();
} finally {
finish();
}
}
/**
* This will open the provided url in browser except the current app.
* @param url Uri
* @param context Activity Context
*/
public static void launchUrlInDefaultBrowser(Uri url, Context context) {
try {
ResolveInfo packageInfo = null;
final Intent browserIntent = new Intent(Intent.ACTION_VIEW);
browserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
browserIntent.setData(url);
// Try to find anything that we can launch the URL with. Pick up the first one that can.
final List<ResolveInfo> resolveInfoList = context.getPackageManager().queryIntentActivities(browserIntent, PackageManager.MATCH_DEFAULT_ONLY);
if (!resolveInfoList.isEmpty()) {
for (ResolveInfo list : resolveInfoList) {
if (!BuildConfig.APP_PACKAGE_NAME.equals(list.activityInfo.packageName)) {
packageInfo = list;
break;
}
}
}
if (packageInfo != null) {
browserIntent.setClassName(packageInfo.activityInfo.packageName, packageInfo.activityInfo.name);
context.startActivity(browserIntent);
} else {
Toast.makeText(context, R.string.can_not_open_url, Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Toast.makeText(context, R.string.can_not_open_url, Toast.LENGTH_SHORT).show();
}
}