Android,如何在我的应用程序中使用外部 APK?

发布于 2024-12-16 01:58:52 字数 1125 浏览 3 评论 0原文

我创建了两个项目。第一个,提取手机信息并显示在屏幕上。我有这个项目的 .apk 文件(例如 test.apk)。

然后我创建了第二个项目。这个屏幕上有一个按钮,我希望当我单击该按钮时,第一个项目运行(显示手机信息)。我通过右键单击项目根目录>构建路径>配置构建路径>添加外部 JAR>test.apk 将 test.apk 添加到该项目中

然后在代码中,我使用意图调用它。这是我的代码:

btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("com.in.test.MainActivity"); 
                startActivity(intent);
            }
        });

但是,当我运行应用程序时,根据 logcat,我看到以下错误: 11-18 10:09:28.933:E/AndroidRuntime(2237):未捕获的处理程序:由于未捕获的异常,线程主线程退出 11-18 10:09:29.022: E/AndroidRuntime(2237): android.content.ActivityNotFoundException: 没有找到处理 Intent { act=com.in.test.MainActivity } 的活动 11-18 10:09:29.022: E/AndroidRuntime(2237): 在 android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1484) 。 。 。

我的问题是什么以及出在哪里? 感谢

更新: 在清单文件中,我在 application 元素内添加了这一行:

<activity
            android:name="com.in.test.MainActivity" />

但结果仍然相同。我会尝试遵循您的建议(使用意图过滤器)。

I have created two projects. The first one, extracts information of handset and shows on the screen. I have .apk file of this project (for example test.apk).

Then I created second project. This one has a button on the screen and I want when I click the button, first project runs (shows information of handset). I have added test.apk into this project by right clicking on root of project>Build Path>Configure Build Path>Add External JARs>test.apk

Then in the code, I called this by using intent. this is my code:

btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("com.in.test.MainActivity"); 
                startActivity(intent);
            }
        });

however, when I run the application, according to logcat, I see following error:
11-18 10:09:28.933: E/AndroidRuntime(2237): Uncaught handler: thread main exiting due to uncaught exception
11-18 10:09:29.022: E/AndroidRuntime(2237): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.in.test.MainActivity }
11-18 10:09:29.022: E/AndroidRuntime(2237): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1484)

.
.
.

What and where is my problem?
Thanks

update:
In the manifest file I added this line inside of application element:

<activity
            android:name="com.in.test.MainActivity" />

but the result is still the same. I'll try to follow your suggestion (using intent filter).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

过去的过去 2024-12-23 01:58:52

您无法从 Eclipse 执行此操作。需要安装 test.apk,它需要导出您需要的 Activity 并为其设置一个 Intent 过滤器。例如:

 <intent-filter>
    <action android:name="com.infindo.test.MainActivity" />
    <category android:name="android.intent.category.DEFAULT" />
 </intent-filter>

如果您有权访问 test.apk 的源代码,请修改它。如果没有,您只能使用它导出的活动/意图。

You can't do this from Eclipse. test.apk needs to be installed, and it needs to export the activity you need and have an intent filter for it. Something like:

 <intent-filter>
    <action android:name="com.infindo.test.MainActivity" />
    <category android:name="android.intent.category.DEFAULT" />
 </intent-filter>

If you have access to test.apk's source, modify it. If not, you can only use the activities/intents it exports.

红衣飘飘貌似仙 2024-12-23 01:58:52

要像您一样以明确的意图启动活动(通过在构造函数中使用活动类),必须在应用程序的清单中声明该活动,并且必须将其编译到您的应用程序中。您不能为此使用单独的 apk 文件。

执行您想要的操作的方法是为 test.apk 中的活动声明一个意图过滤器,并使用适当的意图启动它。请参阅指南主题意图和意图过滤器以及相关文档Intent 类了解有关如何执行此操作的更多信息。

To start an activity with an explicit intent like you are doing (by using the activity class in the constructor), the activity must be declared in the manifest for your app and must be complied into your app. You cannot use a separate apk file for that.

The way to do what you want is to declare an intent filter for the activity in test.apk and to launch it using an appropriate intent. See the guide topic Intents and Intent Filters and the documentation for the Intent class for more information about how to do this.

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