Bonjour,

Je suis en train de faire une application qui devra mettre à jour ces données. Pour cela le système de mise à jour doit pouvoir ce lancer à intervalle régulier (pour les tests 1min) et ce depuis le démarrage du téléphone.

Le problème c'est que malgré tous les tuto que j'ai pu lire cela ne fonctionne pas. Si le service de mise à jour (UpdateService) est démarrée depuis une activity cela fonctionne mais pas depuis le boot au démarrage qui pourtant démarre comme il faut.

La classe qui ce lance au démarrage du téléphone :
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
package src.updates;
 
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
 
public class AutoStartNotifyReceiver extends BroadcastReceiver {
    private final String BOOT_COMPLETED_ACTION = "android.intent.action.BOOT_COMPLETED";
 
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals(BOOT_COMPLETED_ACTION)) {
            System.out.println("AutoStartNotifyReceiver::onReceive");
 
            Intent myIntent = new Intent(context, UpdateService.class);
            context.startService(myIntent);
 
            System.out.println("AutoStartNotifyReceiver::onReceive done");
        }
    }
}
Les lignes AutoStartNotifyReceiver::onReceive et AutoStartNotifyReceiver::onReceive done s'écrivent bien sur la sortie standard, le problème ne semble pas venir de là.

La classe de 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
package src.updates;
 
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
 
public class UpdateService extends android.app.Service {
    final static long CHECK_INTERVAL = 60000; // en millisecondes (devrait être un ou plusieurs heures plus tard)
 
    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }
 
    @Override
    public void onCreate() {
        super.onCreate();
 
        Intent intentOnAlarm = new Intent(UpdateReceiver.ACTION_PULSE_SERVER_ALARM);
        PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intentOnAlarm, 0);
 
        AlarmManager alarms = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        alarms.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + CHECK_INTERVAL, CHECK_INTERVAL, alarmIntent);
 
        System.out.println("UpdateService::onCreate() done");
    }
 
    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        System.out.println("UpdateService::onStart() done");
    }
}
Là aussi tout semble aller car "UpdateService::onCreate() done" et "UpdateService::onStart() done" s'affichent.

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
package src.updates;
 
import java.util.Date;
 
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
 
public class UpdateReceiver extends BroadcastReceiver {
    public static final String ACTION_PULSE_SERVER_ALARM = "com.mon_dom.mon_app.ACTION_PULSE_SERVER_ALARM";
 
    @Override
    public void onReceive(Context context, Intent intent) {
        System.out.println("UpdateReceiver::onReceive");
    }
}
Là "UpdateReceiver::onReceive" ne s'affiche jamais, du moins lorsque l'on démarre le UpdateService depuis le boot car si il est lancé manuellement d'une Activity cela fonctionne très bien.


Mon AndroidManifest.xml :
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
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mon_dom.mon_app"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk
        android:minSdkVersion="4"
        android:targetSdkVersion="4" />
 
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
 
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
 
        <receiver android:name="src.updates.AutoStartNotifyReceiver" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
 
        <receiver android:name="src.updates.UpdateReceiver" android:label="@string/app_name">
            <intent-filter>
                <action android:name="com.mon_dom.mon_app.ACTION_PULSE_SERVER_ALARM" />
            </intent-filter>
        </receiver>
 
        <service android:name="src.updates.UpdateService" />
    </application>
 
</manifest>
Remarque : c'est Android 1.6, je pense que les autorisations sont correctes, j'ai pas mis les Activity pour plus de lisibilité.

Le message d'erreur :
05-11 15:49:11.906: W/ActivityManager(51): Scheduling restart of crashed service com.mon_dom.mon_app/src.updates.UpdateService in 5000ms

Sur la sortie standard aucun "UpdateReceiver::onReceive" à l'horizon, voici tout ce que j'ai dessus :
05-11 15:49:10.056: I/System.out(176): AutoStartNotifyReceiver::onReceive
05-11 15:49:10.106: I/System.out(176): AutoStartNotifyReceiver::onReceive done
05-11 15:49:10.226: I/System.out(176): UpdateService::onCreate() done
05-11 15:49:10.226: I/System.out(176): UpdateService::onStart() done

Autre demande : peut t'on redémarrer correctement l'émulateur Android ? Pour l'instant je ne stoppe et redémarre car je n'ai pas moyen de faire autrement.

Merci