活动与服务之间的连接 [英] Connection between Activity and Service

查看:191
本文介绍了活动与服务之间的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个EditText和一些复选框的活动。用户插入的文本后,文本应被发送到服务和服务将在后台运行,显示时间不时举杯。

I have an Activity with an EditText and some checkboxes. After the user inserts a text the text should be sent to the Service and the Service will run in background showing a Toast from time to time.

我有一个很艰难的时期,试图找出如何发送数据(字符串和布尔值,通过活动用户输入)以服务

I am having a really hard time trying to figure out how to send the data(Strings and Boolean values that user inputs through the Activity) to the Service .

推荐答案

在活动中使用意向把值puExtra

Use Intent on Activity put values in puExtra

Intent intent = new Intent(current.this, YourClass.class);
intent.putextra("keyName","value"); 

,然后调用StartService所以OnStart方法调用被调用..
在服务通过使用意图得到的OnStart值

and then call StartService so the OnStart method call be called.. in service get the values in OnStart by using intent

Bundle extras = getIntent().getExtras();
   if (extras != null) 
   {
     String value = extras.getString("key");
   }

这篇关于活动与服务之间的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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