34
33

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2016-03-29

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版です

実装

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ほしい
34
33
5

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
34
33

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?