-1

I have to call three different methods simultaneously in Broadcast receiver(say method a,b,c). .My problem is how can I call these methods to get data from all together.

public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        getA();
        getB();
        getC();
    }
public void getA(){
//code here
}
public void getB(){
//code here
}
public void getc(){
//code here
}

I have to send the values of these methods in database(public ip).

Please Share some relevent code to solve this issue. Thanks.

Anjali
  • 206
  • 3
  • 10

1 Answers1

2

The easiest way to do threading in Android is using an AsyncTask.

I'm not sure if you would like to pass each of these methods an argument, or if you would like anything back from them; but let's assume you're not passing or expecting anything.

public void onReceive(Context context, Intent intent) {
    // start the first method
    new AsyncTask<Integer, Integer, Integer>() {
        @Override
        protected Integer doInBackground(Integer... params) {
            getA();
        }
    }.execute();
    // start the second method
    new AsyncTask<Integer, Integer, Integer>() {
        @Override
        protected Integer doInBackground(Integer... params) {
            getB();
        }
    }.execute();
    // start the third method
    new AsyncTask<Integer, Integer, Integer>() {
        @Override
        protected Integer doInBackground(Integer... params) {
            getC();
        }
    }.execute();
}

The <Integer, Integer, Integer> params stand for the types of the parameters you would like to pass to the task, the type of progress updates, and the returned result type.

If you would like to pass each method some arguments, then change the first Integer to the type of argument you would like to pass, then add these arguments in the execute() method.


If you would like to start an activity and send it some data from the receiver, you should do it using an Intent.

public void getA(Context context, Object dataToPass) {
    // replace DestinationActivity with the Activity that you want to start
    Intent i = new Intent(context, DestinationActivity.class);
    // add the data that you want to pass
    i.putExtra("some-constant", dataToPass);
    // start the actual activity
    context.startActivity(i);
}

In that case, you would have to change the call to getA() in the onReceive() method to add the context and the data that you want to pass.

Reference: http://developer.android.com/reference/android/os/AsyncTask.html

peter
  • 449
  • 4
  • 7
  • your code is really helpful for me.Aboveall I am retrieving some data through it and want to show it in listview of activity class.so,how can I call these data in activty class to set on listview – Anjali Dec 16 '13 at 06:28
  • what's the data that you're retrieving? depending on what it is, you might be able to implement a method on the activity that takes the data as an argument and updates the listview – peter Dec 16 '13 at 06:33
  • wait, do you mean that you can't get a reference to the running instance of the activity? – peter Dec 16 '13 at 06:34
  • its call log data, web Log data, and Phone Information. – Anjali Dec 16 '13 at 06:36
  • yes,I can't get the idea to call these data in activity – Anjali Dec 16 '13 at 06:37
  • One easy way would be to create a new Intent to start the activity, and pass the data to the activity as an EXTRA object in the Intent – peter Dec 16 '13 at 06:39
  • have a look at http://developer.android.com/training/basics/firstapp/starting-activity.html#BuildIntent – peter Dec 16 '13 at 06:40
  • can u give a sample to pass as an Extra object.I am new here so not have exact idea of it – Anjali Dec 16 '13 at 06:45
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/43232/discussion-between-anjali-and-peter) – Anjali Dec 16 '13 at 06:55