【问题标题】:android activity/service works only if phone connected to pcandroid 活动/服务仅在手机连接到 pc 时才有效
【发布时间】:2013-03-12 14:11:23
【问题描述】:

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

在活动中,我还实现了onStartonStop,只是记录了一条消息(Log.d(TAG,"activity started/stopped")。

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

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

我可以在正在运行的应用程序中看到该过程,如果我回忆起该活动,它会从暂停点重新启动。也就是说,我设置了 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;
    }
}

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

【问题讨论】:

  • 当您说“已连接到PC”时,您的意思是您正在运行调试器吗? “连接到 PC”到底是什么意思?
  • 发布您的代码和堆栈跟踪...
  • 添加了代码。 @DavidWasser mmm 我有调试器吗?我正在使用日食。当然,我没有设置任何调试器。 eclipse会不会自动做点什么?
  • 伙计们,正如您所见,服务有一个指向活动的指针,以便更新 gui onTick()。可能是当活动本身停止时,服务无法再更新活动并开始等待?如果是这样,为什么它可以在模拟器和/或通过电缆连接到电脑的手机上工作?

标签: android service android-activity


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    好吧,我想我想通了。

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多