sábado, 10 de noviembre de 2012

How to pass data from activity to service




Pass data from activity to service
In activity:
?
1
2
3
4
5
6
7
8
Intent myIntent = new Intent(MainActivity.this, MyService.class);
    
Bundle bundle = new Bundle();
bundle.putCharSequence("extraData", data);
myIntent.putExtras(bundle);
    
pendingIntent = PendingIntent.getService(MainActivity.this, 0, myIntent, 0);
Basically, it's similar to that in "Pass data between activity".

In service side, there are no getIntent().getExtras() in Service class. We can handle it in onStart() call-back method, intent will be passed as parameter.
?
1
2
3
4
5
6
7
8
9
10
public void onStart(Intent intent, int startId) {
 // TODO Auto-generated method stub
 super.onStart(intent, startId);
  
 Bundle bundle = intent.getExtras();
 data = (String) bundle.getCharSequence("extraData");
 smsTextToSend = (String) bundle.getCharSequence("extraSmsText");
 ...
}









No hay comentarios:

Publicar un comentario