代码之家  ›  专栏  ›  技术社区  ›  Florian Walther

IntentService不受模拟器上的睡眠或打盹影响

  •  0
  • Florian Walther  · 技术社区  · 6 年前

    我想测试一下关闭屏幕和打开瞌睡模式的效果 IntentService

    为了这个,我开始一个背景 意向服务 我不使用任何唤醒锁。然而,我的服务从未中断过。它一直跑啊跑啊 onHandleIntent 意向服务 应该被销毁。这一假设不正确吗? 意向服务 不受影响?

    public class ExampleService extends IntentService {
        public static final String TAG = "IntentService";
    
        public ExampleService() {
            super("bla");
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
            Log.d(TAG, "onCreate");
        }
    
        @Override
        protected void onHandleIntent(@Nullable Intent intent) {
            Log.d(TAG, "onHandleIntent");
            String input = intent.getStringExtra("inputExtra");
    
            runInBackground(input);
        }
    
    
        private void runInBackground(String input) {
            for (int i = 0; i < 40; i++) {
                Log.d(TAG, input + " " + i);
                SystemClock.sleep(1000);
            }
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
            Log.d(TAG, "onDestroy");
        }
    }
    

    我开始了 意向服务 从我的 MainActivity 多次,所以它运行了大约 :

    public void startService(View v) {
        String input = editTextInput.getText().toString();
    
        Intent serviceIntent = new Intent(this, ExampleService.class);
        serviceIntent.putExtra("inputExtra", input);
    
        startService(serviceIntent);
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Onix    6 年前

    我的模拟器没有正确模拟睡眠/打盹,或者为什么

    你的仿真器似乎没有变成打瞌睡模式。

    应该销毁IntentService

    尝试以下步骤:

    1. 确保使用android 6或更高版本配置emulator
    2. 使用ADB命令启用打瞌睡模式

    请参阅有关“打瞌睡模式”和“待机”条件的官方文档 https://developer.android.com/training/monitoring-device-state/doze-standby