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

The following examples show how to use android.hardware.SensorManager#unregisterListener() . 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: SensorListener.java    From Pedometer with Apache License 2.0 6 votes vote down vote up
private void reRegisterSensor() {
    if (BuildConfig.DEBUG) Logger.log("re-register sensor listener");
    SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
    try {
        sm.unregisterListener(this);
    } catch (Exception e) {
        if (BuildConfig.DEBUG) Logger.log(e);
        e.printStackTrace();
    }

    if (BuildConfig.DEBUG) {
        Logger.log("step sensors: " + sm.getSensorList(Sensor.TYPE_STEP_COUNTER).size());
        if (sm.getSensorList(Sensor.TYPE_STEP_COUNTER).size() < 1) return; // emulator
        Logger.log("default: " + sm.getDefaultSensor(Sensor.TYPE_STEP_COUNTER).getName());
    }

    // enable batching with delay of max 5 min
    sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_STEP_COUNTER),
            SensorManager.SENSOR_DELAY_NORMAL, (int) (5 * MICROSECONDS_IN_ONE_MINUTE));
}
 
Example 2
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 3
Source File: AndroidGyroscopeWrapper.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_GYROSCOPE);
	updateWrapperInfo();
	try {
		if (dcDuration > 0){
			mSensorManager.registerListener(this, mSensor,60000); //around 16Hz 
			Thread.sleep(dcDuration*1000);
			mSensorManager.unregisterListener(this);
		}
	}
	catch (InterruptedException e) {
		Log.e(e.getMessage(), e.toString());
	}
}
 
Example 4
Source File: MotionStrategy.java    From Beginner-Level-Android-Studio-Apps with GNU General Public License v3.0 5 votes vote down vote up
protected void unregisterSensor(Context context){
    if (!mRegistered) return;

    SensorManager mSensorManager = (SensorManager) context
            .getSystemService(Context.SENSOR_SERVICE);
    mSensorManager.unregisterListener(this);

    mRegistered = false;
}
 
Example 5
Source File: AmbientLightManager.java    From zxingfragmentlib with Apache License 2.0 5 votes vote down vote up
public void stop() {
  if (lightSensor != null) {
    SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
    sensorManager.unregisterListener(this);
    cameraManager = null;
    lightSensor = null;
  }
}
 
Example 6
Source File: AmbientLightManager.java    From ZXing-Standalone-library with Apache License 2.0 5 votes vote down vote up
void stop() {
  if (lightSensor != null) {
    SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
    sensorManager.unregisterListener(this);
    cameraManager = null;
    lightSensor = null;
  }
}
 
Example 7
Source File: AmbientLightManager.java    From moVirt with Apache License 2.0 5 votes vote down vote up
public void stop() {
    if (lightSensor != null) {
        SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
        sensorManager.unregisterListener(this);
        cameraManager = null;
        lightSensor = null;
    }
}
 
