I am creating an application in android.
How to pass an object of ArrayList to service while starting service and access in service???
Thanks in advance...
I am creating an application in android.
How to pass an object of ArrayList to service while starting service and access in service???
Thanks in advance...
You have to Override onStart Method in your service.OnStart method you can get intent of Activity. If you want to pass ArrayList from activity to service,you can convert your arraylist to array.
In you Activity
Intent intent=new Intent(ServicesActivity.this,FileManagerRequest.class);
Bundle b=new Bundle()
b.putStringArray("Array", your_array)
intent.putExtras(b);
startService(intent);
in you service
public void onStart(Intent intent, int startid){
super.onStart(intent, startid);
Bundle b=intent.getExtras();
String[] Array = b.getStringArray("Array");
}
Two options:
Bundle
to pass some data.Finally I got answer , its work for me, Just Try it.
1) while sending object likes ArrayList<String> names
from Activity send to this way example. names.add("kdblue");
Intent startIntent = new Intent(CuurentActivity.this, UploadService.class);
startIntent.putStringArrayListExtra("data",names);
startService(startIntent);
2) Now receiving this ArrayList<String>
object from service
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
ArrayList<String> transferData = intent.getStringArrayListExtra("data");
return START_STICKY;
}
Note: transferData
object contains all ArrayList<String> names
properties .
/**in your activity***/
startbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
Bundle b=new Bundle();
b.putString("id", id);
Intent in=new Intent(create.this,myservice.class);
in.putExtras(b);
//Toast.makeText(getBaseContext(), "Service Started", Toast.LENGTH_LONG).show();
startService(in);
}
/**in your service****/
public int onStartCommand(Intent intent, int flags, int startId) {
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
super.onStart(intent, startId);
id=intent.getExtras();
value=id.getString("id");
Toast.makeText(this, "Service Started "+value, Toast.LENGTH_LONG).show();