0

我知道这听起来很奇怪,但我创建了一个简单的计时器,带有一个activity和一个service(启动和绑定)。

在活动中,我还实施onStartonStop仅记录一条消息(Log.d(TAG,“活动开始/停止”)。

事实是,如果手机连接到电脑,一切似乎都可以正常工作。我可以启动计时器,暂停它,修改并重新启动它。打开其他应用程序,它会继续在后台运行。我记得它,我看到实际的倒计时正在下降。如果它完成,我可以从通知中召回活动并停止响铃。等等等等

如果手机与电脑分离,它就像根本没有服务一样工作。所以活动运行,如果我按下主页按钮,它会在后台运行并继续工作几分钟而不是停止。

我可以在正在运行的应用程序中看到该进程,如果我回忆起该活动,它会从它暂停的点重新启动。也就是说,我设置了 10 分钟,我点击开始,然后点击主页按钮。2-3 分钟后它停止工作,如果我记得活动,它会继续从 8-7 分钟倒计时......

任何想法?

活动:

package com.sleone.cookingtimer;

import com.sleone.cookingtimer.TimerService.LocalBinder;

import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.NumericWheelAdapter;
import android.util.Log;

public class TimerMainActivity extends Activity {
    // private CookingTimer timer;
    // suppressWarnings because is initialized binding to the service

    private TimerService timerService;
    private Intent timerServiceIntent;
    private final String TAG = "TimerMainActivity";

    private WheelView hoursWheel ;
    private WheelView minutesWheel;
    private WheelView secondsWheel;

    /*
     * Initialize the activity
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_timer_main);

        timerServiceIntent = new Intent(this, TimerService.class);
        startTimerService();

        // init the gui
        hoursWheel = (WheelView) findViewById(R.id.hoursWheelView);
        minutesWheel = (WheelView) findViewById(R.id.minutesWheelView);
        secondsWheel = (WheelView) findViewById(R.id.secondsWheelView);
        hoursWheel.setViewAdapter(new NumericWheelAdapter(this, 0, 6));
        minutesWheel.setViewAdapter(new NumericWheelAdapter(this, 0, 59));
        secondsWheel.setViewAdapter(new NumericWheelAdapter(this, 0, 59));
    }

    @Override
    protected void onStop(){
        super.onStop();
        Log.d(TAG, "TimerMainActivity stopped");
    }

    @Override
    protected void onStart(){
        super.onStart();
        Log.d(TAG, "TimerMainActivity started");
    }

    private void startTimerService() {
        // connect to the service
        // leave the service in background
        Log.d(TAG, "Starting the TimerService");
        startService(timerServiceIntent);
        // interact with the service
        Log.d(TAG, "Binding to the TimerService");
        bindService(timerServiceIntent, mConnection, Context.BIND_AUTO_CREATE);
    }

    private void stopTimerService() {
        unbindService(mConnection);
        stopService(timerServiceIntent);
    }

    /*
     * Disconnect from the service
     */
    @Override
    protected void onDestroy() {
        Log.d(TAG, "Stopping TimerService");
        super.onStop();
        stopTimerService();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.timer_main, menu);
        return true;
    }

    public void controlTimer(View view) {
        Button controlButton = (Button) findViewById(R.id.controlTimerButton);

        if (controlButton.getText().equals(
                getResources().getText(R.string.startTimer))) {
            if ((hoursWheel.getCurrentItem() == 0)
                    && (minutesWheel.getCurrentItem() == 0)
                    && (secondsWheel.getCurrentItem() == 0)) {
                return;
            }
            controlButton.setText(R.string.stopTimer);
            timerService.startTimer();
        } else {
            controlButton.setText(R.string.startTimer);
            timerService.stopTimer();
        }

    }

    /* Defines callbacks for service binding, passed to bindService() */
    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
            LocalBinder binder = (LocalBinder) service;
            timerService = binder.getService();
            binder.createCookingTimer(TimerMainActivity.this);

            Log.d(TAG, "onServiceConnected() finished");
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            Log.e(TAG, "TimerService unexpectedly disconnected!!");
        }
    };
}

服务:

package com.sleone.cookingtimer;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

public class TimerService extends Service{
    // Binder given to clients
    private final IBinder mBinder = new LocalBinder();
    private CookingTimer timer;
    //private int timerServiceId;

    public class LocalBinder extends Binder {
        public TimerService getService() {
            // Return this instance of LocalService so clients can call public methods

            return TimerService.this;
        }

        // when the client connects to the service  instantiate the CookingImer
        public void createCookingTimer(TimerMainActivity timerMainActivity){
            timer = new CookingTimer(timerMainActivity);    
        }  
    }

    public void startTimer(){
        timer.startTimer();
    }

    public void stopTimer(){
        timer.stopTimer();
    }

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

我认为您不需要计时器本身。它只是一个CountDownTimeronTick更新小时/分钟/秒轮并onFinish播放声音并创建一个notification.

4

2 回答 2

0

好吧,我想我想通了。

基本上我不明白当cpu进入睡眠状态时服务也可以暂停。

所以,我的猜测是,在模拟器上或连接电缆时,cpu 永远不会进入睡眠状态,因为没有电池消耗。

为了从 CPU 睡眠中唤醒应用程序,我使用了带有 AlarmManager.RTC_WAKEUP 标志的 AlarmManger。

于 2013-03-18T09:33:34.213 回答
0

您可能会遇到某种竞争条件,即当连接到 PC 时,执行速度会慢一些,但是当未连接时,时间会有所不同,并且执行顺序会发生变化。没有代码很难说。

于 2013-03-12T14:26:15.597 回答