2014-03-24 53 views
2

我有一項活動和一項服務。我的要求是從活動開始服務,在活動倒數計時器將開始,但問題是我無法從服務中獲得價值到我的活動。請幫助我。如何從本地服務向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; 
    } 

} 

我應該在這個服務寫得那麼我將在我的活動得到吐司消息值

+0

[這裏](http://developer.android.com/guide/components/bound- services.html)你有一個關於如何做到這一點的教程。另外在[這個答案](http://stackoverflow.com/questions/22440064/accessing-context-from-another-thread/22443582#22443582)你有一些解釋 –

回答

1

您可以通過多種方式實現這一目標。 一個優雅的人會有一個控制器類,它將事件發送到您的活動。 您可以在您的onResume方法中將您的Activity註冊爲可觀察的並在onPause方法中取消註冊它。然後,將您的服務中的數據發送到控制器,並將數據從那裏傳遞到您的活動。例如:

//Let your activity implement this interface 
interface MyObservableActivity{ 
    public receiveData(Data yourData); 
} 

//Your observer controller 
class MyController{ 
    Vector<MyObservableActivity> observedItems; 

    public void registerObservable(MyObservableActivity a){ 
     if(!observedItems.contains(a)) 
      observedItems.add(a); 
    } 

    public void unregisterObservable(MyObservableActivity a){ 
     if(observedItems.contains(a)) 
      observedItems.remove(a); 
    } 

    public void sendDataToObservers(Data d){ 
     for(MyObservableActivity a: observedItems){ 
      a.receiveData(d); 
     } 
    } 

} 

因此,從您的服務中,您應該調用sendDataToObservers方法,您將從您的活動中獲取它。

2

閱讀關於廣播接收機。

在活動中創建一個廣播接收器,並使用一些IntentFilter(在字符串值中設置動作)註冊它。

private BroadcastReceiver receiver = new BroadcastReceiver() { 
     public void onReceive(android.content.Context context, Intent intent) { 
     Toast.makeText(context, "death", Toast.LENGTH_LONG) 
       .show(); 
}} 

註冊在onResume

 IntentFilter intentFilter = new IntentFilter();  
intentFilter.addAction("Your Action"); 
this.registerReceiver(receiver, intentFilter); 

而在你的服務只需要調用setBroadcast

@Override 
    public void onFinish() { 
    Intent intent = new Intent(); 
intent.setAction("YourAction"); 
sendBroadcast(intent); 
     stopSelf(); 

    } 
+0

我跟隨你的建議ni得到的解決方案非常感謝(y ) –

相關問題