如何导出活动以便其他应用程序可以调用它?

发布于 2024-12-16 12:45:00 字数 125 浏览 1 评论 0原文

我搜索了很多,但没有找到如何导出 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

去了角落 2024-12-23 12:45:00

作为 Dalmas 答案的替代方案,您实际上可以导出 Activity 而无需创建 (以及提出自定义操作的麻烦) 。

Manifest 中编辑您的 Activity 标记,如下所示:

<activity
    android:name=".SomeActivity"
    ....
    android:exported="true" />

重要的部分是 android:exported="true",此 export 标记确定“该活动是否可以由以下组件发起:其他应用程序”。如果您的 包含 那么此标签会自动设置为 true,如果不包含,则它会自动设置为 true默认设置为 false

然后要启动 Activity ,请执行以下操作:

Intent i = new Intent();
i.setComponent(new ComponentName("package name", "fully-qualified name of activity"));
startActivity(i);

当然,使用此方法,您需要知道您尝试启动的 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 your Activity tag like so:

<activity
    android:name=".SomeActivity"
    ....
    android:exported="true" />

The important part is android:exported="true", this export 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 to true automatically, if it does not then it is set to false by default.

Then to launch the Activity do this:

Intent i = new Intent();
i.setComponent(new ComponentName("package name", "fully-qualified name of activity"));
startActivity(i);

Of course with this method you will need to know the exact name of the Activity you are trying to launch.

数理化全能战士 2024-12-23 12:45:00

您需要在清单中声明一个意图过滤器(我从条形码扫描仪中获取了以下示例):

<activity android:name="...">
    <intent-filter>
        <action android:name="com.google.zxing.client.android.SCAN" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

然后使用相同的操作字符串创建一个意图:

Intent intent = new Intent("com.google.zxing.client.android.SCAN");
startActivityForResult(intent, code);

Android应该启动您的活动(或者如果有的话它将显示一个下拉框多个应用程序共享相同的操作字符串)。

You need to declare an intent-filter in your Manifest (I took the following example from Barcode Scanner) :

<activity android:name="...">
    <intent-filter>
        <action android:name="com.google.zxing.client.android.SCAN" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

Then create an intent with the same action string :

Intent intent = new Intent("com.google.zxing.client.android.SCAN");
startActivityForResult(intent, code);

Android should start your activity (or it will show a drop-down box if there are multiple apps sharing the same action string).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文