所以我想创建一个 Android 应用程序,以便它可以在 android 操作系统的某个地方注册(或者只是在系统启动时启动),并且当手机用户点击网络浏览器内网页上的特殊按钮时:
<a href="myapp://mysettings">Foo</a>
我的应用会弹出并使用该 URL 中发送的参数运行。
那么我该怎么做呢?
我需要一个代码教程!
最佳答案
首先,为了能够从浏览器/邮件中自定义方案“myapp”的链接启动您的应用, 如下设置 Intent 过滤器。
<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="myapp"/>
</intent-filter>
并解析链接中的查询 myapp://someaction/?var=str&varr=string
(代码过于简化,没有错误检查。)
Intent intent = getIntent();
// check if this intent is started via custom scheme link
if (Intent.ACTION_VIEW.equals(intent.getAction())) {
Uri uri = intent.getData();
// may be some test here with your custom uri
String var = uri.getQueryParameter("var"); // "str" is set
String varr = uri.getQueryParameter("varr"); // "string" is set
}
[编辑] 如果您使用自定义方案来启动您的应用程序,问题之一是: 其他应用中的 WebView 可能无法理解您的自定义方案。 这可能会导致那些浏览器显示 404 页面以获取带有自定义方案的链接。
关于java - 如何注册一些 URL 命名空间 (myapp ://app. start/) 以通过在 Android OS 的浏览器中调用 URL 来访问您的程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2430045/