如何导出活动以便其他应用程序可以调用它?
我搜索了很多,但没有找到如何导出 Activity 的准确答案,因此应用程序可以使用 startActivityforResult
启动它。
我该如何实现这一目标?我是否必须在某些方面更改清单?
Well I searched a lot, but I didn't find a precise answer how to export an Activity, so an app can start it with startActivityforResult
.
How do I achieve that? Do I have to change the Manifest in some ways?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
作为 Dalmas 答案的替代方案,您实际上可以导出
Activity
而无需创建
(以及提出自定义操作的麻烦) 。在
Manifest
中编辑您的Activity
标记,如下所示:重要的部分是
android:exported="true"
,此export
标记确定“该活动是否可以由以下组件发起:其他应用程序”。如果您的
包含
那么此标签会自动设置为true
,如果不包含,则它会自动设置为true
默认设置为false
。然后要启动
Activity
,请执行以下操作:当然,使用此方法,您需要知道您尝试启动的 Activity 的确切名称。
As an alternate to Dalmas' answer, you can actually export an
Activity
without creating an<intent-filter>
(along with the hassle of coming up with a custom action).In the
Manifest
edit yourActivity
tag like so:The important part is
android:exported="true"
, thisexport
tag determines "whether or not the activity can be launched by components of other applications". If your<activity>
contains an<intent-filter>
then this tag is set totrue
automatically, if it does not then it is set tofalse
by default.Then to launch the
Activity
do this:Of course with this method you will need to know the exact name of the Activity you are trying to launch.
您需要在清单中声明一个意图过滤器(我从条形码扫描仪中获取了以下示例):
然后使用相同的操作字符串创建一个意图:
Android应该启动您的活动(或者如果有的话它将显示一个下拉框多个应用程序共享相同的操作字符串)。
You need to declare an intent-filter in your Manifest (I took the following example from Barcode Scanner) :
Then create an intent with the same action string :
Android should start your activity (or it will show a drop-down box if there are multiple apps sharing the same action string).