Android 深度链接问题!如何使用自定义 URL 方案 myapp://some_data
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20193683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Deep Linking issue ! How to use Custom Url scheme myapp://some_data
提问by inderbagga
i have tried link1, link2,link3, link4, link5, link6
我试图链接1,链接2,LINK3,LINK4,link5,link6
Here's everything described about DeepLinking
这是关于DeepLinking的所有描述
What i want is the custom uri myapp://some_data, opens the native application installed in the device that requires some_data to initialise the application.
我想要的是自定义 uri myapp://some_data,打开安装在需要 some_data 来初始化应用程序的设备中的本机应用程序。
There are 2 scenarios in which the custom url can be clicked.
有两种情况可以单击自定义 url。
1) from within the SMS app, when user taps the link it should automatically open the installed otherwise open the googleplay store where the app is hosted
1) 在 SMS 应用程序中,当用户点击链接时,它应自动打开已安装的应用程序,否则打开托管应用程序的 googleplay 商店
2) from within the body of a email message.
2) 来自电子邮件的正文。
I have tried all the above listed links, but none of them works for me. I m having major problem with the scheme part.
我已经尝试了上面列出的所有链接,但没有一个对我有用。我在计划部分遇到了重大问题。
Here's my AndroidManifest.xml
这是我的 AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="MainActivity"
android:label="@string/app_name"
android:exported="true" >
<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="inderbagga" />
</intent-filter>
</activity>
</application>
and here's the MainActivity.java
这是 MainActivity.java
TextView tvText=(TextView)findViewById(R.id.tvid);
if (getIntent().getAction() == Intent.ACTION_VIEW&&getIntent().getScheme().equals("inderbagga")) {
Toast.makeText(getApplicationContext(), ""+getIntent().getScheme(), Toast.LENGTH_SHORT).show();
Uri uri = getIntent().getData();
// do stuff with uri
tvText.setText(uri.toString());
}
else tvText.setText("NULL");
To be more specific, i want to open the native application when u url of type inderbagga://a1b22c333 is clicked, Either from sms application or gmail/yahoomail email message body.
更具体地说,我想在单击 inderbagga://a1b22c333 类型的 url 时打开本机应用程序,无论是来自短信应用程序还是 gmail/yahoomail 电子邮件正文。
in order to achieve the same, i 've used intent filters to set the scheme. and getIntent() to read the data that equals to a1b22c333 in the MainActivity.
为了达到同样的目的,我使用了意图过滤器来设置方案。并 getIntent() 读取 MainActivity 中等于 a1b22c333 的数据。
回答by prakash
click link means this code will work
单击链接意味着此代码将起作用
<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:scheme="http"
android:host="domain.com(google.com)"android:pathPrefix="/wp/poc1/(sufexes)" />
</intent-filter>
get url data
获取网址数据
//get uri data
Uri data = getIntent().getData();
//get schma
String scheme = data.getScheme(); // "http"
//get server name
String host = data.getHost(); // Ipaddress or domain name
//get parameter
String urltextboxname=data.getQueryParameter("name");
//set value in textview
name.setText(urltextboxname);
回答by CommonsWare
To be more specific, i want to open the native application when u url of type inderbagga://a1b22c333 is clicked, Either from sms application or gmail/yahoomail email message body.
更具体地说,我想在单击 inderbagga://a1b22c333 类型的 url 时打开本机应用程序,无论是来自短信应用程序还是 gmail/yahoomail 电子邮件正文。
There are many SMS and email applications available for Android. Precisely none of them know to convert inderbagga://a1b22c333
into clickable entries. You could build a list of all of those apps, contact each of their development teams, and ask them to add it.
有许多适用于 Android 的 SMS 和电子邮件应用程序。确切地说,他们中没有人知道将其转换inderbagga://a1b22c333
为可点击的条目。您可以构建所有这些应用程序的列表,联系他们的每个开发团队,并要求他们添加它。
Or, you could have your app watch for a particular http://
URL instead. While the user would be presented with a chooser, to view that URL within your app or a Web browser, at least it will be clickable.
或者,您可以让您的应用程序监视特定的http://
URL。虽然用户会看到一个选择器,但要在您的应用程序或 Web 浏览器中查看该 URL,至少它是可点击的。
回答by Christian
You might also want to try out this library which facilitates declaring deep links and extracting out the parameters you need:
您可能还想尝试这个库,它有助于声明深层链接并提取您需要的参数:
https://github.com/airbnb/DeepLinkDispatch
https://github.com/airbnb/DeepLinkDispatch
It allows you to declare the URI you're interested in and the parameter you'd like to extract through annotations, without having to do the parsing yourself.
它允许您声明您感兴趣的 URI 和您想要通过注释提取的参数,而无需自己进行解析。
回答by Alex Austin
It looks like the problem you are having is that your email and SMS client are not parsing the URI scheme and path correctly. Most do not allow you to enter in a URI scheme directly. Additionally, if the app is not installed, you need to fallback to the Play Store so the user is given a good experience.
看起来您遇到的问题是您的电子邮件和 SMS 客户端没有正确解析 URI 方案和路径。大多数不允许您直接输入 URI 方案。此外,如果未安装该应用程序,您需要回退到 Play 商店,以便为用户提供良好的体验。
In order to do this, you need to call the URI scheme in client side JS. You can setup a simple web host and use this script:
为此,您需要在客户端 JS 中调用 URI 方案。您可以设置一个简单的网络主机并使用此脚本:
<script type="text/javascript">
window.onload = function() {
var method = 'iframe';
var fallbackFunction = function() {
if (method == 'iframe') {
window.location = "market://details?id=your.package.name";
}
};
var addIFrame = function() {
var iframe = document.createElement("iframe");
iframe.style.border = "none";
iframe.style.width = "1px";
iframe.style.height = "1px";
iframe.src = "inderbagga://a1b22c333";
document.body.appendChild(iframe);
};
var loadChromeIntent = function() {
method = 'intent';
document.location = "intent://a1b22c333#Intent;scheme= inderbagga;package=your.package.name;end";
};
if (navigator.userAgent.match(/Chrome/) && !navigator.userAgent.match("Version/")) {
loadChromeIntent();
}
else if (navigator.userAgent.match(/Firefox/)) {
window.location = "inderbagga://a1b22c333";
}
else {
addIFrame();
}
setTimeout(fallbackFunction, 750);
};
</script>
Or, you can use a service like branch.iowhich automatically assembles this client side JS for you in addition to working on desktop and iOS as well.