【问题标题】:How to install APK automatically when the file download is complete (hosted on private)文件下载完成后如何自动安装APK(私有托管)
【发布时间】:2011-09-16 02:57:18
【问题描述】:

我是 android 新手,最近两天我一直在网上搜索这个。我也找到了以下链接,但我不知道如何以及在何处实现此代码以在完全下载后开始自动安装 apk 文件以及如何在安装后删除下载的 apk 文件。请指导我正确的方式帮助我。

how to install apk file programmatically

Invoking activity from APK in another android application

Android install apk programmatically

Install APK programmatically on android

http://www.anddev.org/viewtopic.php?p=23928

编辑:

我已经在清单文件中编写了这段代码:

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.WRITE_CONTACTS" />
    <!-- <uses-permission android:name="android.permission.INSTALL_PACKAGES"/> -->
    <application android:icon="@drawable/biz_logo"
        android:permission="android.permission.WRITE_CONTACTS">
        <activity android:name="com.biz.mlm.Main">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="content" />
                <data android:scheme="file" />
                <data android:mimeType="application/vnd.android.package-archive" />
            </intent-filter>
        </activity>

【问题讨论】:

  • 不清楚你想做什么。您想在您的应用程序中下载 APK 并安装它吗?或者你想让手机检测到下载的APK然后安装它们...
  • 我想让手机检测到下载的apk然后安装它们请帮我看看怎么做

标签: android installation download


【解决方案1】:

不可能。正如其中一些链接所指出的那样,制作一个自动安装并删除原始安装程序而无需任何用户进一步干预的 APK 称为恶意软件。

如果用户授予它相关权限,您可以编写一个程序来下载和安装任意 APK,但我还没有看到一个好的实现或文档。通常,您可以使用如下代码调用默认系统安装程序:

File apkFile = new File({path to APK});
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
    startActivity(intent);

【讨论】:

  • 您好 fermi,感谢您的回答,但我不知道在哪里编写此代码?我应该在启动器活动的 .java 文件中写入还是为它创建一个新的 java 类。如果我创建一个新的 java 类,那么我应该如何调用该类以及在哪里调用?我需要添加什么权限?
  • 这给了我这个错误,我不知道为什么。有人可以帮助我。android.os.FileUriExposedException: file:///storage/emulated/0/k5bbsz99d4gbzc0h.apk 通过 Intent 暴露在应用程序之外.getData()
  • 可能是版本问题:看看stackoverflow.com/questions/38200282/…中的一些选项
  • 用这个 Intent.ACTION_INSTALL_PACKAGE 替换 Intent.ACTION_VIEW
  • 作为一个开发者,每次需要更新正在开发的应用的安装时,点击允许安装包真的很烦人。
【解决方案2】:

您是否尝试过添加以下权限?

<uses-permission
    android:name="android.permission.INSTALL_PACKAGES" />

将上述代码添加到您的清单中(尤其是当您遇到安全异常时)。

可以参考Install apps silently, with granted INSTALL_PACKAGES permission

希望这对你有用。

【讨论】:

  • 它说权限只会授予系统应用程序!!?
  • 这意味着它不能再用于非系统应用程序。
  • 一个好奇的问题,packigizer怎么知道一个特定的应用程序不是系统应用程序?
【解决方案3】:
Intent intent = new Intent(Intent.ACTION_VIEW);
File file = new File("/mnt/sdcard/myapkfile.apk");
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
startActivity(intent);

以上代码将安装 myapkfile.apk。你只需要把这段代码放在Activity的onCreate()中即可。

【讨论】:

  • 假设,我已经下载了一个应用程序(我正在构建它)。而我,自动希望它在获得许可的情况下安装它。那么,上面的sn-p要放在哪个onCreate活动中呢?在应用程序中的任何活动,我的意思是 MainActivity?
【解决方案4】:

其实这是可以的,使用BroadcastReceiver在系统中添加包(PACKAGE_ADDED)时进行监听。创建 BroadcastReceiver 类,代码如下:

public void onReceive(Context arg0, Intent arg1) {
    File downLoadApk = new File(Environment.getExternalStorageDirectory(),"New.apk");
        if (downLoadApk.exists()) {
            downLoadApk.delete();
        }

}

【讨论】:

    【解决方案5】:

    下载后给出apk文件的路径,它会自动安装到android中

    意图intent1 = new Intent(Intent.ACTION_INSTALL_PACKAGE); intent1.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent1.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" +filename)), "application/vnd.android.package-archive"); intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-06
      • 2017-03-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多