【问题标题】:Two Android apps in one file一个文件中的两个 Android 应用程序
【发布时间】:2017-01-14 05:30:57
【问题描述】:
我想将两个应用程序“打包”在一个 Android APK 文件中。要求:
- 当我安装 APK 文件时,它会安装到单独的应用程序中。
- 如果我卸载一个应用程序,其他应用程序仍会保持安装状态。
我可以那样做吗?
【问题讨论】:
标签:
android
android-studio
apk
android-install-apk
【解决方案1】:
首先上传您想在 Dropbox 上安装的那些应用程序(仅限)。
现在获取这些 APK 文件的链接,并在链接中将 dropbox.com/..... 替换为 d.dropboxusercontent.com/...
现在,制作一个应用并将下面的代码放在“onCreate”或某处,
String destination = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/";
String fileName = "AppName.apk";
destination += FileName;
final Uri uri = Uri.parse("file://" + destination);
String URL = "d.dropboxusercontent.com/...............";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(URL));
request.setDescription(Main.this.getString(R.string.notification_description));
request.setTitle(Main.this.getString(R.string.app_name));
// Set destination
request.setDestinationUri(uri);
// Get download service and enqueue file
final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
final long downloadId = manager.enqueue(request);
// Set BroadcastReceiver to install app when .apk is downloaded
BroadcastReceiver onComplete = new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
Intent install = new Intent(Intent.ACTION_VIEW);
install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
install.setDataAndType(uri,
manager.getMimeTypeForDownloadedFile(downloadId));
startActivity(install);
unregisterReceiver(this);
finish();
}
};
// Register receiver for when .apk download is complete
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
如果您想安装多个应用程序,则可以通过更改变量名称多次应用此代码。
如果成功请评论。