Aswan
Aswan

Reputation: 5135

install apk from remote server

when user click on url link i want to install apk file from server.

please guide me how to do i searched on net i am not getting corresponding info

Thanks in advance

Aswan

Upvotes: 0

Views: 12576

Answers (3)

DagW
DagW

Reputation: 955

This is the code i use, it is not for a webview but you could easily override url loading and apply this code anyways.. The intent at the bottom is the answer to your question.

/**
 * Main
 * When started, will download latest version of AN APPLICATIONand launch an install
 * Is just a dialog
 * 
 * REQUIRES SDCARD
 * @author Dag
 *
 */
public class Main extends Activity {
ProgressDialog dlDialog;
String path = Environment.getExternalStorageDirectory()+ "/"; // Path to where you want to save the file
String inet = "http://www.google.com/test.apk"; // Internet path to the file
String cachedir = "";                                       
String filename = "TMC.apk";                                

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TextView webview = new TextView(this);
    setContentView(webview);

    File getcache = this.getCacheDir();
    cachedir = getcache.getAbsolutePath();

    dlDialog = new ProgressDialog(Main.this);
    dlDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    dlDialog.setTitle("Downloadin");
    dlDialog.setMessage("Connecting");
    dlDialog.show();

    new Thread(new Runnable() {

        public void run() {

            String filePath = path;

            InputStream is = null;
            OutputStream os = null;
            URLConnection URLConn = null;

            try {
                URL fileUrl;
                byte[] buf;
                int ByteRead = 0;
                int ByteWritten = 0;
                fileUrl = new URL(inet);

                URLConn = fileUrl.openConnection();

                is = URLConn.getInputStream();

                String fileName = inet.substring(inet.lastIndexOf("/") + 1);

                File f = new File(filePath);
                f.mkdirs();
                String abs = filePath + fileName;
                f = new File(abs);                      


                os = new BufferedOutputStream(new FileOutputStream(abs));

                buf = new byte[1024];

                /*
                 * This loop reads the bytes and updates a progressdialog
                 */
                while ((ByteRead = is.read(buf)) != -1) {

                    os.write(buf, 0, ByteRead);
                    ByteWritten += ByteRead;

                    final int tmpWritten = ByteWritten;
                    runOnUiThread(new Runnable() {

                        public void run() {
                            dlDialog.setMessage(""+tmpWritten+" Bytes");
                        }

                    });
                }

                runOnUiThread(new Runnable() {

                    public void run() {
                        dlDialog.setTitle("Startar");
                    }

                });
                is.close();
                os.flush();
                os.close();


                Thread.sleep(200);

                dlDialog.dismiss();

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

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

            }

        }
    }).start();

}
}

Upvotes: 3

florianbaethge
florianbaethge

Reputation: 2560

You cannot force an APK to be installed...

If it were so anybody could hide viruses or spyware on some servers and when the user clicks a link it automatically gets installed...

Simply put the apk-file you want to install on your server and let the hyperlink point to it... just like to a zip-archive, movie or other executable file.

The browser simply will download the apk and install it (if the user wants so). The user of course needs to activate non-market apps in his settings... (as described in the link above)

I hope this helps you...

Upvotes: 2

Related Questions