Nov 3, 2011

Pass data from Activity to Service via Intent in startService()

In this example, modify from last post Pass data from Service to Activity, Extra is pass from Activity to Service in startService(). It will be retrieved in onStartCommand() of Service.

Pass data from Activity to Service via Intent in startService()

AndroidServiceTestActivity.java
package com.AndroidServiceTest;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.Toast;

public class AndroidServiceTestActivity extends Activity {

MyReceiver myReceiver;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

}

@Override
protected void onStart() {
// TODO Auto-generated method stub

//Register BroadcastReceiver
//to receive event from our service
myReceiver = new MyReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(MyService.MY_ACTION);
registerReceiver(myReceiver, intentFilter);

//Start our own service
Intent intent = new Intent(AndroidServiceTestActivity.this,
com.AndroidServiceTest.MyService.class);
intent.putExtra("INIT_DATA", "Data passed from Activity to Service in startService");
startService(intent);

super.onStart();
}

@Override
protected void onStop() {
// TODO Auto-generated method stub
unregisterReceiver(myReceiver);
super.onStop();
}

private class MyReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub

int datapassed = arg1.getIntExtra("DATAPASSED", 0);
String orgData = arg1.getStringExtra("DATA_BACK");

Toast.makeText(AndroidServiceTestActivity.this,
"Triggered by Service!\n"
+ "Data passed: " + String.valueOf(datapassed) + "\n"
+ "original Data: " + orgData,
Toast.LENGTH_LONG).show();

}

}
}


package com.AndroidServiceTest;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class MyService extends Service {

final static String MY_ACTION = "MY_ACTION";
String initData;

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub

initData = intent.getStringExtra("INIT_DATA");

MyThread myThread = new MyThread();
myThread.start();

return super.onStartCommand(intent, flags, startId);
}

public class MyThread extends Thread{

@Override
public void run() {
// TODO Auto-generated method stub
for(int i=0; i<10; i++){
try {
Thread.sleep(5000);
Intent intent = new Intent();
intent.setAction(MY_ACTION);

intent.putExtra("DATAPASSED", i);
intent.putExtra("DATA_BACK", initData);

sendBroadcast(intent);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
stopSelf();
}

}

}


Note: Modify AndroidManifest.xml to add service ".MyService", refer to the post Create our own Service and BroadcastReceiver.

Next post:
- Interactive between Activity and Service



1 comment:

Infolinks In Text Ads