2014-02-18 28 views
0

你好我试图做一个时间计数器,应该在后台工作的应用程序或当用户离开活动。从不同活动的服务中获得价值

它的工作正常,我能够从我的服务中获得时间,并让他即使在应用程序处于后台时也能运行。

我的问题是,当我离开绑定服务的活动并重新输入活动时,我获得了相同服务变量的不同值。

当我第一次拨打电话startTime()时,我在两个日志调用Log.i("time 2", time+"")Log.i("time", time+"")中获得相同的号码。当我离开的活动,重新进入我得到的日志Log.i("time 2", time+"")保持couting的正确时间,但始终Log.i("time", time+"")显示为0。

如何获得的time从我最后一次开始该服务的价值。

这是我的服务代码:

public class OnTheWayCounterService extends Service{ 

    long time; 
    boolean stopThread; 
    private final IBinder mBinder = new LocalBinder(); 

    public boolean isRunning() 
    { 
     return !stopThread; 
    } 

    public void stopTime() 
    { 
     stopThread = true; 
     time = 0; 
    } 

    public void startTime() 
    { 
     stopThread = false; 
     startStopWatch(); 
     time = 0; 
    } 

    @Override 
    public IBinder onBind(Intent arg0) { 
     // TODO Auto-generated method stub 
     return mBinder; 
    } 

    public void startStopWatch() 
    { 

     Handler handler = new Handler(); 

     handler.postDelayed(new Runnable() { 

      @Override 
      public void run() 
      {    
       time++; 

       Log.i("time 2", time+""); 

       if(stopThread == false) 
        startStopWatch(); 

      } 
     }, 1000); 
    } 

    public class LocalBinder extends Binder { 
     public OnTheWayCounterService getService() { 
      // Return this instance of LocalService so clients can call public methods 
      return OnTheWayCounterService.this; 
     } 
    } 

    public String getOnTheWayTime() 
    { 
     long hours = time/3600; 
     long minutes = time/60; 
     long seconds = time % 60; 

     Log.i("time", time+""); 

     String sMinutes; 
     String sSeconds; 

     if(minutes < 10) 
      sMinutes = "0" + minutes; 
     else 
      sMinutes = minutes+""; 

     if(seconds < 10) 
      sSeconds = "0" + seconds; 
     else 
      sSeconds = seconds+""; 

     return hours + ":" + sMinutes + ":" + sSeconds; 
    } 

} 

这是在使用该服务的活动:

public void startStopWatch() 
     { 

      onTheWayTime = (TextView) featureList.findViewById(R.id.travel_time); 

      Handler handler = new Handler(); 

      handler.postDelayed(new Runnable() { 

       @Override 
       public void run() { 
        // TODO Auto-generated method stub 

        runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 

          if(mService.isRunning()) 
          { 
           Log.i("time time", mService.getOnTheWayTime()); 

           onTheWayTime.setText(mService.getOnTheWayTime()); 
           startStopWatch(); 
          } 

         } 
        }); 
       } 
      }, 1000); 

     } 

    @Override 
     protected void onStart() { 
      super.onStart(); 
      // Bind to LocalService 
      Intent intent = new Intent(this, OnTheWayCounterService.class); 
      bindService(intent, mConnection, Context.BIND_AUTO_CREATE); 
      Log.i("onStart", "onStart"); 
     } 

     @Override 
     protected void onStop() { 
      super.onStop(); 
      // Unbind from the service 
      if (mBound) { 
       unbindService(mConnection); 
       mBound = false; 
      } 
     } 

     private ServiceConnection mConnection = new ServiceConnection() { 

      @Override 
      public void onServiceConnected(ComponentName className, 
        IBinder service) { 
       // We've bound to LocalService, cast the IBinder and get LocalService instance 
       Log.i("bind", "bind"); 

       LocalBinder binder = (LocalBinder) service; 
       mService = binder.getService(); 
       mBound = true; 

    if(mService.isRunning() == false) 
       mService.startTime(); 




      } 

      @Override 
      public void onServiceDisconnected(ComponentName arg0) { 
       mBound = false; 
      } 
     }; 

回答

0

我的问题是的onStop

@Override 
     protected void onStop() { 
      super.onStop(); 
      // Unbind from the service 
      if (mBound) { 
       unbindService(mConnection); 
       mBound = false; 
      } 
     } 

我删除它,它的工作。

相关问题