Java Code Examples for android.hardware.SensorManager#registerListener()

The following examples show how to use android.hardware.SensorManager#registerListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AndroidSensor.java    From ssj with GNU General Public License v3.0 7 votes vote down vote up
/**
*
   */
  @Override
  protected boolean connect() throws SSJFatalException
  {
      manager = (SensorManager) SSJApplication.getAppContext().getSystemService(Context.SENSOR_SERVICE);

      boolean ok = true;
      for(SensorListener l : listeners)
      {
          Sensor s = manager.getDefaultSensor(l.getType().getType());
          if (s == null)
          {
              Log.e(l.getType().getName() + " not found on device");
              ok = false;
          }
          else
          {
              ok &= manager.registerListener(l, s, options.sensorDelay.get());
          }
      }

      return ok;
  }
 
Example 2
Source File: ProximitySensor.java    From AcDisplay with GNU General Public License v2.0 7 votes vote down vote up
@Override
public void onStart(@NonNull SensorManager sensorManager) {
    if (DEBUG) Log.d(TAG, "Starting proximity sensor...");

    mHistory.clear();
    mHistory.add(new Event(false, getTimeNow()));

    Config.getInstance().registerListener(this);
    updateWave2WakeProgram();

    // Ignore pocket program's start delay,
    // so app can act just after it has started.
    mFirstChange = true;
    mPocketProgram.dataArray[0].timeMin = 0;

    Sensor proximitySensor = sensorManager.getDefaultSensor(getType());
    sensorManager.registerListener(this, proximitySensor, SensorManager.SENSOR_DELAY_NORMAL);

    mMaximumRange = proximitySensor.getMaximumRange();
    sAttached = true;
}
 
Example 3
Source File: MotionStrategy.java    From Beginner-Level-Android-Studio-Apps with GNU General Public License v3.0 6 votes vote down vote up
protected void registerSensor(Context context){
    if (mRegistered) return;

    SensorManager mSensorManager = (SensorManager) context
            .getSystemService(Context.SENSOR_SERVICE);
    Sensor sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);

    if (sensor == null){
        Log.e(TAG,"TYPE_ROTATION_VECTOR sensor not support!");
        return;
    }

    mSensorManager.registerListener(this, sensor, getParams().mMotionDelay, MDMainHandler.sharedHandler());

    mRegistered = true;
}
 
Example 4
Source File: AmbientLightMonitor.java    From haven with GNU General Public License v3.0 6 votes vote down vote up
public AmbientLightMonitor(Context context) {
    prefs = new PreferenceManager(context);

    context.bindService(new Intent(context,
            MonitorService.class), mConnection, Context.BIND_ABOVE_CLIENT);

    sensorMgr = (SensorManager) context.getSystemService(AppCompatActivity.SENSOR_SERVICE);
    //noinspection RedundantCast
    sensor = (Sensor) sensorMgr.getDefaultSensor(Sensor.TYPE_LIGHT);

    if (sensor == null) {
        Log.i("AccelerometerFrament", "Warning: no accelerometer");
    } else {
        sensorMgr.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
    }

}
 
Example 5
Source File: MainActivity.java    From codeexamples-android with Eclipse Public License 1.0 6 votes vote down vote up
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	compassView = new MyCompassView(this);
	setContentView(compassView);

	sensorService = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
	sensor = sensorService.getDefaultSensor(Sensor.TYPE_ORIENTATION);
	if (sensor != null) {
		sensorService.registerListener(mySensorEventListener, sensor,
				SensorManager.SENSOR_DELAY_NORMAL);
		Log.i("Compass MainActivity", "Registerered for ORIENTATION Sensor");

	} else {
		Log.e("Compass MainActivity", "Registerered for ORIENTATION Sensor");
		Toast.makeText(this, "ORIENTATION Sensor not found",
				Toast.LENGTH_LONG).show();
		finish();
	}
}
 
