在与服务的背景中运行的秒表

时间:2016-07-10 13:36:29

标签: java android android-studio

我正在尝试为Android制作一个简单的秒表。它应该在最小化应用程序后继续计算时间。我已将秒表逻辑放入服务中,并在MainActivity中绑定此服务以控制它。计数时间在单独的线程中运行,我使用messenger将时间发送回MainActivity。只要我没有最小化应用程序并在秒表运行时返回,一切都很好。之后,按开始/停止运行第二个线程计数时间而不是停止第一个。可以检查一下是什么问题?会很感激:))

以下是java类:

public class MainActivity extends Activity {

    public static Handler sHandler;
    private final int playPause = 0;
    private final int reset = 1;
    private int secs = 0;
    private int mins = 0;
    private int millis = 0;
    private long currentTime = 0L;
    private boolean isBound = false;
    private MyService myService;
    private Intent intent;
    @BindView(R.id.timer)
    TextView time;



    @OnClick(R.id.fab_playPause)
    public void playPause() {

        myService.startStop();

    }

    @OnClick(R.id.fab_reset)
    public void reset() {

        myService.reset();
        mins = 0;
        secs = 0;
        millis = 0;
        setTime();

    }

    @OnClick(R.id.fab_exit)
    public void exit() {
        onDestroy();
    }

    @OnClick(R.id.fab_save)
    public void save() {


    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        intent = new Intent(this, MyService.class);

        MainActivity.sHandler = new Handler() {

            @Override
            public void handleMessage(Message timeMsg) {
                super.handleMessage(timeMsg);

                currentTime = Long.valueOf(timeMsg.obj.toString());

                secs = (int) (currentTime / 1000);
                mins = secs / 60;
                secs = secs % 60;
                millis = (int) (currentTime % 1000);
                setTime();
            }
        };


    }

    @Override
    protected void onResume() {
        super.onResume();

        bindService(intent, myConnection, Context.BIND_AUTO_CREATE);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopService(intent);
        finishAffinity();

    }

    private ServiceConnection myConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {

            MyService.LocalBinder binder = (MyService.LocalBinder) service;
            myService = binder.getService();
            isBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            isBound = false;
        }
    };

    public void setTime() {
        time.setText("" + mins + ":" + String.format("%02d", secs) + ":"
                + String.format("%03d", millis));
    }

}

public class MyService extends Service {

    private boolean isRunning = false;
    private long startTime = 0;
    private long timeInMilliseconds = 0;
    private long timeSwapBuff = 0;
    private long updatedTime = 0;
    private final IBinder mBinder = new LocalBinder();
    private Message timeMsg;

    public MyService() { }

    public Runnable updateTimer = new Runnable() {
        public void run() {
            timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
            updatedTime = timeSwapBuff + timeInMilliseconds;
            Log.d("Czas:", String.valueOf(updatedTime));

            timeMsg = new Message();
            timeMsg.obj = updatedTime;
            MainActivity.sHandler.sendMessage(timeMsg);

            MainActivity.sHandler.postDelayed(this, 0);
        }
    };

    @Override
    public void onCreate(){
        super.onCreate();

    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }


    public void startStop(){

        if (isRunning) {
            timeSwapBuff += timeInMilliseconds;
            MainActivity.sHandler.removeCallbacks(updateTimer);
            isRunning = false;
        } else {
            startTime = SystemClock.uptimeMillis();
            MainActivity.sHandler.postDelayed(updateTimer, 0);
            isRunning = true;
        }
    }


    public void reset(){

        MainActivity.sHandler.removeCallbacks(updateTimer);
        isRunning=false;
        startTime = 0L;
        timeInMilliseconds = 0L;
        timeSwapBuff = 0L;
        updatedTime = 0L;

        timeMsg = new Message();
        timeMsg.obj = updatedTime;
        MainActivity.sHandler.sendMessage(timeMsg);

    }

    public class LocalBinder extends Binder {
        public MyService getService(){
            return MyService.this;
        }
    }


}

和GitHub链接:https://github.com/zelaznymarek/stoper

1 个答案:

答案 0 :(得分:0)

1)从服务类扩展您的服务,就像

一样
public class AlarmService extends Service {...

2)返回您的服务STICKY,如

        return Service.START_STICKY;

3)使用CountDownTimer或Timer或Simple Tread Classes;