你好社區,
我已經看了關於我的問題的論壇,但我不知道如何使用的處理程序。
我的問題:
GUI有一個文本框。我創建了一個服務,它的工作原理。該服務應更新UI。
我有什麼:
- 的Android的Galaxy S手持
- 靛藍服務發佈2
我的編碼:
public class SamsungLoc1 extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_samsung_loc1);
buttonStart = (Button) findViewById(R.id.buttonStart);
buttonStop = (Button) findViewById(R.id.buttonStop);
buttonStart.setOnClickListener(this);
buttonStop.setOnClickListener(this);
TextView tv1 = (TextView) findViewById(R.id.TextView01);
tv1.setText("initial1");
}
public void onClick(View src) {
switch (src.getId()) {
case R.id.buttonStart:
startService(new Intent(this, MyService.class));
break;
case R.id.buttonStop:
Log.d(TAG, "onClick: stopping srvice");
break;
}
}
}
所以點擊開始按鈕時,該服務MyService.class啓動:
public class MyService extends Service {
private static final String TAG = "MyService";
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
}
@Override
public void onDestroy() {
}
@Override
public void onStart(Intent intent, int startid) {
Timer t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
// @Override
public void run() {
//Here I would like to change the textview of the UI
tv1.setText("New Information");
}
},0,300000);
}
}
那麼,我已經讀過,我必須使用處理程序,但我不知道如何使用它。可以請請請:-)給我一個代碼片斷如何更改服務的時間任務內的textview tv1?
請問候,
安迪
你確實需要更新用戶界面的服務嗎? – twocity