title: How to create a job with WorkManager when device is plugged in?
body: <p>I want to create a job that will be triggered when the device is plugged in, just like broadcast receiver for <code>ACTION_POWER_CONNECTED</code>.</p>
  
  <p>Here is the worker class:</p>
  
  <pre><code>public class ChargerWorker extends Worker {
  
  /* Constants */
      private static final String TAG = "ChargerWorker";
      private static final long TRIGGER_AGE = TimeUnit.MINUTES.toMillis(30);       // At least 30 min old.
  
      @NonNull
      @Override
      public Result doWork() {
          Log.e(TAG, "Power connection worker of Indoor/Outdoor lib.");
          IndoorOutdoorLogger.v(TAG, "Power connection worker of Indoor/Outdoor lib.");
          Context context = getApplicationContext();
  
          if (Conditions.isBatteryChargingOnAC(context)) {
              IndoorOutdoorLogger.d(context, TAG, "Power plugged and connected to AC.");
              Log.e(TAG, "Power plugged and connected to AC.");
  
              if (WiFiConnection.isWifiConnected(context) &amp;&amp; WiFiConnection.isCurrentNetworkIndoorRecognized(context)) {
                  // In this case an "upgrade" to the confidence level is possible.
                  // Only run when the last known detection is old enough.
                  DetectionResult latestResult = DetectionResult.loadFromCache(context);
                  if (!latestResult.isTimestampValid(TRIGGER_AGE)) {
                      IndoorOutdoorLogger.d(context, TAG, "AC power while connected to a recognized WiFi network, and last detection is old enough, starting detection.");
                      IndoorOutdoorClient client = new IndoorOutdoorClient(context, null);
                      client.startShortDetection(Trigger.POWER);
                  }
              }
          }
          return Result.SUCCESS;
      } }
  </code></pre>
  
  <p>This is how I initialize the work in my onCreate() method:</p>
  
  <pre><code>Constraints constraints = new Constraints.Builder().setRequiresCharging(true).build();
          OneTimeWorkRequest simpleReuquest = new OneTimeWorkRequest.Builder(ChargerWorker.class)
                  .setConstraints(constraints)
                  .build();
          WorkManager.getInstance().enqueue(simpleReuquest);
  </code></pre>
  
  <p>When I connect the device to the adapter for the first time, everything works as it should. However when I disconnect the device, and try again, I never reach the doWork function again.
  Instead I see the following message in the logcat:</p>
  
  <pre><code>E/WorkerWrapper: Status for 031e39f1-bc10-4a35-9341-11453fc0ca21 is SUCCEEDED; not doing any work.
  </code></pre>
  
  <p>Is it because I use <code>OneTimeWorkRequest</code>? If so how can I schedule it to run every time the device is connected to a power source?</p>