[AYUDA] Crear archivos con IntentService

1.136 0
 #1
Escrito   0  0  
Buen día Compañeros!

Una pregunta:

Tengo una aplicación que utiliza un IntentService, que su tarea es crear cuatro archivos en un intervalo de tiempo.

tiempos.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="tiempoFinal">20190123223200</string>
<string-array
name="arregloTiempos">
<item>20190123223000</item>
<item>20190123223030</item>
<item>20190123223100</item>
<item>20190123223130</item>
</string-array>
</resources>

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
androidemoticon desconcertadorientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
androidemoticon desconcertadonClick="startService"
android:text="start service" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:layout_gravity="center_horizontal"
androidemoticon desconcertadonClick="stopService"
android:text="stop service" />
</LinearLayout>

MainActivity.java
package com.servicedemo;

import ...

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startService(View view) {
Intent intent = new Intent(this, MyService.class);
startService(intent);
}
public void stopService(View view) {
Intent intent = new Intent(this, MyService.class);
stopService(intent);
}
}

MyService.java
package com.servicedemo;

import ...

public class MyService extends IntentService {
String ruta = "/storage/emulated/0/";
String nuevaCarpeta = "Carpeta";
String nuevoArchivo = "Archivo_";

public MyService() {
super("My_Worker_Thread");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "Service Started...", Toast.LENGTH_LONG).show();
return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
Toast.makeText(this, "Service Stopped...", Toast.LENGTH_LONG).show();
super.onDestroy();
}

@Override
protected void onHandleIntent(Intent intent) {
synchronized (this) {
String[] tiempos = getResources().getStringArray(R.array.arregloTiempos);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss", Locale.getDefault());
boolean sw = true;
byte pos = 0;
while (sw) {
Date date = new Date();
String fecha = dateFormat.format(date);
if (fecha.equals(getResources().getString(R.string.tiempoFinal))) sw = false;
for (byte i = pos; i < tiempos.length; i++) {
if (fecha.equals(tiempos[i])) {
pos = i;
try {
File file = new File(ruta + nuevaCarpeta, nuevoArchivo + tiempos[i] + ".txt");
file.createNewFile();
} catch (Exception e) {
Log.e("Error", "e: " + e);
}
}
}
}
}
}
}

Pero, el IntentService termina antes de que se cumpla la condición, si me crea los cuatro archivos, pero lo que me causa ruido es que el mensaje del Toast que esta en el destroy aparece antes de que termine de generar los cuatro archivos (como si ya se hubiera cumplido la condición de salida -> <string name="tiempoFinal">20190123223200</string> ).

De antemano, gracias.
Volver a Android