我有一項活動和一項服務。我的要求是從活動開始服務,在活動倒數計時器將開始,但問題是我無法從服務中獲得價值到我的活動。請幫助我。如何從本地服務向ANDROID中的活動發送數據
是任何代碼,教程,例子,這將有助於我。
定時服務
public class TimerService extends Service {
MyCounter timer;
@Override
public void onCreate() {
timer = new MyCounter(1 * 60 * 1000, 1000);
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
timer.start();
return super.onStartCommand(intent, flags, startId);
}
private class MyCounter extends CountDownTimer {
public MyCounter(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
Toast.makeText(getApplicationContext(), "death", Toast.LENGTH_LONG)
.show();
stopSelf();
}
@Override
public void onTick(long millisUntilFinished) {
Toast.makeText(getApplicationContext(),
(millisUntilFinished/1000) + "", Toast.LENGTH_SHORT)
.show();
}
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
timer.cancel();
super.onDestroy();
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
我應該在這個服務寫得那麼我將在我的活動得到吐司消息值
[這裏](http://developer.android.com/guide/components/bound- services.html)你有一個關於如何做到這一點的教程。另外在[這個答案](http://stackoverflow.com/questions/22440064/accessing-context-from-another-thread/22443582#22443582)你有一些解釋 –