在自己的网站上托管一个.apk文件[英] Hosting an .apk file on over own site

本文是小编为大家收集整理的关于在自己的网站上托管一个.apk文件的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我需要在除了Android市场以外的一个公共站点上托管我的.apk文件或任何其他应用商店.在Android Market,在市场上注册后,Downloded .apk将在移动设备上自动安装,无需任何手动操作.所以我愿意创建一个网址并托管我的.apk文件,并希望下载该.apk进入android手机,它必须自动安装.

如何做到这一点.... PLZ分享如果任何代码或链接都会重新结束.

推荐答案

如果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);

其他推荐答案

{
        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);
}

本文地址:https://www.itbaoku.cn/post/102700.html

问题描述

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.

推荐答案

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);

其他推荐答案

{
        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);
}