skip to Main Content

I want to pass the arraylist of all the phonenumbers created into the background service so that I can message them all at once when a shake gesture is triggered.

This is the MainActivity snippet where I use the intent.putExtra() method to pass my arraylist into the service.

SensorService sensorService = new SensorService();
            Intent intent = new Intent(this, sensorService.getClass());
            if(!isMyServiceRunning(sensorService.getClass())){
                startService(intent);
                intent.putExtra("ContactNumberList", Contact_Number);
                Toast.makeText(this, "Starting Service", Toast.LENGTH_SHORT).show();
            }

and this is the snippet for assigning the passed arraylist. in the SensorService

       DatabaseHelper db = new DatabaseHelper(SensorService.this);
       ArrayList<String> Contact_id, Contact_Name, Contact_Number;
       ArrayList<String> phonenumber = new ArrayList<String>();
       phonenumber = (ArrayList<String>)getIntent().getSerializableExtra("ContactNumberList");

I get the compiler error Cannot resolve method 'getIntent()'

I wanted to know if there is a better way to pass the created arraylist from the MainActivity to my background service ?

2

Answers


  1. Try removing (ArrayList<String>) type cast. It should give you the option to import getIntent after that. If you need to type cast it, you can do it after you have the value.

    Login or Signup to reply.
  2. You need two things:

    1. call "intent.putExtra(…)" BEFORE use that intent on startService()
    2. implement "onStartCommand(Intent intent…) { … } in your Service and use "intent" parameter (this method is called from MainThread/UiThread)
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search