如何从我的应用程序调用 apk

发布于 2024-10-01 14:30:42 字数 502 浏览 2 评论 0原文

我正在尝试将开源游戏添加到我的应用程序中。我的意思是,我开发了一个应用程序。当用户安装我的应用程序时,应用程序中会有一个按钮,当用户点击该按钮时,内部 apk(开源游戏)将启动。

但是,我不知道应该将游戏 apk 文件放在哪里?我不知道如何调用游戏apk?

我发现以下代码可以从 sdcard 启动任何 apk(我猜)。

String fileName = Environment.getExternalStorageDirectory() + "/myApp.apk";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(fileName)), "application/vnd.android.package-archive");
startActivity(intent);

但是,我计划准备一个 APK(并且游戏将放置在 res/raw 等位置)。

I am trying to add an open source game to my application. I mean, I developed an application. When a user installs my application, there will be button in the app and when user taps the button, internal apk (open source game) will be started.

But, I dont know where I should put the game apk file to? I dont know how I can invoke the game apk?

I found following code which starts any apk from sdcard (I guess).

String fileName = Environment.getExternalStorageDirectory() + "/myApp.apk";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(fileName)), "application/vnd.android.package-archive");
startActivity(intent);

But, I plan to prepare one APK (and game will be put somewhere such res/raw).

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

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

发布评论

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

评论(1

月下客 2024-10-08 14:30:42

<块引用>

应用程序中会有一个按钮,当用户点击该按钮时,内部 apk(开源游戏)将启动。

如果您的意思是应用程序(内部 apk)将启动,那么您不明白它是如何工作的。让我解释一下:APK 与 Windows 系统上的 .EXE 不同。我的意思是,你不能只是“运行”APK。

APK 是一个包含应用程序的包,您唯一能做的就是安装它。您上面发布的代码似乎调用了 android 应用程序安装程序,这将执行以下操作:安装应用程序(而不是运行它)。正如您所看到的,应用程序必须位于外部存储(SD 卡)中,这是因为应用程序安装程序无法直接访问您的 /res/raw。

所以,你要做的就是将 APK 从 res/raw 复制到 SDCard(Google 是你的朋友),然后使用上面的代码。同样,这不会执行该应用程序,它只会安装它。安装后,您可以从您的应用程序执行它,执行一些操作:

Intent intent = new Intent();
intent.setClassName("com.android.bla", "com.android.bla.YourActivity");
startActivity(intent);

there will be button in the app and when user taps the button, internal apk (open source game) will be started.

If you mean, that the application (the internal apk) will be started, then you don't understand how that works. So let me explain: an APK is not like an .EXE on a Windows system. I mean, you cannot just "run" an APK.

An APK is a package which contains an application, and the only thing you can do with it is install it. The code you post above seems to invoke the android application installer, which would do that: install the app (not run it). As you can see, it's necessary that the app is in the external storage (SD card), and that's because the app installer cannot access your /res/raw directly.

So, what you have to do is copy the APK from res/raw to the SDCard (Google is your friend), and then use the code above. Again, that won't execute the app, it will just install it. Once it's installed, you can execute it from your app doing something lile:

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