Wednesday, 15 April 2015

Android Install APK by prommatically




call this method  where u needed or u can use the broadcast receiver when the new updated APK is uploaded.
-----------------------------------------------------------------------------------------------

private void update() {

new updateAPK().execute();
System.out.println("updating..done");


}
---------------------------------------------------------------------------------------------
use the asyntask

class updateAPK extends AsyncTask<String, Void, String>{

@Override
protected String doInBackground(String... params) {
downloadapk();
return null;
}

@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
installApk();
}

}


-----------------------------------------------------------------------------
download the APK

private void downloadapk(){
   try {
       URL url = new URL("http://bluetaxi.in/apk/Spot.apk");
       HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
       urlConnection.setRequestMethod("GET");
       urlConnection.setDoOutput(true);
       urlConnection.connect();

       File sdcard = Environment.getExternalStorageDirectory();
       File file = new File(sdcard, "filename.apk");

       FileOutputStream fileOutput = new FileOutputStream(file);
       InputStream inputStream = urlConnection.getInputStream();

       byte[] buffer = new byte[1024];
       int bufferLength = 0;

       while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
           fileOutput.write(buffer, 0, bufferLength);
       }
       fileOutput.close();
    //   this.checkUnknownSourceEnability();
     //  this.initiateInstallation();

   } catch (MalformedURLException e) {
           e.printStackTrace();
   } catch (IOException e) {
           e.printStackTrace();
   }
   }

---------------------------------------------------------------------------------------------
install by programm

private void installApk(){
System.out.println("start install");
        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri uri = Uri.fromFile(new File("/sdcard/filename.apk"));
        intent.setDataAndType(uri, "application/vnd.android.package-archive");
        startActivity(intent);
        System.out.println("complete install");
    }

No comments:

Post a Comment