我正在我的应用程序内实现深层链接,无法找到方法,也没有从我自己的应用程序内部打开它们的示例。例如:我希望打开某些横幅会打开myapp://game/1
链接,这将导致我的应用程序内发生另一项活动。我怎样才能做到这一点 ?
在清单中,您应该注册深层链接方案。
<activity android:name=".DeepLinkingActivity"
android:configChanges="orientation|screenSize" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="myapp" />
</intent-filter>
</activity>
这样,当单击具有已定义方案的链接时,DeepLinkingActivity将打开。并在活动中处理该怎么做:
private final String GAME_LINK = "game";
private final String VIDEO_LINK = "video";
private static String PASSED_LINK = "PassedLink";
public static Intent createIntent(String link, Context context) {
Intent intent = new Intent(context, DeepLinkingActivity.class);
intent.putExtra(PASSED_LINK, link);
return intent;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
String host;
String link = getIntent().getStringExtra(PASSED_LINK);
if(TextUtils.isEmpty(link)) {
Intent intent = getIntent();
if (intent.getData() != null) {
Uri data = intent.getData();
host = data.getHost();
} else {
// No links
}
} else {
Uri data = Uri.parse(link);
host = data.getHost();
}
if(host.equals(GAME_LINK)) {
// myapp://game/
// Do something
} else if(host.equals(VIDEO_LINK)){
// myapp://video/
// Do something
} else {
// Do something
}
...
}
然后,您可以从小部件中调用:
widget.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(DeepLinkingActivity.createIntent("linik_for_this_wiget"), getContext());
}
});
如果您在WebView中有链接,则也可以覆盖 shouldOverrideUrlLoading
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句