android活动/服务只有在手机连接到pc时才有效
我知道这听起来很奇怪,但我创建了一个带有android活动/服务只有在手机连接到pc时才有效,android,service,android-activity,Android,Service,Android Activity,我知道这听起来很奇怪,但我创建了一个带有活动和服务(启动并绑定)的简单计时器 在活动中,我还实现了onStart和onStop只记录一条消息(Log.d(标签,“活动启动/停止”) 事实上,如果手机连接到电脑,一切似乎都正常。我可以启动计时器,暂停它,修改并重新启动它。打开其他应用程序,它会在后台继续工作。我可以回忆起它,我看到实际的倒计时正在进行。如果它完成,我可以通过通知回忆活动并停止铃声。等等 如果手机与pc分离,它的工作方式就像根本没有任何服务一样。因此,活动会运行,如果我按下home(
活动
和服务
(启动并绑定)的简单计时器
在活动中,我还实现了onStart
和onStop
只记录一条消息(Log.d(标签,“活动启动/停止”)
事实上,如果手机连接到电脑,一切似乎都正常。我可以启动计时器,暂停它,修改并重新启动它。打开其他应用程序,它会在后台继续工作。我可以回忆起它,我看到实际的倒计时正在进行。如果它完成,我可以通过通知回忆活动并停止铃声。等等
如果手机与pc分离,它的工作方式就像根本没有任何服务一样。因此,活动会运行,如果我按下home(主页)按钮,它会在后台继续工作几分钟,然后停止
我可以在运行中的应用程序中看到流程,如果我回忆起活动,它会从暂停点重新启动。也就是说,我设置了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;
}
}
我认为你不需要定时器本身。它只是一个
倒计时器onTick
它更新时/分/秒轮,并onFinish
播放声音并创建一个通知你可能有某种竞争条件,即当连接到电脑时,执行速度稍慢,但当未连接时间有点不同,执行顺序也会改变。没有代码很难判断。好的,我想我已经知道了
基本上,我并不完全理解,当cpu进入睡眠状态时,服务也会暂停
因此,我猜测,当在模拟器上或连接电缆时,cpu永远不会进入睡眠状态,因为没有电池消耗
为了从cpu睡眠中唤醒应用程序,我使用了带有AlarmManager.RTC_WAKEUP标志的AlarmManager。当你说“连接到电脑”时,你的意思是调试器正在运行吗?你说的“连接到电脑”到底是什么意思?发布您的代码和堆栈跟踪…添加了代码。@DavidWasser mmm我有调试器吗?我正在使用eclipse。当然我没有设置任何调试器。eclipse会自动执行一些操作吗?伙计们,正如您所看到的,服务有一个指向活动的指针,以便更新gui onTick()。可能是当活动本身停止时,服务无法再更新活动并开始等待?如果是,为什么它在模拟器上工作和/或在通过电缆连接到pc的手机上工作?