Bonjour à tous,
Je dois développer une appli pour ma soeur, (je suis développeur php mais très débutant en android) et j'ai beau suivre tous les tuto je ne comprend pas pourquoi au bout de 2 heures le service s'arrète sur mon Galaxy S7 alors que sur le S4 il continue de fonctionné.
Je me doute qui doit y avoir un problème de compatibilité mais la je bloque.
Pour l'instant but de l'appli est de donner l'heure toutes les X secondes
Mon Manifest:
MyBroadcastreceiver:
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 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.didier.newtyro"> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.DISABLE_KEYGUARD" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" 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> <receiver android:name=".MyBroadcastreceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.intent.action.PACKAGE_RESTARTED" /> </intent-filter> </receiver> <service android:name="MonServiceAutoRun" android:enabled="true" android:exported="false" android:process=":ServiceProcess" android:isolatedProcess="true"/> </application> </manifest>
Service:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6 public class MyBroadcastreceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { context.startService(new Intent(context, MonServiceAutoRun.class)); } }
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 public class MonServiceAutoRun extends Service { private Timer timer; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); Log.w("aa", "Service Started"); timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { // TODO Auto-generated method stub Log.w("aa", giveHeure()); } }, 1000, 35000); } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); Log.w("aa", "Service Destroyed"); timer.cancel(); Log.w("aa", "Timer terminated in Service"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { super.onStartCommand(intent, flags, startId); return Service.START_STICKY; } public String giveHeure() { Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("HH:mm"); return sdf.format(cal.getTime()); } }
Partager