PowenKo > Android > service >Pass data from activity to service

March 25, 2012   ·   0 Comments

Pass data from activity to service
In activity:

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.

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");
 ...
}
shareShare on FacebookShare on Google+Share on LinkedInEmail this to someonePrint this pageBuffer this pageDigg thisFlattr the authorShare on RedditPin on PinterestShare on StumbleUponShare on TumblrTweet about this on Twitter

By


Readers Comments (0)


You must be logged in to post a comment.

PowenKo’s Online Store

powenko_arduinoBook
Android7

Amazon

adsbygoogle

Recent Posts

Categories

Amazon