document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public class Principal extends Activity implements AlertDialog.OnClickListener{
  2.    
  3.     private Autoupdater updater;
  4.     private RelativeLayout loadingPanel;
  5.     private Context context;
  6.    
  7.     protected void onCreate(Bundle savedInstanceState) {
  8.             super.onCreate(savedInstanceState);
  9.             setContentView(R.layout.activity_Principal);
  10.  
  11.             try {
  12.                     loadingPanel = (RelativeLayout) findViewById(R.id.loadingPanel);
  13.             //Esto sirve si la actualizacion no se realiza al principio. No es este caso.
  14.                     //loadingPanel.setVisibility(View.GONE);
  15.             comenzarActualizacion();
  16.  
  17.             }catch (Exception ex){
  18.             //Por Cualquier error.
  19.                     Toast.makeText(this,ex.getMessage(),Toast.LENGTH_LONG)
  20.             }
  21.     }
  22.    
  23.     private void comenzarActualizar(){
  24.         //Para tener el contexto mas a mano.
  25.             context = this;
  26.         //Creamos el Autoupdater.
  27.             updater = new Autoupdater(this);
  28.         //Ponemos a correr el ProgressBar.
  29.             loadingPanel.setVisibility(View.VISIBLE);
  30.         //Ejecutamos el primer metodo del Autoupdater.
  31.             updater.DownloadData(finishBackgroundDownload);
  32.     }
  33.    
  34.     /**
  35.     * Codigo que se va a ejecutar una vez terminado de bajar los datos.
  36.     */
  37.     private Runnable finishBackgroundDownload = new Runnable() {
  38.             @Override
  39.             public void run() {
  40.             //Volvemos el ProgressBar a invisible.
  41.                     loadingPanel.setVisibility(View.GONE);
  42.             //Comprueba que halla nueva versión.
  43.                     if(updater.isNewVersionAvailable()){
  44.                 //Crea mensaje con datos de versión.
  45.                 String msj = "Nueva Version: " + updater.isNewVersionAvailable();
  46.                         msj += "\\nCurrent Version: " + updater.getCurrentVersionName() + "(" + updater.getCurrentVersionCode() + ")";
  47.                         msj += "\\nLastest Version: " + updater.getLatestVersionName() + "(" + updater.getLatestVersionCode() +")";
  48.                 msj += "\\nDesea Actualizar?";
  49.                 //Crea ventana de alerta.
  50.                         AlertDialog.Builder dialog1 = new AlertDialog.Builder(context);
  51.                         dialog1.setMessage(msj);
  52.                         dialog1.setNegativeButton(R.string.cancel, null);
  53.                 //Establece el boton de Aceptar y que hacer si se selecciona.
  54.                         dialog1.setPositiveButton(R.string.accept, new DialogInterface.OnClickListener() {
  55.                                 @Override
  56.                                 public void onClick(DialogInterface dialogInterface, int i) {
  57.                     //Vuelve a poner el ProgressBar mientras se baja e instala.
  58.                                 loadingPanel.setVisibility(View.VISIBLE);
  59.                     //Se ejecuta el Autoupdater con la orden de instalar. Se puede poner un listener o no
  60.                                 updater.InstallNewVersion(null);
  61.                                 }
  62.                         });
  63.                
  64.                 //Muestra la ventana esperando respuesta.
  65.                         dialog1.show();
  66.                     }else{
  67.                 //No existen Actualizaciones.
  68.                 Log.d("No Hay actualizaciones");
  69.             }
  70.         }
  71.     };
  72. }
');