【问题标题】:Android 1.5, wana send info from "class extending activity" to "class extending service"Android 1.5,wana 将信息从“类扩展活动”发送到“类扩展服务”
【发布时间】:2011-03-18 10:37:47
【问题描述】:

我是 Android 新手!

我想创建一个后台服务,UI 包含 EditText 字段(用户在其中输入时间)和“开始活动”和“停止活动按钮”。通过点击开始活动按钮,日志信息将显示在服务已启动的日志猫中,并且在用户指定的时间间隔后也会显示重复的日志信息消息! 我的问题是,我可以在 EditText 字段中获取用户提供的时间值,但是我该如何使用呢?

public class ServicesDemo extends Activity implements OnClickListener {
    ...

    public void onClick(View src) {
              switch (src.getId()) {        
          case R.id.buttonStart:
          Log.d(TAG, "onClick: starting srvice");

              EditText text = (EditText) findViewById(R.id.text);
          int i = Integer.parseInt(text.getText().toString());

              Intent intent = new Intent(this, ServicesDemo.class);
          startService(new Intent(this, MyService.class));
          break;
}}

(我必须以某种方式将int i 传递给MyService.class,因为我将在onStart(Intent intent, int startid) 中使用它

public class MyService extends Service { .. }

问题已解决: 只需在类扩展活动中将“int i”放在意图中(并且也稍微改变意图)

Intent i1 = new Intent(this, MyService.class);
i1.putExtra("milisec", i*1000);

那么类扩展服务的变化将是:

value = intent.getExtras().getInt("milisec");

现在我们可以使用“值”变量了! 问候!

【问题讨论】:

    标签: android service android-activity


    【解决方案1】:

    在服务和活动之间使用绑定。在这里搜索会得到一些结果:

    how-do-i-bind-this-service-in-android

    Service binding

    【讨论】:

    • 可能是解决这个问题的一种方法,我没有检查过,但我找到了另一种解决方案!
    • 请在此处发布,作为与社区分享知识的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-09
    • 2018-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多