Example 6
Source File: PocketProtector.java    From Huochexing12306 with Apache License 2.0 6 votes vote down vote up
@Override
public boolean start(Object obj) {
	try{
		mSensorManger = (SensorManager)getmServiceContext().getSystemService(Context.SENSOR_SERVICE);
		mSensor = mSensorManger.getDefaultSensor(Sensor.TYPE_PROXIMITY);
		mSensorListener = new MySensorListener();
		request(AntiTheftService.REQUEST_SHOW_MSG, getName() + "防护已开启,将在10秒后正式生效" + SF.TIP);
		mCDTimer = new CountDownTimer(10000, 1000) {
			
			@Override
			public void onTick(long millisUntilFinished) {
			}
			
			@Override
			public void onFinish() {
				mSensorManger.registerListener(mSensorListener, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
			}
		};
		setNeedDelay(true);
		mCDTimer.start();
		return true;
	}catch(Exception e){
		request(AntiTheftService.REQUEST_SHOW_MSG, "开启\"" + getName() + "\"防护失败,可能您的设备不支持距离传感器!");
		return false;
	}
}
 
Example 7
Source File: AndroidLightWrapper.java    From gsn with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void runOnce() {
	mSensorManager = (SensorManager) StaticData.globalContext.getSystemService(Context.SENSOR_SERVICE);
	mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
	updateWrapperInfo();
	try {
		if (dcDuration > 0){
			mSensorManager.registerListener(this, mSensor,SensorManager.SENSOR_DELAY_NORMAL); 
			Thread.sleep(dcDuration*1000);
			mSensorManager.unregisterListener(this);
		}
	}
	catch (InterruptedException e) {
		Log.e(e.getMessage(), e.toString());
	}
}
 
Example 8
Source File: CalibrationFragment.java    From prayer-times-android with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.calibration_dialog, container, false);
    mSensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);

    mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
    mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_GAME);
    mAccuracy = view.findViewById(R.id.accuracy_text);
    mAccuracy.setText(
            Html.fromHtml(String.format("%s: <font color='red'>%s</font>", getString(R.string.accuracy), getString(R.string.accuracy_low))));
    view.findViewById(R.id.close).setOnClickListener(v -> getParentFragment().getChildFragmentManager().beginTransaction().remove(CalibrationFragment.this).commit());
    return view;
}
 
Example 9
Source File: ShakeListener.java    From DroidDLNA with GNU General Public License v3.0 5 votes vote down vote up
public void start() {
	sensorManager = (SensorManager) context
			.getSystemService(Context.SENSOR_SERVICE);
	// 获得传感器管理器
	if (sensorManager != null) {
		// 获得重力传感器
		sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
	}
	// 注册
	if (sensor != null) {
		sensorManager.registerListener(this, sensor,
				SensorManager.SENSOR_DELAY_GAME);
	}
}
 
Example 10
Source File: AmbientLightManager.java    From barcodescanner-lib-aar with MIT License 5 votes vote down vote up
void start(CameraManager cameraManager) {
  this.cameraManager = cameraManager;
  SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
  if (FrontLightMode.readPref(sharedPrefs) == FrontLightMode.AUTO) {
    SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
    lightSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
    if (lightSensor != null) {
      sensorManager.registerListener(this, lightSensor, SensorManager.SENSOR_DELAY_NORMAL);
    }
  }
}
 
Example 11
Source File: StopByFacedownMgr.java    From prayer-times-android with Apache License 2.0 5 votes vote down vote up
public static void start(Context ctx, MyPlayer myPlayer) {
    try {
        SensorManager sensorManager = (SensorManager) ctx.getSystemService(Context.SENSOR_SERVICE);
        sensorManager.registerListener(new StopByFacedownMgr(sensorManager, myPlayer), sensorManager.getDefaultSensor(TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_UI);
    } catch (Exception e) {
        //do not crash, but report issue
        Crashlytics.logException(e);
    }
}
 
Example 12
Source File: BeaconRadarFragment.java    From BLE-Indoor-Positioning with Apache License 2.0 5 votes vote down vote up
@Override
public void onAttach(Context context) {
    super.onAttach(context);
    sensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
    sensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
    sensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_NORMAL);
}
 
Example 13
Source File: RageShake.java    From matrix-android-console with Apache License 2.0 5 votes vote down vote up
/**
 * start the sensor detector
 */
public void start(Context context) {
    mContext = context;

    SensorManager sm = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
    Sensor s = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    if (s == null) {
        Log.e(LOG_TAG, "No accelerometer in this device. Cannot use rage shake.");
        return;
    }
    sm.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);
}
 
Example 14
Source File: AmbientLightManager.java    From BarcodeEye with Apache License 2.0 5 votes vote down vote up
public void start(CameraManager cameraManager) {
    this.cameraManager = cameraManager;
    SharedPreferences sharedPrefs = PreferenceManager
            .getDefaultSharedPreferences(context);

    SensorManager sensorManager = (SensorManager) context
            .getSystemService(Context.SENSOR_SERVICE);
    lightSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
    if (lightSensor != null) {
        sensorManager.registerListener(this, lightSensor,
                SensorManager.SENSOR_DELAY_NORMAL);
    }
}
 
Example 15
Source File: Shake2Share.java    From AndroidLinkup with GNU General Public License v2.0 5 votes vote down vote up
private void startSensor() {
	mSensorManager = (SensorManager) activity.getSystemService(Context.SENSOR_SERVICE);
	if (mSensorManager == null) {
		throw new UnsupportedOperationException();
	}
	Sensor sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
	if (sensor == null) {
		throw new UnsupportedOperationException();
	}
	boolean success = mSensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_GAME);
	if (!success) {
		throw new UnsupportedOperationException();
	}
}
 
