void registerSensorListeners() { if (config.useAccelerometer) { manager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); if (manager.getSensorList(Sensor.TYPE_ACCELEROMETER).size() == 0) { accelerometerAvailable = false; } else { Sensor accelerometer = manager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0); accelerometerListener = new SensorListener( this.nativeOrientation, this.accelerometerValues, this.magneticFieldValues); accelerometerAvailable = manager.registerListener( accelerometerListener, accelerometer, SensorManager.SENSOR_DELAY_GAME); } } else accelerometerAvailable = false; if (config.useCompass) { if (manager == null) manager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); Sensor sensor = manager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); if (sensor != null) { compassAvailable = accelerometerAvailable; if (compassAvailable) { compassListener = new SensorListener( this.nativeOrientation, this.accelerometerValues, this.magneticFieldValues); compassAvailable = manager.registerListener(compassListener, sensor, SensorManager.SENSOR_DELAY_GAME); } } else { compassAvailable = false; } } else compassAvailable = false; Gdx.app.log("AndroidInput", "sensor listener setup"); }
public void start() { // 模拟器下启动时键盘模拟重力 if (LSystem.isEmulator()) { _state._isConnected = true; sensorProcess = new SensorProcess(accelerometerValues); RealtimeProcessManager.get().addProcess(sensorProcess); return; } if (!_state._isConnected && manager == null) { manager = (SensorManager) LSystem.getActivity().getSystemService(Context.SENSOR_SERVICE); if (manager.getSensorList(Sensor.TYPE_ACCELEROMETER).size() == 0) { _state._isConnected = false; } else { Sensor accelerometer = manager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0); accelerometerListener = new SensorListener(this.accelerometerValues, this.magneticFieldValues); _state._isConnected = manager.registerListener( accelerometerListener, accelerometer, SensorManager.SENSOR_DELAY_GAME); } // 如果无法正常启动,则开启伪重力感应 if (!_state._isConnected) { _state._isConnected = true; sensorProcess = new SensorProcess(accelerometerValues); RealtimeProcessManager.get().addProcess(sensorProcess); } } }
/** {@inheritDoc} */ @Override public void onStart() { super.onStart(); try { sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE); sensors = sensorMgr.getSensorList(Sensor.TYPE_ACCELEROMETER); if (sensors.size() > 0) sensorGrav = sensors.get(0); sensors = sensorMgr.getSensorList(Sensor.TYPE_MAGNETIC_FIELD); if (sensors.size() > 0) sensorMag = sensors.get(0); sensorMgr.registerListener(this, sensorGrav, SensorManager.SENSOR_DELAY_NORMAL); sensorMgr.registerListener(this, sensorMag, SensorManager.SENSOR_DELAY_NORMAL); } catch (Exception ex1) { try { if (sensorMgr != null) { sensorMgr.unregisterListener(this, sensorGrav); sensorMgr.unregisterListener(this, sensorMag); sensorMgr = null; } } catch (Exception ex2) { ex2.printStackTrace(); } } }
public AccelerometerHandler(Context context) { SensorManager manager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); if (manager.getSensorList(Sensor.TYPE_ACCELEROMETER).size() != 0) { Sensor accelerometer = manager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0); manager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME); } }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sensors); listSensorsView = (TextView) findViewById(R.id.sensorsListTextView); Button buttonExit = (Button) findViewById(R.id.buttonSensorsExit); buttonExit.setOnClickListener(this); sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); sensorList = sensorManager.getSensorList(Sensor.TYPE_ALL); StringBuilder sensorDescriptions = new StringBuilder(); int count = 0; for (Sensor sensor : sensorList) { String sensorName = sensor.getName(); sensorDescriptions.append( count + ". " + sensorName + "\n" + " " + " Ver :" + sensor.getVersion() + " Range: " + sensor.getMaximumRange() + " Power: " + sensor.getPower() + " Res: " + sensor.getResolution()); sensorDescriptions.append("\n"); count++; } listSensorsView.setText(sensorDescriptions); }
/** * Check that if the PackageManager declares a sensor feature that the device has at least one * sensor that matches that feature. Also check that if a PackageManager does not declare a sensor * that the device also does not have such a sensor. * * @param featuresLeft to check in order to make sure the test covers all sensor features * @param expectedFeature that the PackageManager may report * @param expectedSensorType that that {@link SensorManager#getSensorList(int)} may have */ private void assertFeatureForSensor( Set<String> featuresLeft, String expectedFeature, int expectedSensorType) { assertTrue( "Features left " + featuresLeft + " to check did not include " + expectedFeature, featuresLeft.remove(expectedFeature)); boolean hasSensorFeature = mPackageManager.hasSystemFeature(expectedFeature); List<Sensor> sensors = mSensorManager.getSensorList(expectedSensorType); List<String> sensorNames = new ArrayList<String>(sensors.size()); for (Sensor sensor : sensors) { sensorNames.add(sensor.getName()); } boolean hasSensorType = !sensors.isEmpty(); String message = "PackageManager#hasSystemFeature(" + expectedFeature + ") returns " + hasSensorFeature + " but SensorManager#getSensorList(" + expectedSensorType + ") shows sensors " + sensorNames; assertEquals(message, hasSensorFeature, hasSensorType); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); textView = new TextView(this); setContentView(textView); SensorManager manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); if (manager.getSensorList(Sensor.TYPE_ACCELEROMETER).size() == 0) { textView.setText("No Accelerometer installed"); } else { Sensor accelerometer = manager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0); if (!manager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME)) { textView.setText("Couldn't register sensor listener"); } } }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sorry); // マナーモードを解除する SoundControl soundCon = new SoundControl(this); soundCon.setNormalRinger(); // センサーオブジェクト作成 sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); // 画面のロックを防ぐ this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); tv = (TextView) findViewById(R.id.count); // } // // //アクティビティが動き始めたらリスナーを登録する // public void onResume() { // super.onResume(); // カウントアップスタート startTimer(); // 加速度センサーリストを取得 List<Sensor> sensorList = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER); if (sensorList != null && !sensorList.isEmpty()) { Sensor sensor = sensorList.get(0); // リスナー登録(p163) sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_UI); } }
@Override public void init() { if (!pluginEnabled) { /* here should be your specific initialization code */ SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); frequency = prefs.getInt(EditActivity.PREF_FREQUENCY, 50); pluginId = prefs.getInt(EditActivity.KEY_PLUGIN_ID, -1); ignoreThreshold = EditActivity.getRate(frequency); prefs.registerOnSharedPreferenceChangeListener(this); // make sure not to call it twice sm = (SensorManager) getSystemService(SENSOR_SERVICE); List<Sensor> sensors = sm.getSensorList(Sensor.TYPE_ORIENTATION); if (sensors != null && sensors.size() > 0) { orientationSensor = sensors.get(0); sm.registerListener(this, orientationSensor, SensorManager.SENSOR_DELAY_UI); pluginEnabled = true; } else { Toast.makeText( this, "Accelerometer sensor is not available on this device!", Toast.LENGTH_SHORT) .show(); } } }
// metodo chamado no momento em que a view vai ser criada @TargetApi(Build.VERSION_CODES.M) @Override public void onViewCreated( final View view, Bundle savedInstanceState) { // chamado quando a view do fragment vai ser criada view.findViewById(R.id.picture) .setOnClickListener(this); // setando listeners nos btoes que estao sendo criados view.findViewById(R.id.info).setOnClickListener(this); view.findViewById(R.id.mudacam).setOnClickListener(this); view.findViewById(R.id.flash).setOnClickListener(this); view.findViewById(R.id.dividetela).setOnClickListener(this); textureView = (TextureView) view.findViewById(R.id.texture); // try { verificaCameras(); } catch (CameraAccessException e) { e.printStackTrace(); } cameraEscolhida = idCameras[0]; // inicia com primeira camera activity = getActivity(); manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE); verificaPreferenciasIndice(); sensoresEscolhios = getContext().getSharedPreferences(sensores, 0); Map<String, String> retorno = (Map<String, String>) sensoresEscolhios.getAll(); Collection<String> valores = retorno.values(); sm = (SensorManager) getContext().getSystemService(Context.SENSOR_SERVICE); List<Sensor> listaSensores = sm.getSensorList(Sensor.TYPE_ALL); for (String elem : valores) { for (Sensor aux : listaSensores) if (aux.getName().compareTo(elem) == 0) sens.add(aux); } ativaListenersSensores(sens); }
/* * (non-Javadoc) * * @see android.test.AndroidTestCase#setUp() */ protected void setUp() throws Exception { setContext(new DelegatingMockContext(getContext())); SensorManager sensorManager = (SensorManager) getContext().getSystemService(Context.SENSOR_SERVICE); isDeviceInGeneralAvailable = sensorManager.getSensorList(Sensor.TYPE_PROXIMITY).size() > 0; super.setUp(); }
/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // присвоили менеджеру работу с серсором mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); // создали список сенсоров для записи и сортировки List<Sensor> sensors = mSensorManager.getSensorList(Sensor.TYPE_ALL); // делаем проверку если больше нуля значит все хорошо и начинаем обрабатывать работу датчика if (sensors.size() > 0) { // форич для зацикливания работы, что бы не единожды выполнялось, а постоянно for (Sensor sensor : sensors) { // берем данные с акселерометра switch (sensor.getType()) { case Sensor.TYPE_ACCELEROMETER: // если пусто значит возвращаем значения сенсора if (mAccelerometerSensor == null) mAccelerometerSensor = sensor; break; default: break; } } } // привязываем наши объекты к нашей разметке mXValueText = (TextView) findViewById(R.id.value_x); mYValueText = (TextView) findViewById(R.id.value_y); mZValueText = (TextView) findViewById(R.id.value_z); }
public CompassHandler(Context context) { SensorManager manager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); if (manager.getSensorList(Sensor.TYPE_ORIENTATION).size() != 0) { Sensor compass = manager.getDefaultSensor(Sensor.TYPE_ORIENTATION); manager.registerListener(this, compass, SensorManager.SENSOR_DELAY_GAME); } }
@Override protected void onResume() { super.onResume(); SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE); List<Sensor> sensors = sm.getSensorList(Sensor.TYPE_ACCELEROMETER); if (sensors.size() > 0) { sm.registerListener(this, sensors.get(0), SensorManager.SENSOR_DELAY_GAME); } }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); sensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE); List<Sensor> deviceSensors = sensorManager.getSensorList(Sensor.TYPE_ALL); sensorAdapter = new SensorAdapter(getActivity().getApplicationContext(), deviceSensors); }
/** Returns true if at least one Orientation sensor is available */ public boolean isLightSensor() { boolean isLightSensor = false; SensorManager sm = (SensorManager) BibleApplication.getApplication().getSystemService(Context.SENSOR_SERVICE); if (sm != null) { List<Sensor> sensors = sm.getSensorList(Sensor.TYPE_LIGHT); isLightSensor = (sensors.size() > 0); } return isLightSensor; }
@Override public void onConnected() { sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); List<Sensor> sensorList = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER); if (sensorList.size() > 0) { accelerometer = sensorList.get(0); } else { Log.e(TAG, "No accelerometer found on device!"); } }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); mSensors = mSensorManager.getSensorList(Sensor.TYPE_ALL); ListView listview = (ListView) findViewById(R.id.listview); listview.setAdapter(new SensorListAdapter(this, android.R.layout.simple_list_item_1, mSensors)); }
@Kroll.setProperty @Kroll.method protected int[] getSensorList(int type) { List<Sensor> sensors = sensorManager.getSensorList(type); int[] list = new int[sensors.size()]; for (int i = 0; i < sensors.size(); i++) { Sensor sensor = sensors.get(i); list[i] = sensor.getType(); } return list; }
@Override public void onConnected() { SENSOR_NAME = "Light Sensor"; sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); List<Sensor> sensorList = sensorManager.getSensorList(Sensor.TYPE_LIGHT); if (sensorList.size() > 0) { lightSensor = sensorList.get(0); } else { Log.e(TAG, "No lightSensor found on device!"); } }
public static void startListening(AccelerometerListener accelerometerListener) { sensorManager = (SensorManager) RosbeeRemoteActivity.getContext().getSystemService(Context.SENSOR_SERVICE); List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER); if (sensors.size() > 0) { sensor = sensors.get(0); running = sensorManager.registerListener( sensorEventListener, sensor, SensorManager.SENSOR_DELAY_GAME); listener = accelerometerListener; } }
public ArrayList<Sensor> data() { ArrayList<Sensor> sensorList = new ArrayList<Sensor>(); SensorManager sensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE); List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ALL); for (Sensor sensor : sensors) { sensorList.add(sensor); } return sensorList; }
@Override public void onConnected() { SENSOR_NAME = "Light Sensor"; sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); List<Sensor> sensorList = sensorManager.getSensorList(Sensor.TYPE_LIGHT); if (sensorList.size() > 0) { lightSensor = sensorList.get(0); } else { Toast.makeText(getApplicationContext(), "No lightSensor found on device!", Toast.LENGTH_SHORT) .show(); Log.d(TAG, "No lightSensor found on device!"); } }
@Override public void onConnected() { SENSOR_NAME = "Movement Sensor"; sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); List<Sensor> sensorList = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER); if (sensorList.size() > 0) { accelerometer = sensorList.get(0); } else { Toast.makeText( getApplicationContext(), "No accelerometer found on device!", Toast.LENGTH_SHORT) .show(); Log.e(TAG, "No accelerometer found on device!"); } }
/** Returns true if at least one Accelerometer sensor is available */ public static boolean isSupported() { if (supported == null) { if (RosbeeRemoteActivity.getContext() != null) { sensorManager = (SensorManager) RosbeeRemoteActivity.getContext().getSystemService(Context.SENSOR_SERVICE); List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER); supported = new Boolean(sensors.size() > 0); } else { supported = Boolean.FALSE; } } return supported; }
/** * Device Orientation Profile<br> * イベントの登録. * * @param response レスポンス * @param serviceId サービスID * @param sessionKey セッションキー */ private void registerDeviceOrientationEvent( final Intent response, final String serviceId, final String sessionKey) { mServiceId = serviceId; mSensorManager = getSensorManager(); List<Sensor> sensors; sensors = mSensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER); if (sensors.size() > 0) { Sensor sensor = sensors.get(0); mSensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL); } else { MessageUtils.setNotSupportAttributeError(response); return; } sensors = mSensorManager.getSensorList(Sensor.TYPE_GRAVITY); if (sensors.size() > 0) { Sensor sensor = sensors.get(0); mSensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL); } else { MessageUtils.setNotSupportAttributeError(response); return; } sensors = mSensorManager.getSensorList(Sensor.TYPE_GYROSCOPE); if (sensors.size() > 0) { Sensor sensor = sensors.get(0); mSensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL); } else { MessageUtils.setNotSupportAttributeError(response); return; } DConnectProfile.setResult(response, DConnectMessage.RESULT_OK); response.putExtra(DConnectMessage.EXTRA_VALUE, "Register OnDeviceOrientation event"); }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 센서 매니저 객체 참조 manager = (SensorManager) getSystemService(SENSOR_SERVICE); // 모든 센서 객체 가져오기 sensors = manager.getSensorList(Sensor.TYPE_ALL); // 리스트에 어댑터 설정 adapter = new SensorListAdapter(this, R.layout.listitem, sensors); setListAdapter(adapter); }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); final BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter(); if (btAdapter != null) bluetoothDefaultIsEnable = btAdapter.isEnabled(); // get Orientation sensor List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ORIENTATION); if (sensors.size() > 0) orientSensor = sensors.get(0); else showDialog(NO_ORIENTATION_SENSOR); context = this.getApplicationContext(); // create a log instance for use by this application triplog = TripLog.getInstance(context); }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); List<Sensor> sensorList = mSensorManager.getSensorList(Sensor.TYPE_ALL); for (Sensor s : sensorList) { Log.d("sensor", "Name=" + s.getName()); Log.d("sensor", "Vendor=" + s.getVendor()); Log.d("sensor", "Version=" + s.getVersion()); Log.d("sensor", "MaximumRange=" + s.getMaximumRange()); Log.d("sensor", "MinDelay=" + s.getMinDelay()); Log.d("sensor", "Power=" + s.getPower()); Log.d("sensor", "Type=" + s.getType()); } }
/** Returns true if at least one Accelerometer sensor is available */ public static boolean isSupported(Context context) { aContext = context; if (supported == null) { if (aContext != null) { sensorManager = (SensorManager) aContext.getSystemService(Context.SENSOR_SERVICE); // Get all sensors in device List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER); supported = new Boolean(sensors.size() > 0); } else { supported = Boolean.FALSE; } } return supported; }