Greetings lovers to the programming!
I have a problem calling a class external, I used:
AutoUpdate shh = new AutoUpdate();
shh;
However, I can not call the class from the MainActivity.
At the moment I am calling the class as if it were another activity (but it is not, what the class does is to verify if there is a new version and showing a dialog if actualise now or after.), I used:
Intent uppp = new Intent(this, AutoUpdate.class);
startActivity(uppp,null);
The code works perfectly. But I do not want this to create a second activity, the essential thing is to show the dialog in the same MainActivity.
This is the code:
public class AutoUpdate extends AppCompatActivity {
public static String url1 = "localhost/version.json";
String VersionUpdate;
String Cambios;
String link;
String nombre;
String ejecutar;
private ProgressDialog pDialog;
public static final int progress_bar_type = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigator);
new VersionCheck().execute();
permission_check();
}
public void permission_check() {
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},100);
return;
}
}
//initialize();
}
public class VersionCheck extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
String jsonStr = sh.makeServiceCall(url1);
if (jsonStr != null){
try {
JSONObject jsonObj = new JSONObject(jsonStr);
JSONArray obtener = jsonObj.getJSONArray("Obtener");
for (int i = 0; i < obtener.length(); i++)
{
JSONObject v = obtener.getJSONObject(i);
link = v.getString("link");
VersionUpdate = v.getString("version");
nombre = v.getString("nombre");
Cambios = "";
JSONArray cambiosArr = v.getJSONArray("cambios");
for (int j = 0; j < cambiosArr.length(); j++) {
Cambios += cambiosArr.getString(j) + "\n";
}
}
}catch (final JSONException e) {
// Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"El formato de JSON es invalido: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
});
}
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"El servidor de comprobar la version esta caido, por favor chequear la version en: Ajustes > Comprobar",
Toast.LENGTH_LONG)
.show();
}
});
}
return null;
}
@Override
protected void onPostExecute (Void result){
if (VersionUpdate != null) {
super.onPostExecute(result);
String VersionName = BuildConfig.VERSION_NAME;
if (VersionUpdate.equals(VersionName)) {
/*Toast.makeText(getApplicationContext(),
"Version actual: " + VersionName,
Toast.LENGTH_LONG)
.show();*/
finish();
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(AutoUpdate.this);
builder.setTitle("Actualización");
builder.setIcon(R.mipmap.ic_launcher);
builder.setCancelable(false);
builder.setMessage("Actual: "+ VersionName + "\n"+ "Disponible: " + VersionUpdate + "\n" + "\n" + "Incluye: " +"\n" +"\n" + Cambios + "\n")
.setPositiveButton("¡Actualizame!", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Ejecutamos el class para descargar la version
new DownloadFileFromURL().execute(link);
}
}
)
.setNegativeButton("Despues", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
}
}
@Override
protected Dialog onCreateDialog(int id){
switch (id){
case progress_bar_type:
pDialog = new ProgressDialog(this);
pDialog.setMessage("Descargando actualización"+"\n"+"Espere...");
pDialog.setIndeterminate(false);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setCancelable(false);
pDialog.show();
return pDialog;
default:
return null;
}
}
class DownloadFileFromURL extends AsyncTask<String, String, String>{
@Override
protected void onPreExecute(){
super.onPreExecute();
showDialog(progress_bar_type);
}
@Override
protected String doInBackground(String... f_url){
int count;
try{
URL url = new URL(f_url[0]);
URLConnection connection = url.openConnection();
connection.connect();
int lenghtOfFile = connection.getContentLength();
InputStream input = new BufferedInputStream(url.openStream(), 8192);
String storageDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "/download/"+nombre+VersionUpdate+".apk";
File arch = new File(storageDir+fileName);
OutputStream output = new FileOutputStream(arch);
ejecutar = storageDir+fileName;
byte data[] = new byte[1024];
long total = 0;
while((count = input.read(data)) != -1){
total += count;
publishProgress(""+(int)((total*100)/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
}catch (Exception e){
//Log.e("Error: ", e.getMessage());
}return null;
}
protected void onProgressUpdate(String... progress){
pDialog.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String file_url){
dismissDialog(progress_bar_type);
//aca ejecutamos al finalizar la descarga
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(ejecutar)),
"application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // without this flag android returned a intent error!
startActivity(intent);
finish(); //finalizamos cerrando la app
}
}
}
I will be reading them, to see what possible solution there is.
For those who may not have understood me, or I have not given myself to explain well.
EDIT:
This is the MainActivity:
And this happens when I call "AutoUpdate":
As you can see, another activity is created, when I only want the dialog to be
shown in the
MainActivity
(Image 1)