Example 8
Source File: SensorStepService.java    From StepSensor with MIT License 5 votes vote down vote up
/**
 * Allows to unregister 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 unregisterSensorStep() {
    if (mContext != null) {
        Log.d(TAG, "Unregister sensor listener");
        SensorManager sm = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);
        sm.unregisterListener(this);
        stopForeground(true);
    }
}
 
Example 9
Source File: ReaderActivity.java    From BookyMcBookface with GNU General Public License v3.0 5 votes vote down vote up
private void unlistenLight() {
    try {
        if (lightSensorListener != null) {
            SensorManager sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
            sensorManager.unregisterListener(lightSensorListener);
            lightSensorListener = null;
        }
    }  catch (Throwable t) {
        Log.e(TAG, t.getMessage(), t);
    }
}
 
Example 10
Source File: SensorsActivity.java    From android-motion-detector with Apache License 2.0 5 votes vote down vote up
/**
 * {@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();
        }
    }
}
 
Example 11
Source File: PressureService.java    From BackPackTrackII with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onDestroy() {
    Log.i(TAG, "Unregistering pressure listener");
    SensorManager sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    sm.unregisterListener(pressureListener);

    super.onDestroy();
}
 
Example 12
Source File: AmbientLightManager.java    From Study_Android_Demo with Apache License 2.0 5 votes vote down vote up
void stop() {
  if (lightSensor != null) {
    SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
    sensorManager.unregisterListener(this);
    cameraManager = null;
    lightSensor = null;
  }
}
 
Example 13
Source File: AmbientLightManager.java    From weex with Apache License 2.0 5 votes vote down vote up
void stop() {
  if (lightSensor != null) {
    SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
    sensorManager.unregisterListener(this);
    cameraManager = null;
    lightSensor = null;
  }
}
 
Example 14
Source File: SensorDetector.java    From pandora with Apache License 2.0 5 votes vote down vote up
public void unRegister() {
    try {
        SensorManager manager = (SensorManager) Utils.getContext().getSystemService(Context.SENSOR_SERVICE);
        Sensor sensor = manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        manager.unregisterListener(this, sensor);
    } catch (Throwable t) {
        t.printStackTrace();
    }
}
 
Example 15
Source File: SensorsTester.java    From PermissionAgent with Apache License 2.0 5 votes vote down vote up
@Override
public boolean test() throws Throwable {
    SensorManager sensorManager = (SensorManager)mContext.getSystemService(Context.SENSOR_SERVICE);
    try {
        Sensor heartRateSensor = sensorManager.getDefaultSensor(Sensor.TYPE_HEART_RATE);
        sensorManager.registerListener(SENSOR_EVENT_LISTENER, heartRateSensor, 3);
        sensorManager.unregisterListener(SENSOR_EVENT_LISTENER, heartRateSensor);
    } catch (Throwable e) {
        PackageManager packageManager = mContext.getPackageManager();
        return !packageManager.hasSystemFeature(PackageManager.FEATURE_SENSOR_HEART_RATE);
    }
    return true;
}
 
Example 16
Source File: SensorsTester.java    From PermissionAgent with Apache License 2.0 5 votes vote down vote up
@Override
public boolean test() throws Throwable {
    SensorManager sensorManager = (SensorManager)mContext.getSystemService(Context.SENSOR_SERVICE);
    try {
        Sensor heartRateSensor = sensorManager.getDefaultSensor(Sensor.TYPE_HEART_RATE);
        sensorManager.registerListener(SENSOR_EVENT_LISTENER, heartRateSensor, 3);
        sensorManager.unregisterListener(SENSOR_EVENT_LISTENER, heartRateSensor);
    } catch (Throwable e) {
        PackageManager packageManager = mContext.getPackageManager();
        return !packageManager.hasSystemFeature(PackageManager.FEATURE_SENSOR_HEART_RATE);
    }
    return true;
}
 
Example 17
Source File: StepCounterService.java    From BackPackTrackII with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onDestroy() {
    Log.w(TAG, "Unregistering step counter listener");
    SensorManager sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    sm.unregisterListener(mStepCounterListener);

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    prefs.edit().remove(SettingsFragment.PREF_LAST_STEP_COUNT).apply();

    super.onDestroy();
}
 
Example 18
Source File: VoIPBaseService.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void onDestroy() {
	if (BuildVars.LOGS_ENABLED) {
		FileLog.d("=============== VoIPService STOPPING ===============");
	}
	stopForeground(true);
	stopRinging();
	NotificationCenter.getInstance(currentAccount).removeObserver(this, NotificationCenter.appDidLogout);
	SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
	Sensor proximity = sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
	if (proximity != null) {
		sm.unregisterListener(this);
	}
	if (proximityWakelock != null && proximityWakelock.isHeld()) {
		proximityWakelock.release();
	}
	unregisterReceiver(receiver);
	if(timeoutRunnable!=null){
		AndroidUtilities.cancelRunOnUIThread(timeoutRunnable);
		timeoutRunnable=null;
	}
	super.onDestroy();
	sharedInstance = null;
	AndroidUtilities.runOnUIThread(new Runnable(){
		@Override
		public void run(){
			NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.didEndCall);
		}
	});
	if (tgVoip != null) {
		updateTrafficStats();
		StatsController.getInstance(currentAccount).incrementTotalCallsTime(getStatsNetworkType(), (int) (getCallDuration() / 1000) % 5);
		onTgVoipPreStop();
		onTgVoipStop(tgVoip.stop());
		prevTrafficStats = null;
		callStartTime = 0;
		tgVoip = null;
	}
	cpuWakelock.release();
	AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
	if(!USE_CONNECTION_SERVICE){
		if(isBtHeadsetConnected && !playingSound){
			am.stopBluetoothSco();
			am.setBluetoothScoOn(false);
			am.setSpeakerphoneOn(false);
		}
		try{
			am.setMode(AudioManager.MODE_NORMAL);
		}catch(SecurityException x){
			if(BuildVars.LOGS_ENABLED){
				FileLog.e("Error setting audio more to normal", x);
			}
		}
		am.abandonAudioFocus(this);
	}
	am.unregisterMediaButtonEventReceiver(new ComponentName(this, VoIPMediaButtonReceiver.class));
	if (haveAudioFocus)
		am.abandonAudioFocus(this);

	if (!playingSound)
		soundPool.release();

	if(USE_CONNECTION_SERVICE){
		if(!didDeleteConnectionServiceContact)
			ContactsController.getInstance(currentAccount).deleteConnectionServiceContact();
		if(systemCallConnection!=null && !playingSound){
			systemCallConnection.destroy();
		}
	}

	ConnectionsManager.getInstance(currentAccount).setAppPaused(true, false);
	VoIPHelper.lastCallTime=System.currentTimeMillis();
}
 
Example 19
Source File: Engine.java    From 30-android-libraries-in-30-days with Apache License 2.0 4 votes vote down vote up
private void unregisterSelfAsSensorListener(final SensorManager pSensorManager, final int pType) {
	final Sensor sensor = pSensorManager.getSensorList(pType).get(0);
	pSensorManager.unregisterListener(this, sensor);
}
 
Example 20
Source File: ShakeEventWire.java    From tinybus with Apache License 2.0 4 votes vote down vote up
@Override
protected void onStop() {
	SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
	sensorManager.unregisterListener(this);
	queue.clear();
}