简体   繁体   English

从链接下载并安装apk

[英]Download And Install apk from a link

I`am trying to download and install an apk from some link, 我试图从某个链接下载并安装apk,

but for some reason i get an exception. 但由于某种原因,我得到一个例外。

I have one method downloadfile() which downloading the file and a call 我有一个方法downloadfile()下载文件和一个调用

to and installFile() method, which supposed to install it in the device. to和installFile()方法,它应该在设备中安装它。

some code: 一些代码:

public void downloadFile()
{
    String fileName = "someApplication.apk";
    MsgProxyLogger.debug(TAG, "TAG:Starting to download");
    try
    {

        URL u = new URL(
                "http://10.122.233.22/test/someApplication.apk");

        try
        {
            HttpURLConnection c = (HttpURLConnection) u.openConnection();

            try
            {
                c.setRequestMethod("GET");
                c.setDoOutput(true);

                try
                {
                    c.connect();


                    FileOutputStream f = context.openFileOutput(fileName,
                            context.MODE_WORLD_READABLE);

                    try
                    {
                        InputStream in = c.getInputStream();

                        byte[] buffer = new byte[1024];
                        int len1 = 0;
                        int totsize = 0;
                        try
                        {
                            while ((len1 = in.read(buffer)) > 0)
                            {
                                totsize += len1;
                                f.write(buffer, 0, len1);// .write(buffer);
                            }
                        } catch (IOException e)
                        {
                            e.printStackTrace();
                        }
                        f.close();
                        MsgProxyLogger.debug(TAG, TAG
                                + ":Saved file with name: " + fileName);

                                    InstallFile(fileName);


                    } catch (IOException e)
                    {
                        e.printStackTrace();
                    }

                } catch (IOException e)
                {
                    e.printStackTrace();
                }

            } catch (ProtocolException e)
            {
                e.printStackTrace();
            }
        } catch (IOException e)
        {
            e.printStackTrace();
        }

    } catch (MalformedURLException e)
    {
        e.printStackTrace();
    }
}

and this is the install file method: 这是安装文件方法:

 private void InstallFile(String fileName)
{
    MsgProxyLogger.debug(TAG, TAG + ":Installing file  " + fileName);

    String src = String.format(
            "file:///data/data/com.test/files/",
            fileName);

    Uri mPackageURI = Uri.parse(src);
    PackageManager pm = context.getPackageManager();

    int installFlags = 0;
    try
    {
        PackageInfo pi = pm.getPackageInfo("com.mirs.agentcore.msgproxy",
                PackageManager.GET_UNINSTALLED_PACKAGES);
        if (pi != null)
        {
            MsgProxyLogger.debug(TAG, TAG + ":replacing  " + fileName);

            installFlags |= PackageManager.REPLACE_EXISTING_PACKAGE;
        }
    } catch (NameNotFoundException e)
    {
    }

    try
    {
        // PackageInstallObserver observer = new PackageInstallObserver();
        pm.installPackage(mPackageURI);
    } catch (SecurityException e)
    {
                       //!!!!!!!!!!!!!here i get an security exception!!!!!!!!!!!
        MsgProxyLogger.debug(TAG, TAG + ":not permission?  " + fileName);
    }

this is the exception details: "Neither user 10057 nor current process has android.permission.INSTALL_PACKAGES". 这是异常细节:“用户10057和当前进程都没有android.permission.INSTALL_PACKAGES”。

and i have set in my main app that permission in the manifest. 我已经在我的主应用程序中设置了清单中的权限。

anyone has any idea? 有谁有任何想法?

thanks, 谢谢,

ray. 射线。

You cannot install APKs that way -- only applications that are part of the system firmware can do that. 您不能以这种方式安装APK - 只有属于系统固件的应用程序才能这样做。

You should be able to use an ACTION_VIEW Intent , with a MIME type of application/vnd.android.package-archive and a Uri pointing to your file. 您应该能够使用ACTION_VIEW Intent ,MIME类型为application/vnd.android.package-archiveUri指向您的文件。 Note that this may not work on devices that do not have "allow non-Market installs" checked. 请注意,这可能不适用于未选中“允许非市场安装”的设备。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM