1

Basically I have created an app into which the user enters a number (in seconds). They then click a button and a service is created which runs a countdown starting at the number of seconds entered. However in my service I reference variables such as:

public EditText text = (EditText)findViewById(R.id.editText1);
int Time = Integer.parseInt(text.getText().toString());

This 'breaks' the service as it is static and can't reference findViewById. I have tried for hours to work around this but I have no clue, any help much appreciated!

4

1 に答える 1

0

なぜ であるService必要があるのstaticですか? AServiceは、開始/停止またはバインド/アンバインド (または 2 つの組み合わせ) のいずれかである必要があります。staticAndroidを作成する必要はありませんService

このコードを...Activityではなくあなたに入れますService

public EditText text = (EditText)findViewById(R.id.editText1);
int time = Integer.parseInt(text.getText().toString());

...次に、起動に使用するServiceに時間を渡すだけです...Intent

Intent i = new Intent(this, MyService.class);
i.putExtra("the_time", time);
startService(i);

次に、Service onStartCommand(...)メソッドの使用で...

int time = intent.getIntExtra("the_time", 0);
于 2012-11-29T00:14:23.640 に答える