Bonjour tous le monde c'est encore moi :p,
Voilà avec l'aide de tutoriel j'ai créer une classe Service qui se nomme KernelService et je n'arrive pas à la lancer depuis mon activity principal
Voici le service:
Et voici l'activity qui doit lancer le service:
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 package com.caranille.myownpet.Kernel; import android.app.IntentService; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.TaskStackBuilder; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.support.v7.app.AlertDialog; import android.support.v7.app.NotificationCompat; import android.util.Log; import android.widget.Toast; import com.caranille.myownpet.R; import java.util.Timer; import java.util.TimerTask; public class KernelService extends IntentService { private Timer timer = new Timer(); static String name = "KernelService"; public KernelService() { super(name); } @Override public void onCreate() { Toast.makeText(KernelService.this, "Lancement du service", Toast.LENGTH_LONG).show(); timer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { Refresh(); } }, 0, 1000); } public void Refresh() { Toast.makeText(KernelService.this, "Methode Refresh du service", Toast.LENGTH_LONG).show(); } @Override protected void onHandleIntent(Intent intent) { } @Override public void onDestroy() { super.onDestroy(); } }
Et voici 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
37
38
39
40
41
42
43
44
45
46 package com.caranille.myownpet; import android.Manifest; import android.app.Activity; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.TaskStackBuilder; import android.bluetooth.BluetoothAdapter; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.support.annotation.RequiresApi; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private Button Resume; private Button Stats; private Button Training; private Button Arena; private TextView TrainerSerialNumber; private int PERMISSION_ALL = 1; //STATIC static Disease disease; @RequiresApi(api = Build.VERSION_CODES.M) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startService(new Intent(MainActivity.this, KernelService.class)); } public void onDestroy() { super.onDestroy(); } }
le problème est que je n'ai aucun Toast qui se lance ce qui veut bien dire que mon service passe à la poubelle :p
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 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.caranille.myownpet"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name="Kernel.KernelService"></service> </application> </manifest>
Je ne sais pas où je me suis vautré mais si ça se trouve vous allez vous dire ha c'est tout bête il a oublié ça :p
Merci d'avance de votre aide
Cordialement,
Partager