开发者

Android 1.5, wana send info from "class extending activity" to "class extending service"

I'm new to Android!

I want to create a background service, the UI contains EditText field (in which user inputs the time) and "start activity" and "stop activity buttons". By clicking start activity button, log info will be shown in log cat that servic开发者_如何学Ce is started and a repeating log info message after the time interval specified by user will also be shown! my problem is, I can get the time value provided by user in EditText field, but how can I use it?

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;
}}

(I have to pass int i somehow to MyService.class as I will use it in onStart(Intent intent, int startid)

public class MyService extends Service { .. }

PROBLEM SOLVED: simply put the "int i" in the intent (and also slightly changing the intent too) in class extending actiivity

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

then change in class extending service will be:

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

now we can use "value" variable! regards!


Use binding between service and activity. Searching here gives you some results:

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

Service binding

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