下面列出了android.hardware.SensorManager#unregisterListener ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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());
}
}
@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());
}
}
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));
}
@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();
}
@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;
}
@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;
}
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();
}
}
void stop() {
if (lightSensor != null) {
SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
sensorManager.unregisterListener(this);
cameraManager = null;
lightSensor = null;
}
}
void stop() {
if (lightSensor != null) {
SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
sensorManager.unregisterListener(this);
cameraManager = null;
lightSensor = null;
}
}
protected void unregisterSensor(Context context){
if (!mRegistered) return;
SensorManager mSensorManager = (SensorManager) context
.getSystemService(Context.SENSOR_SERVICE);
mSensorManager.unregisterListener(this);
mRegistered = false;
}
@Override
public void onDestroy() {
Log.i(TAG, "Unregistering pressure listener");
SensorManager sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
sm.unregisterListener(pressureListener);
super.onDestroy();
}
/**
* {@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();
}
}
}
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);
}
}
/**
* 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);
}
}
public void stop() {
if (lightSensor != null) {
SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
sensorManager.unregisterListener(this);
cameraManager = null;
lightSensor = null;
}
}
void stop() {
if (lightSensor != null) {
SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
sensorManager.unregisterListener(this);
cameraManager = null;
lightSensor = null;
}
}
public void stop() {
if (lightSensor != null) {
SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
sensorManager.unregisterListener(this);
cameraManager = null;
lightSensor = null;
}
}
@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();
}
@Override
protected void onStop() {
SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
sensorManager.unregisterListener(this);
queue.clear();
}
private void unregisterSelfAsSensorListener(final SensorManager pSensorManager, final int pType) {
final Sensor sensor = pSensorManager.getSensorList(pType).get(0);
pSensorManager.unregisterListener(this, sensor);
}