2013-03-12 36 views
0

我知道這聽起來很奇怪,但我創建了一個帶有活動和服務(啓動和綁定)的簡單計時器。只有當手機連接到PC時,android活動/服務纔有效

在活動中我還實施了在onStart和的onStop只是記錄消息(Log.d(TAG,「活動啓動/停止」)。

事實是,如果手機連接到PC的一切似乎我可以啓動計時器,暫停它,修改並重新啓動它,打開其他應用程序,它會繼續在後臺工作,我可以回想起來,並且我看到實際的倒計時正在進行,如果完成,我可以從一個通知,並停止振鈴等等等

如果手機它從PC分離,它的工作原理就像沒有服務,所以活動運行,如果我按主頁按鈕它會在背景和保持工作國王停了幾分鐘。

我可以在「正在運行的應用程序」中看到該進程,如果我記得它從暫停點重新開始的活動。也就是說,我設置了10分鐘,我點擊開始,然後點擊主頁按鈕。 2-3分鐘後停止工作,如果我記得活動從8-7分鐘繼續倒計時... ...

任何想法?

活動:

package com.sleone.cookingtimer; 

進口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因爲被初始化綁定到服務

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; 

進口android.app.Service; import android.content.Intent; import android.os.Binder; import android.os。的IBinder;

public class TimerService extends Service { //給客戶端的綁定器 private final IBinder mBinder = new LocalBinder(); 私人CookingTimer定時器; // 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; 
} 

}

我不認爲你需要的定時器本身。它只是一個CountDownTimer,它可以更新小時/分鐘/秒的速度,並在onFinish播放聲音並創建通知。

+0

當你說「連接到PC」時,你的意思是說你有一個調試器運行? 「連接到PC」究竟是什麼意思? – 2013-03-12 14:15:15

+0

發佈您的代碼和堆棧跟蹤... – 2013-03-12 14:29:51

+0

添加了代碼。 @DavidWasser嗯我有一個調試器?我正在使用eclipse。當然,我沒有設置任何調試器。月食會自動做些什麼嗎? – Segolas 2013-03-12 14:39:31

回答

0

您可能會遇到某種競爭條件,即連接到PC時執行速度稍慢,但未連接時,時序稍有不同,執行順序也會改變。沒有代碼很難說。

+0

我可以分享代碼,讓我編輯第一篇文章 – Segolas 2013-03-12 14:35:29

0

好吧,我想我想通了。

基本上我不明白,一個服務也可以暫停時,CPU進入睡眠。

所以,我的猜測是,當在模擬器上或連接電纜時,CPU不會進入睡眠狀態,因爲沒有電池消耗。

即使從CPU睡眠中喚醒應用程序,我也使用了AlarmManager.RTC_WAKEUP標誌的AlarmManger。

相關問題