Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using putExtra to pass values to intent service

within my main activity I have the following code:

EditText usernameText;
EditText passwordText;
public void sendLogin (View loginview){
    Intent i = new Intent(this, NetworkService.class);
    startService(i);
}

Currently, this just sends an intent to the NetworkService, which is handled as follows (truncated):

public class NetworkService extends IntentService {

    public NetworkService() {
        super("NetworkService");
    }

    protected void onHandleIntent(Intent i) {

        /* HTTP CONNECTION STUFF */

        String login = URLEncoder.encode("Username", "UTF-8") + "=" + URLEncoder.encode("XXX", "UTF-8");
        login += "&" + URLEncoder.encode("Password", "UTF-8") + "=" + URLEncoder.encode("XXX", "UTF-8"); 
    }
}

Now, what I need to figure out, is how to pass those usernameText and passwordText values through to the NetworkService into the 'XXX', but ALSO within the NetworkService, I intend (no pun intended), to have it handle multiple intents from various places, one from a login, one from retrieving some information on users using the logon token, for instance. It's where all my networking will be contained. I was instructed this was the best practise within android applications, to keep the networking separate.

My question is: What is the best way of sending those two variables to the NetworkService and also how, within the onHandleIntent of the NetworkService, do I separate the code to only do what I'm asking it to (login, fetch user information, fetch location data etc)?

Sorry if the answer is a simple one, but I'm very new to application programming.

Thanks

like image 619
Seb Avatar asked Aug 09 '12 10:08

Seb


People also ask

How do you pass data to a service using Intent?

To pass the data through Intent we will use putExtra() method and in parameter, we will use Key-Value Pair. Now, where we have to mention putExtra() method? We have to add putExtra() method in onClick() as shown in the below code and in parameter we have to mention key and its value.

What is the use of Intent putExtra?

Using putExtra() We can start adding data into the Intent object, we use the method defined in the Intent class putExtra() or putExtras() to store certain data as a key value pair or Bundle data object. These key-value pairs are known as Extras in the sense we are talking about Intents.

Which method of the Intent class allows you to pass information to the target?

putExtra method is used.


1 Answers

public void sendLogin (View loginview){
    Intent i = new Intent(this, NetworkService.class);
    i.putExtra("username", usernameText.getText().toString());
    i.putExtra("password", passwordText.getText().toString());
    startService(i);
}

Then in your IntentService:

@Override
    protected void onHandleIntent(Intent intent) {
    String username = intent.getStringExtra("username");
    String password = intent.getStringExtra("password");
    ...
}

IntentServices are designed to handle several requests sent to it. In other words, if you keep sending intents using startService(intent), your NetworkService will keep getting its onHandleIntent method called. Under the hood, it has a queue of intents that it will work through until it is finished. So if you keep sending intents the way you are currently, but with certain flags set through the putExtra methods, then you can detect what your NetworkService should do and act appropriately. e.g. set a boolean extra to your intent called login, in your intentservice look for that flag being set via intent.getBooleanExtra("login"). If true, do your login stuff, else look for other flags you set.

like image 157
luuts Avatar answered Oct 19 '22 10:10

luuts