Android - Crear archivos con IntentService

 
Vista:
sin imagen de perfil
Val: 7
Ha aumentado su posición en 3 puestos en Android (en relación al último mes)
Gráfica de Android

Crear archivos con IntentService

Publicado por luis (4 intervenciones) el 24/01/2019 19:32:43
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
1
2
3
4
5
6
7
8
9
10
11
<?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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?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"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:onClick="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"
        android:onClick="stopService"
        android:text="stop service" />
</LinearLayout>

MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder