Synchronisation application Android
Bonjour,
Actuellement, je travaille sur une application qui appel un service web afin de récupérer des information d'un utilisateur passé en paramètre et ça marche. Ensuite j'ai eu une autre idée j'aimerai que mon application reçoit l'information dés que l'une des informations de l'utilisateur change c'est à dire comme une application mail dés que on reçoit un mail il y a une petite information à coté de l’icône de l'application qui nous informes de la nouvelle information.
Au début, j'ai utilisé les notifications mais c'est statiques j'ai voulu quelque chose de dynamique j'ai cherché sur les forum ils disent d'utiliser les services je me suis documenté, je sens que je devient ennuyeux maintenant :lol:
Ma question : pourrait-je avoir un coup de main discuter un peu de l'architecture comment ça fonctionne ou bien un simple exemple pour commencer
merci
the applications has stopped...
bonsoir,
Désolé pour le dérangement pendant ce jour fériée :-)
J'ai suivi le tuto sur les servies que Aerinder là posté http://blog.developpez.com/android23...on-de-service/ et j'ai essayé de l'appliqué à mon application mais malheureusement il y a une erreur de ce type qui s'affiche the application <nom_de_mon_application> has stopped unexpectedly
si j'enlève cette partie dans mon code
Code:
1 2 3 4 5 6 7 8 9 10
|
BackgroundService.getService().addListener(new IBackgroundServiceListener() {
public void dataChanged(final Object o) {
WS_SAP_activity.this.runOnUiThread(new Runnable() {
public void run() {
// Mise à jour de l'UI
tf.setText(BackgroundService.CallWS());
}
});
}}); |
l'erreur disparait mais je n'aurais pas le résultat voulu. mon but c'est d'appeler un service web à chaque 1 min a fin qu'il ma le résultat dans le champs que je lui assignée.
voici mon activity :
Code:
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
|
public class WS_SAP_activity extends Activity implements View.OnClickListener, IBackgroundServiceListener {
private Button mButton;
private TextView tf;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton =(Button) findViewById(R.id.button1);
tf=(TextView) findViewById(R.id.editText2);
mButton.setOnClickListener(this);
BackgroundService.getService().addListener(new IBackgroundServiceListener() {
public void dataChanged(final Object o) {
WS_SAP_activity.this.runOnUiThread(new Runnable() {
public void run() {
// Mise à jour de l'UI
tf.setText(BackgroundService.CallWS());
}
});
}});
}
@Override
public void onClick(View v) {
// Intent intent = new Intent(this,BackgroundService.class);
// startService(intent);
startService(new Intent(WS_SAP_activity.this, BackgroundService.class));
tf.setText(BackgroundService.CallWS());
}
@Override
public void dataChanged(Object o) {
// TODO Auto-generated method stub
}
} |
voici mon service
Code:
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
public class BackgroundService extends Service implements IBackgroundService{
private static final int START_NOT_STICKY = 0;
private List<IBackgroundServiceListener> listeners = null;
private static IBackgroundService service;
private Timer timer ;
@Override
public void onCreate() {
super.onCreate();
timer = new Timer();
service = this;
Log.d(this.getClass().getName(), "onCreate");
}
public static IBackgroundService getService() {
return service;
}
public int onStartCommand(Intent intent, int flags, final int startId) {
Log.d(this.getClass().getName(), "onStart");
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
// Executer de votre tâche
CallWS();
}
}, 0, 60000);
return START_NOT_STICKY;
}
public static String CallWS()
{
String rs="";
try {
// Create SOAP request
SoapObject request = new SoapObject(NAMESPACE,"Zmafonctionsws2");
request.addProperty("Nom", "FHUYNH");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
// Get response from envelope
Object result1 = envelope.getResponse();
SoapObject body = (SoapObject)envelope.bodyIn;
String result = (String)body.getProperty("WiCount").toString();
// Display result
rs=result;
} catch (Exception e) {
e.printStackTrace();
}
return rs;
}
@Override
public void onDestroy() {
this.listeners.clear();
this.timer.cancel();
Log.d(this.getClass().getName(), "onDestroy");
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void addListener(IBackgroundServiceListener listener) {
// TODO Auto-generated method stub
if(listeners == null){
listeners = new ArrayList<IBackgroundServiceListener>();
}
listeners.add(listener);
}
@Override
public void removeListener(IBackgroundServiceListener listener) {
// TODO Auto-generated method stub
if(listeners != null){
listeners.remove(listener);
}
}
// Notification des listeners
private void fireDataChanged(Object data){
if(listeners != null){
for(IBackgroundServiceListener listener: listeners){
listener.dataChanged(data);
}
}
}
} |
merci,