Gwi*_*llz 2 url linker android android-intent
我试图让Android应用程序选择器在注册电子邮件中单击特定URL时启动.我已经看过下面的问题和其他几个问题,但我仍然没有运气.
我已经在AndroidManifest.xml文件中创建了我的意图,如下所示(我已将我的网站地址放在您看到".website.org"的位置):
<activity android:name=".MainActivity" android:configChanges="orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<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="https"
android:host="*.website.org"
android:path="/" />
</intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)
有没有人能帮助我还有什么我可能会丢失,因为这是目前还没有推出任何东西,它只是直接加载在默认的Web浏览器的链接?
您可以通过应用中的深层链接实现此目的.
首先,您需要为传入链接添加意图过滤器.
<action>指定ACTION_VIEW意图操作,以便可以从Google搜索访问意图过滤器.
<data>添加一个或多个标记,每个标记表示解析为活动的URI格式.至少,标记必须包含android:scheme属性.
您可以添加更多属性以进一步优化活动接受的URI类型.例如,您可能有多个活动接受类似的URI,但这些活动仅根据路径名称而有所不同.在这种情况下,使用机器人:路径属性或其pathPattern或pathPrefix变体来区分系统应打开不同的URI的路径,其活性.
<category>包括BROWSABLE类别.为了从Web浏览器访问intent过滤器,需要它.没有它,单击浏览器中的链接无法解析为您的应用程序.
还包括DEFAULT类别.这允许您的应用响应隐式意图.如果没有这个,只有在intent指定您的应用程序组件名称时才能启动活动.
我已使用此网址启动我的应用" http://www.example.com/gizmos "
看看我的Manifest.xml文件,
<activity
android:name="com.example.android.GizmosActivity"
android:label="@string/title_gizmos" >
<intent-filter android:label="@string/filter_view_http_gizmos">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- Accepts URIs that begin with "http://www.example.com/gizmos” -->
<data android:scheme="http"
android:host="www.example.com"
android:pathPrefix="/gizmos" />
<!-- note that the leading "/" is required for pathPrefix-->
</intent-filter>
<intent-filter android:label="@string/filter_view_example_gizmos">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- Accepts URIs that begin with "example://gizmos” -->
<data android:scheme="example"
android:host="gizmos" />
</intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)
请注意,两个intent过滤器仅因<data>元素而异.
<intent-filter>
...
<data android:scheme="https" android:host="www.example.com" />
<data android:scheme="app" android:host="open.my.app" />
</intent-filter>
Run Code Online (Sandbox Code Playgroud)
它似乎只支持https://www.example.com和 app://open.my.app.但是,它实际上支持这两个,加上:app://www.example.com和https://open.my.app.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = getIntent();
String action = intent.getAction();
Uri data = intent.getData();
}
Run Code Online (Sandbox Code Playgroud)
使用adb测试意图过滤器URI的一般语法是:
$ adb shell am start
-W -a android.intent.action.VIEW
-d <URI> <PACKAGE>
Run Code Online (Sandbox Code Playgroud)
例如,以下命令尝试查看与指定URI关联的目标应用程序活动.
$ adb shell am start
-W -a android.intent.action.VIEW
-d "example://gizmos" com.example.android
Run Code Online (Sandbox Code Playgroud)
感谢您的回复。最后我不得不将我的意图更改为以下内容,并且得到了预期的结果。当我单击意图中指定的 URL 时,会弹出应用程序选择器,允许我在应用程序中加载链接。
正如为像我一样苦苦挣扎的人提供的信息一样,您不需要从传入意图中读取数据来启动应用程序选择器,您所需要的只是 AndroidManifest 文件中的以下内容。
<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="http" />
<data android:scheme="https" />
<data android:host=".website.com" />
</intent-filter>
Run Code Online (Sandbox Code Playgroud)
谢谢大家。
| 归档时间: |
|
| 查看次数: |
4075 次 |
| 最近记录: |