首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >只有当电话连接到pc时,android活动/服务才能工作。

只有当电话连接到pc时,android活动/服务才能工作。
EN

Stack Overflow用户
提问于 2013-03-12 14:11:23
回答 2查看 291关注 0票数 0

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

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

事实是,如果电话连接到个人电脑,一切似乎都正常。我可以启动计时器,暂停它,修改并重新启动它。打开其他应用程序,它就会在后台继续工作。我能回忆起来,我看到了真正的倒计时。如果它结束了,我可以从通知中回忆起活动,并停止铃声。等

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

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

有什么想法吗?

活动:

代码语言:javascript
复制
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!!");
        }
    };
}

服务:

代码语言:javascript
复制
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

EN

回答 2

Stack Overflow用户

发布于 2013-03-12 14:26:15

您可能有某种争用条件,当连接到PC时,执行速度会稍微慢一些,但是当没有连接时,时间会有点不同,执行顺序也会发生变化。没有密码就很难说了。

票数 0
EN

Stack Overflow用户

发布于 2013-03-18 09:33:34

好吧,我想我想明白了。

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

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15363567

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档