我知道这听起来很奇怪,但我创建了一个带有活动和服务(启动和绑定)的简单计时器。只有当手机连接到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播放声音并创建通知。
当你说“连接到PC”时,你的意思是说你有一个调试器运行? “连接到PC”究竟是什么意思? – 2013-03-12 14:15:15
发布您的代码和堆栈跟踪... – 2013-03-12 14:29:51
添加了代码。 @DavidWasser嗯我有一个调试器?我正在使用eclipse。当然,我没有设置任何调试器。月食会自动做些什么吗? – Segolas 2013-03-12 14:39:31