Bonjour,
Je sollicite votre aide car je dois faire une application qui envoie un Toast dans le futur à l'aide d'un service.
Je débute en Android, j'ai lu la doc et j'ai produit le code suivant pour mon IntentService :
J'ai déclaré mon service sur un process spécifique dans le Manifest :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 public class DelayedToast extends IntentService { private final static boolean Debug = true; private final static String TAG = "ALT"; public DelayedToast() { super("DelayedToast"); } @Override protected void onHandleIntent(Intent intent) { final int delay = intent.getIntExtra("delay", -1); if (Debug) Log.i(TAG, "DelayedToast:onHandleIntent delay: " + delay); if (delay > 0) { SystemClock.sleep(delay * 1000); if (Debug) Log.i(TAG, "Wake up !! "); // Intent broadcastIntent = new Intent(); Intent broadcastIntent = new Intent(getApplicationContext(), Receiver.class); // Need to be explicit for Broadcast : https://stackoverflow.com/questions/55610977/why-a-static-broadcastreceiver-not-working broadcastIntent.setAction(getString(R.string.intent_action)); broadcastIntent.putExtra("delay", delay); sendBroadcast(broadcastIntent); showToast("toast Service: " + delay); } } // https://stackoverflow.com/a/34832674 protected void showToast(final String msg) { Handler handler = new Handler(Looper.getMainLooper()); handler.post(new Runnable() { @Override public void run() { if (Debug) Log.i(TAG, msg); Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show(); } }); } }
Mon service est lancé par un bouton :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5 <service android:name=".DelayedToast" android:enabled="true" android:exported="true" android:process=":DelayedToast" />
Je travail avec un A8 pour déboguer mon appli, SDK min 24 pour l'apk
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 public void onClickStart(View v) { EditText editText = findViewById(R.id.delay); final String str_delay = editText.getText().toString(); if (Debug) Log.i(TAG, "MainActivity:onClickStart str: " + str_delay); if (!str_delay.isEmpty()) { final int delay = Integer.parseInt(str_delay); if (Debug) Log.i(TAG, "Q1_MainActivity:onClickStart delay: " + delay); if (delay > 0) { Intent intent = new Intent(this, DelayedToast.class); intent.putExtra("delay", delay); startService(intent); } } }
Lorsque je lance et laisse tourné l'apk en arrière plan tout est bon.
Quand je ferme l'apk, le service est tué, alors qu'il devrait théoriquement resté actif tant que je ne le stop pas manuellement.
Voici la trace obtenu avec adb
Ça fait une semaine que je regarde tous les tuto sur le sujet (web et youtube)
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 C:\Users\dark_vidor>adb shell "ps | grep tp08" // L'application n'est pas lancée C:\Users\dark_vidor>adb shell "ps | grep tp08" // L'application est en cours dexécution u0_a246 20032 3012 2569888 132044 0 0 S test.tp08 C:\Users\dark_vidor>adb shell "ps | grep tp08" // Start "Delayed Toast" (le service) u0_a246 20032 3012 2573316 135404 0 0 S test.tp08 u0_a246 20090 3012 2324660 79876 0 0 S test.tp08:DelayedToast C:\Users\dark_vidor>adb shell "ps | grep tp08" // Le Receiver est activé (Broacast Intet) u0_a246 20032 3012 2580964 137252 0 0 S test.tp08 u0_a246 20090 3012 2326168 96376 0 0 S test.tp08:DelayedToast u0_a246 20127 3012 2325228 99696 0 0 S test.tp08:Receiver C:\Users\dark_vidor>adb shell "ps | grep tp08" // Je quit, service killed.. C:\Users\dark_vidor>
Je n'arrive pas à comprendre qu'est ce qui fait que mon service ne perdure pas...
Auriez vous une idée svp ?
Partager