下载,安装和删除对Android设备的.apk文件编程方式从一个网站比市场上其他 [英] Download, Installing and Delete .apk file on android device programmatically from a website other than marketplace

查看:120
本文介绍了下载,安装和删除对Android设备的.apk文件编程方式从一个网站比市场上其他的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我已经开发了一些安卓游戏,创造的.apk文件..
我已经把这些.apk文件的文件在我的网站(例如: HTTP://www.sush19 .COM / androidApp / APK / myGame1.apk ) 是否有可能直接安装这个游戏的时候,从另一个应用程序这个网址访问的onClick()事件。


I've developed some android games and created .apk file..
I've put these .apk files on my website (say: http://www.sush19.com/androidApp/apk/myGame1.apk) Is it possible to directly install this game when this url visit from another app onClick() event.

我不希望用户能够精细下载apk文件到他们的SD卡,然后手动安装,事实上本场比赛应该被直接安装到设备上。

I don't want user to download .apk fine to their sdcard and then install manually, infact the game should be installed directly to the device.

我试图跌破code。在我的另一个应用程序的onClick()事件:

I was trying below code in my another app onClick() event:

Intent goToMarket = new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://www.sush19.com/androidApp/apk/myGame1.apk"));
startActivity(goToMarket);

我知道上面code是不正确的。但可以在这个人的评论。

I know the above code is not correct.. but can anyone comment on this..

推荐答案

以下code,允许用户下载,安装和删除对Android设备的.apk文件。 我创建了一个Android应用程序(说App1的),它会下载到SD卡上的其他Android应用程序。在按钮单击App1的,它会下载从我自己的网站上背景的.apk文件,在下载完成会提示用户安装近日从App1的,并在安装后下载的应用程序完成下载的.apk文件将从中删除SD卡。

The below code, allow user to Download, Install and Delete .apk file on Android device. I've created an Android app (Say App1), which downloads other android apps on SD card. On Button click in App1, it will download the .apk file from my own website on Background, on download complete it will prompt user to install the app downloaded recently from App1 and after the installation is completed the downloaded .apk file will be deleted from the SD card.

在我App1的主要活动:我已经包含按钮
就我而言,我启动我从App1的其他应用程序,如果没有安装在设备上,我从我的网站下载并安装它。
按钮单击事件的方法

In my App1 main activity: I've included button
In my case I'm launching my other applications from App1, if not installed on the device, I'm downloading it from my website and installing it.
button click event method

public OnClickListener ButtonClicked = new OnClickListener() {
        public void onClick(View v) {
            Intent i;
            PackageManager manager = getPackageManager();
            try {
                i = manager.getLaunchIntentForPackage("com.mycompany.mygame");
                if (i == null)
                    throw new PackageManager.NameNotFoundException();
                i.addCategory(Intent.CATEGORY_LAUNCHER);
                startActivity(i);
            } catch (PackageManager.NameNotFoundException e) {
                InstallAPK downloadAndInstall = new InstallAPK();
                progress.setCancelable(false);
                progress.setMessage("Downloading...");
                downloadAndInstall.setContext(getApplicationContext(), progress);
                downloadAndInstall.execute("http://xyz/android/gamedownload.aspx?name=mygame.apk");
            }
        }
    };

InstallAPK类

public class InstallAPK extends AsyncTask<String,Void,Void> {

    ProgressDialog progressDialog;
    int status = 0;

    private Context context;
    public void setContext(Context context, ProgressDialog progress){
        this.context = context;
        this.progressDialog = progress;
    }

    public void onPreExecute() {
        progressDialog.show();
    }

    @Override
    protected Void doInBackground(String... arg0) {
        try {
            URL url = new URL(arg0[0]);
            HttpURLConnection c = (HttpURLConnection) url.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();

            File sdcard = Environment.getExternalStorageDirectory();
            File myDir = new File(sdcard,"Android/data/com.mycompany.android.games/temp");
            myDir.mkdirs();
            File outputFile = new File(myDir, "temp.apk");
            if(outputFile.exists()){
                outputFile.delete();
            }
            FileOutputStream fos = new FileOutputStream(outputFile);

            InputStream is = c.getInputStream();

            byte[] buffer = new byte[1024];
            int len1 = 0;
            while ((len1 = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len1);
            }
            fos.flush();
            fos.close();
            is.close();

            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(new File(sdcard,"Android/data/com.mycompany.android.games/temp/temp.apk")), "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // without this flag android returned a intent error!
            context.startActivity(intent);


        } catch (FileNotFoundException fnfe) {
            status = 1;
            Log.e("File", "FileNotFoundException! " + fnfe);
        }

        catch(Exception e)
        {
            Log.e("UpdateAPP", "Exception " + e);
        }
        return null;
    }

    public void onPostExecute(Void unused) {
        progressDialog.dismiss();
        if(status == 1)
            Toast.makeText(context,"Game Not Available",Toast.LENGTH_LONG).show();
    }
}

要删除我用的BroadcastReceiver类的SD卡下载的文件

@Override
    public void onReceive(Context context, Intent intent) { 

        try
        {
            String packageName = intent.getData().toString() + getApplicationName(context, intent.getData().toString(), PackageManager.GET_UNINSTALLED_PACKAGES);

            if(intent.getAction().equals("android.intent.action.PACKAGE_ADDED")){
                File sdcard = Environment.getExternalStorageDirectory();
                File file = new File(sdcard,"Android/data/com.mycompany.android.games/temp/temp.apk");
                file.delete();
            }
        }catch(Exception e){Toast.makeText(context, "onReceive()", Toast.LENGTH_LONG).show();}
    }

不要忘了,包括以下在AndroidManifest.xml许可

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

在我的网站,我创建两个.aspx页,并把它放在Android的文件夹,.apk文件的文件里面的Andr​​oid /游戏在Visual Studio文件夹内
第一页:marketplace.aspx.cs

public partial class marketplace : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DirectoryInfo directory = new DirectoryInfo(Server.MapPath("~/Android/Games"));
            int counter = 0;
            foreach (FileInfo file in directory.GetFiles())
            {
                HyperLink link = new HyperLink();
                link.ID = "Link" + counter++;
                link.Text = file.Name;
                link.NavigateUrl = "gamedownload.aspx?name=" + file.Name;

                Page.Controls.Add(link);
                Page.Controls.Add(new LiteralControl("<br/>"));

            }
        }

        protected void Click(object sender, EventArgs e)
        {
            Response.Redirect("gamedownload.aspx");
        }
    }

第二页:gamedownload.aspx.cs

public partial class gamedownload : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string fileName = Request.QueryString["name"].ToString();
            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
            Response.TransmitFile(Server.MapPath("~/Android/Games/" + fileName));
            Response.End();
        }
    }

我说下code。在Web.config文件

<system.webServer>
    <staticContent>
      <mimeMap fileExtension=".apk"
               mimeType="application/vnd.android.package-archive" />
    </staticContent>
  </system.webServer>

我希望这个信息将帮助全为一些人。

I hope this information will be help full for some people.

这篇关于下载,安装和删除对Android设备的.apk文件编程方式从一个网站比市场上其他的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