使用putExtra将值传递给目标服务 [英] Using putExtra to pass values to intent service

查看:192
本文介绍了使用putExtra将值传递给目标服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的主要活动,我有以下code:

 的EditText usernameText;
的EditText passwordText;
公共无效sendLogin(查看loginview){
        意图I =新的意图(这一点,NetworkService.class);
        startService(ⅰ);
    }
 

目前,这只是发送意图的网络服务,这是处理如下(截断):

 公共类网络服务扩展IntentService {

    公共网络服务(){
        超级(网络服务);
    }
    保护无效onHandleIntent(意向我){

                        #HTTP连接STUFF#

                字符串登录= URLEn coder.en code(用户名,UTF-8)+=+ URLEn coder.en code(XXX,UTF-8 );
                登录+ =&放大器; + URLEn coder.en code(密码,UTF-8)+=+ URLEn coder.en code(XXX,UTF-8) ;

    }
}
 

现在,我需要弄清楚,是如何通过对网络服务通过这些usernameText和passwordText值到'XXX',而且出现在网络服务,我打算(没有双关语意),将它处理多意图从不同的地方,一个从登录,一是从检索使用登录标记用户的一些信息,比如。这是我所有的网络将得到遏制。我奉命这是在Android应用程序的最佳做法,以保持网络的独立的。

我的问题是:什么是这两个变量发送到网络服务,也是怎么样,网络服务的onHandleIntent内,我分出code只做什么,我要求它的最好办法(登录,获取用户信息,获取定位数据等)?

很抱歉,如果答案是简单的,但我很新的应用程序。

感谢

解决方案

 公共无效sendLogin(查看loginview){
    意图I =新的意图(这一点,NetworkService.class);
    i.putExtra(用户名,usernameText.getText()的toString());
    i.putExtra(密码,passwordText.getText()的toString());
    startService(ⅰ);
}
 

然后在您的IntentService:

  @覆盖
    保护无效onHandleIntent(意向意图){
    字符串username = intent.getStringExtra(用户名);
    字符串password = intent.getStringExtra(密码);
    ...
}
 

IntentServices旨在处理发送给它的多个请求。换句话说,如果你继续使用发送意图 startService(意向),您的网络服务将不断得到所谓的 onHandleIntent 方法。引擎盖下,它的意图,直到它完成,这将完成的队列。所以,如果你不停地发送意图您目前的方式,但通过 putExtra 方法中设置某些标志,那么你可以检测一下你的网络服务应该做的,采取适当的行动。例如设置一个布尔值额外的你的意图名为登录,在你intentservice寻找该标志通过被设置intent.getBooleanExtra(登录)。如果为真,做你登录的东西,别人找你设置其他标志。

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

解决方案

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.

这篇关于使用putExtra将值传递给目标服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