从url安装apk

时间:2011-05-10 15:29:01

标签: android

我尝试从URl安装apk,这是我的代码:

 Intent promptInstall = new Intent(android.content.Intent.ACTION_VIEW);
    promptInstall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    promptInstall.setDataAndType(Uri.parse("http://10.0.2.2:8081/MyAPPStore/apk/Teflouki.apk"), "application/vnd.android.package-archive" );

       startActivity(promptInstall);

但我有这个问题:

   05-10 15:09:29.511: ERROR/AndroidRuntime(1668): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=http://10.0.2.2:8081/MyAPPStore/apk/Teflouki.apk typ=application/vnd.android.package-archive flg=0x10000000 }

提前致谢

4 个答案:

答案 0 :(得分:4)

您应该在安装之前将xxx.apk下载到存储中:

Intent promptInstall = new Intent(android.content.Intent.ACTION_VIEW);
promptInstall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
promptInstall.setDataAndType(Uri.parse("storage/xxx.apk"), "application/vnd.android.package-archive" );
startActivity(promptInstall);

答案 1 :(得分:2)

如果应用程序在mearketplace上不可用,这将无济于事,但如果是:

Uri marketUri = Uri.parse("market://search?q=pname:com.appmaker.tefloukipackage");
                                        Intent marketIntent = new Intent(Intent.ACTION_VIEW, marketUri);
                                        try {
                                            context.startActivity(marketIntent);
                                        } catch (ActivityNotFoundException ex) {
                                            showAlertDialog(context, "Error", "Could not launch the market application.", true, null);
                                        }

答案 2 :(得分:0)

先试试这里: Android: install .apk programmatically

首先下载apk然后安装它可能是个更好的主意。

答案 3 :(得分:0)

继续。

在模块清单中,添加

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

//Inside application block
<application>
    <provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_path"/>
    </provider>
</application>

在模块的res / xml文件夹中,如果未创建,则使用文件provider_path.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>

并使用此方法。

private fun updateApplication(activity: Activity) {
    //This will get you the root directory path
    val externalStoragePublicDirectory: String =
        Environment.getExternalStorageDirectory().path

    val externalStoragePublicDirectoryFile =
        File(externalStoragePublicDirectory, "MyApp" + ".apk")

    val uri = FileProvider.getUriForFile(
        activity.applicationContext,
        activity.applicationContext.packageName + ".provider",
        externalStoragePublicDirectoryFile
    )

    val installAppIntent = Intent(Intent.ACTION_VIEW)
        .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
        .setDataAndType(
            uri,
            "application/vnd.android.package-archive"
        )
        .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
        .putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true)
    activity.startActivity(installAppIntent)
    //This will close your app, remove if not needed
    exitProcess(0)
}

重要,还转到您的手机设置,搜索未知来源,在旧设备中启用它,但在新设备中,搜索您的应用并允许其安装新应用包。只有这样,您才能弹出弹出窗口以安装该应用程序。