从不同活动的服务中获取价值

时间:2014-02-18 15:02:32

标签: android

您好我正在尝试创建一个时间计数器,该计时器应该在后台运行应用程序或用户离开活动时。

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

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

当我第一次拨打startTime()时,我的日志调用Log.i("time 2", time+"")Log.i("time", time+"")都会得到相同的号码。当我离开活动并重新输入时,我会得到Log.i("time 2", time+"")的日志,保持正确的时间,但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;
            }
        };

1 个答案:

答案 0 :(得分:0)

我的问题是onStop

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

我删除了它并且正在工作。