Android的 - 活动和服务之间的连接 [英] Android - Connection between Activity and Service
问题描述
我有一个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");
}
这篇关于Android的 - 活动和服务之间的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!