Example 16
Source File: MainActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.KITKAT_WATCH)
private void doConnectHeartRateSensor() {
  SensorManager sm = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
  Sensor heartRateSensor = sm.getDefaultSensor(Sensor.TYPE_HEART_RATE);
  if (heartRateSensor == null)
    Log.d(TAG, "No Heart Rate Sensor Detected.");
  else {
    sm.registerListener(mHeartRateListener, heartRateSensor,
      SensorManager.SENSOR_DELAY_NORMAL);
  }
}
 
Example 17
Source File: SensorStepService.java    From StepSensor with MIT License 5 votes vote down vote up
/**
 * Allows to register to the {@link android.hardware.Sensor#TYPE_STEP_COUNTER} for counting step
 * thanks to the hardware chip.
 * Must calls {@link com.orangegangsters.github.lib.SensorStepServiceManager#isStepCounterFeatureAvailable(android.content.pm.PackageManager)} before to
 * know if the feature is available.
 */
@TargetApi(Build.VERSION_CODES.KITKAT)
public void registerSensorStep() {
    if (mContext != null) {
        Log.d(TAG, "Register sensor listener");
        SensorManager sm = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);
        sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_STEP_COUNTER), SensorManager.SENSOR_DELAY_FASTEST);
    }
}
 
Example 18
Source File: VoIPBaseService.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
protected void configureDeviceForCall() {
	needPlayEndSound = true;
	AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
	if(!USE_CONNECTION_SERVICE){
		am.setMode(AudioManager.MODE_IN_COMMUNICATION);
		am.requestAudioFocus(this, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN);
		if(isBluetoothHeadsetConnected() && hasEarpiece()){
			switch(audioRouteToSet){
				case AUDIO_ROUTE_BLUETOOTH:
					if(!bluetoothScoActive){
						needSwitchToBluetoothAfterScoActivates=true;
						try {
							am.startBluetoothSco();
						} catch (Throwable ignore) {

						}
					}else{
						am.setBluetoothScoOn(true);
						am.setSpeakerphoneOn(false);
					}
					break;
				case AUDIO_ROUTE_EARPIECE:
					am.setBluetoothScoOn(false);
					am.setSpeakerphoneOn(false);
					break;
				case AUDIO_ROUTE_SPEAKER:
					am.setBluetoothScoOn(false);
					am.setSpeakerphoneOn(true);
					break;
			}
		}else if(isBluetoothHeadsetConnected()){
			am.setBluetoothScoOn(speakerphoneStateToSet);
		}else{
			am.setSpeakerphoneOn(speakerphoneStateToSet);
		}
	}/*else{
		if(isBluetoothHeadsetConnected() && hasEarpiece()){
			switch(audioRouteToSet){
				case AUDIO_ROUTE_BLUETOOTH:
					systemCallConnection.setAudioRoute(CallAudioState.ROUTE_BLUETOOTH);
					break;
				case AUDIO_ROUTE_EARPIECE:
					systemCallConnection.setAudioRoute(CallAudioState.ROUTE_WIRED_OR_EARPIECE);
					break;
				case AUDIO_ROUTE_SPEAKER:
					systemCallConnection.setAudioRoute(CallAudioState.ROUTE_SPEAKER);
					break;
			}
		}else{
			if(hasEarpiece())
				systemCallConnection.setAudioRoute(!speakerphoneStateToSet ? CallAudioState.ROUTE_WIRED_OR_EARPIECE : CallAudioState.ROUTE_SPEAKER);
			else
				systemCallConnection.setAudioRoute(!speakerphoneStateToSet ? CallAudioState.ROUTE_WIRED_OR_EARPIECE : CallAudioState.ROUTE_BLUETOOTH);
		}
	}*/
	updateOutputGainControlState();
	audioConfigured=true;

	SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
	Sensor proximity = sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
	try{
		if(proximity!=null){
			proximityWakelock=((PowerManager) getSystemService(Context.POWER_SERVICE)).newWakeLock(PROXIMITY_SCREEN_OFF_WAKE_LOCK, "telegram-voip-prx");
			sm.registerListener(this, proximity, SensorManager.SENSOR_DELAY_NORMAL);
		}
	}catch(Exception x){
		if (BuildVars.LOGS_ENABLED) {
			FileLog.e("Error initializing proximity sensor", x);
		}
	}
}
 
Example 19
Source File: MySensorService.java    From service with Apache License 2.0 4 votes vote down vote up
@Override
public void onCreate() {
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
 
Example 20
Source File: SensorEventListenerWrapper.java    From SensorAnnotations with Apache License 2.0 4 votes vote down vote up
public void registerListener(@NonNull SensorManager sensorManager) {
    sensorManager.registerListener(getEventListener(), getSensor(sensorManager), mDelay);
}