3

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...

4

5 に答える 5

2

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");
}
于 2012-04-16T05:46:36.210 に答える
1

Two options:

  1. If service is local, then you can bind to it and just call methods directly
  2. If service is remote, than you can use Bundle to pass some data.
于 2012-04-16T05:46:19.567 に答える
1

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 .

于 2018-01-10T22:17:28.670 に答える
-1

/**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);
}
于 2012-12-23T11:15:48.467 に答える
-1

/**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();
于 2012-12-23T11:18:52.913 に答える