[Android]部屋の灯りが消えたら自動でGet Wildを再生してGet Wild退勤する

More than 1 year has passed since last update.

device-2016-03-29-205128.png

https://twitter.com/kozeni_shkt/status/709743397196541953
http://www.b-ch.com/ttl/index.php?ttl_c=467

[iOS]部屋の灯りが消えたら自動でGet Wildを再生してGet Wild退勤するのAndroid版です

実装

https://github.com/napplecomputer/GetWild

public class MainActivity extends AppCompatActivity {
  private int thresholdValue;
  private float tmpValue;
  private TextView currentBrightness;
  private TextView threshold;

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();
  }

  @Override protected void onStart() {
    super.onStart();
    registerLightSensor();
  }

  @Override protected void onStop() {
    unregisterLightSensor();
    super.onStop();
  }

  private void initView() {
    currentBrightness = (TextView) findViewById(R.id.current_brightness);
    threshold = (TextView) findViewById(R.id.threshold);

    SeekBar seekBar = (SeekBar) findViewById(R.id.seek_bar);
    updateThreshold(seekBar.getProgress());
    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
      @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        updateThreshold(progress);
      }

      @Override public void onStartTrackingTouch(SeekBar seekBar) {
      }

      @Override public void onStopTrackingTouch(SeekBar seekBar) {
      }
    });
  }

  private void updateThreshold(int progress) {
    thresholdValue = progress * 5;
    threshold.setText(getString(R.string.threshold, thresholdValue));
  }

  private void registerLightSensor() {
    SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
    sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_NORMAL);
  }

  private void unregisterLightSensor() {
    ((SensorManager) getSystemService(SENSOR_SERVICE)).unregisterListener(listener);
  }

  private Intent getWildAndTough() {
    Intent intent = new Intent();
    intent.setAction(MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH);
    intent.putExtra(MediaStore.EXTRA_MEDIA_FOCUS, "vnd.android.cursor.item/audio");
    intent.putExtra(MediaStore.EXTRA_MEDIA_TITLE, "get wild");
    intent.putExtra(SearchManager.QUERY, "get wild");
    return intent;
  }

  private SensorEventListener listener = new SensorEventListener() {
    @Override public void onSensorChanged(SensorEvent event) {
      float value = event.values[0];
      if (tmpValue >= thresholdValue && value < thresholdValue) {
        startActivity(getWildAndTough());
      }
      tmpValue = value;
      currentBrightness.setText(getString(R.string.current_Brightness, tmpValue));
    }

    @Override public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }
  };
}

メモ

  • 対応している音楽再生アプリが複数あるとそこで止まってしまうので、常にそのアプリで動かすようにしておかないといけません
  • GetWildが複数あると選択させるUIが表示されて止まる可能性があります
  • 音楽再生アプリにUIが移ってしまうので、次の日、自分のアプリを立ち上げる必要があります
  • バックグラウンドで音楽再生するCommon Intentsほしい
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account log in.