在自己的网站上托管 .apk 文件

发布于 11-12 04:45 字数 261 浏览 2 评论 0原文

我需要将我的 .apk 文件托管在 Android 市场或任何其他应用商店之外的一个公共网站上。在android市场中,注册市场后,下载的.apk将自动安装在手机上,无需任何手动操作。因此,我愿意创建一个 URL 并将我的 .apk 文件托管到其中,并希望将该 .apk 下载到 Android 手机中,并且它必须自动安装。

我该怎么做......请分享是否有任何代码或链接可以重新分级。

I got requirement to host my .apk file on one public site other than android market or any other app stores. In android market, after registred in to the market the downloded .apk will automatically installed on mobile without any manual action. So I am willing to create one URL and host my .apk file in to that and want to download that .apk in to the android mobile and it has to install automatically.

How can I do that....plz share if any code or links are there regrading this.

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

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

发布评论

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

评论(2

日暮斜阳2024-11-19 04:45:37

如果 Android 设备已选中“设置”->“应用程序”->“未知来源”,Android 将允许安装 .apk。如果不检查 - 你将不会成功。

假设复选框已选中并且您已下载 .apk 文件。您可以运行下一个代码来触发安装:

Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(new File(apkFileName));
intent.setDataAndType(uri, "application/vnd.android.package-archive");
startActivity(intent);

If Android device has Settings->Applications->Unknown Sources checked, Android will allow .apk installation. If its not checked - you will not succeed.

Assuming that check bar is checked and you have downloaded .apk file. You can run next code to trigger installation:

Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(new File(apkFileName));
intent.setDataAndType(uri, "application/vnd.android.package-archive");
startActivity(intent);
呢古2024-11-19 04:45:37
{
        String url = "http://www.server.com/yourapp.apk";
        String PATH = Environment.getExternalStorageDirectory() + "/download/";
        File file = new File(PATH);
        file.mkdirs();
        File outputFile = new File(file, "yourapp.apk");
        downloadFile(url, outputFile);
        installApp(context);
}



private static void downloadFile(String url, File outputFile) {
    try {
        URL u = new URL(url);
        URLConnection conn = u.openConnection();
        int contentLength = conn.getContentLength();

        DataInputStream stream = new DataInputStream(u.openStream());

        byte[] buffer = new byte[contentLength];
        stream.readFully(buffer);
        stream.close();

        DataOutputStream fos = new DataOutputStream(new FileOutputStream(outputFile));
        fos.write(buffer);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        Log.e("FileNotFoundException",e+"");
        return; 
    } catch (IOException e) {
        Log.e("IOException",e+"");
        return; 
    }
}


private static void installApp(Context mycontext) {
    Intent installer = new Intent();
    installer.setAction(android.content.Intent.ACTION_VIEW);
    String PATH = "file://" + Environment.getExternalStorageDirectory() + "/download/yourapp.apk";
    installer.setDataAndType(Uri.parse(PATH), "application/vnd.android.package-archive");
    mycontext.startActivity(installer);
}
{
        String url = "http://www.server.com/yourapp.apk";
        String PATH = Environment.getExternalStorageDirectory() + "/download/";
        File file = new File(PATH);
        file.mkdirs();
        File outputFile = new File(file, "yourapp.apk");
        downloadFile(url, outputFile);
        installApp(context);
}



private static void downloadFile(String url, File outputFile) {
    try {
        URL u = new URL(url);
        URLConnection conn = u.openConnection();
        int contentLength = conn.getContentLength();

        DataInputStream stream = new DataInputStream(u.openStream());

        byte[] buffer = new byte[contentLength];
        stream.readFully(buffer);
        stream.close();

        DataOutputStream fos = new DataOutputStream(new FileOutputStream(outputFile));
        fos.write(buffer);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        Log.e("FileNotFoundException",e+"");
        return; 
    } catch (IOException e) {
        Log.e("IOException",e+"");
        return; 
    }
}


private static void installApp(Context mycontext) {
    Intent installer = new Intent();
    installer.setAction(android.content.Intent.ACTION_VIEW);
    String PATH = "file://" + Environment.getExternalStorageDirectory() + "/download/yourapp.apk";
    installer.setDataAndType(Uri.parse(PATH), "application/vnd.android.package-archive");
    mycontext.startActivity(installer);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文