Download, install and delete the .apk file on your Android device programmatically from a website other than the market


I developed some Android games and created an .apk file.
I put these .apk files on my website (say: http://www.sush19.com/androidApp/apk/myGame1.apk ) Can I directly install this game when this URL visits another onClick() event.

I do not want the user to download .apk according to their SD card, and then install it manually, infact games must be installed directly on the device.

I tried to use the code below in my onClick() application:

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

I know that the code above is incorrect ... but can someone comment on this.

+6
source share
2 answers

The code below allows the user to download, install and remove the .apk file on an Android device. I created an Android application (Say App1) that downloads other Android applications on an SD card. Click on the button in App1, it will download the .apk file from my own website to Background, after the download is complete, it will ask the user to install the application recently downloaded from App1, and after the installation is complete, the downloaded .apk file will be deleted from the SD card.

In my main activity App1: I turned on the button
In my case, I launch my other applications from App1, if they are not installed on the device, I download it from my site and install 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 Class

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

To delete downloaded file from SD card I've used BroadcastReceiver class

 @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();} } 

Don't forget to include following permission in the 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" /> 

In my website, I create two .aspx pages and placed it inside Android folder and .apk files inside Android/Games folder in Visual Studio
First page: 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"); } } 

Second Page: 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(); } } 

I added following code in Web.config file

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

I hope this information will be useful to some people.

+14
source

Who supports the server for your apk, because for this you need to perform some server settings: set the MIME type of your folder on the server containing this apk file, like .apk and application/vnd.android.package-archive . Then it is automatically installed when you click on the link like any other application from the Google game.

0
source

Source: https://habr.com/ru/post/958365/


All Articles