gpt4 book ai didi

Android-检测手机何时翻转

转载 作者:行者123 更新时间:2023-11-29 14:39:11 24 4
gpt4 key购买 nike

我想检测手机何时翻转,即手机屏幕朝下,手机背面朝上。我该怎么做?

基本上我想检测手机何时翻转!

最佳答案

这是我的做法:

package com.example.accsensordemo.service;

import android.app.Service;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Vibrator;
import android.util.Log;

public class MotionControlService extends Service implements SensorEventListener {

private static final String TAG = "MotionControlService";

private float mGZ = 0;//gravity acceleration along the z axis
private int mEventCountSinceGZChanged = 0;
private static final int MAX_COUNT_GZ_CHANGE = 10;

@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

Log.d(TAG, "onStartCommand, Started: " + mStarted);

if (!mStarted) {
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

mSensorManager.registerListener(this,
mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_GAME);

mStarted = true;
}
return START_STICKY;
}

@Override
public void onSensorChanged(SensorEvent event) {
int type = event.sensor.getType();
if (type == Sensor.TYPE_ACCELEROMETER) {
float gz = event.values[2];
if (mGZ == 0) {
mGZ = gz;
} else {
if ((mGZ * gz) < 0) {
mEventCountSinceGZChanged++;
if (mEventCountSinceGZChanged == MAX_COUNT_GZ_CHANGE) {
mGZ = gz;
mEventCountSinceGZChanged = 0;
if (gz > 0) {
Log.d(TAG, "now screen is facing up.");
} else if (gz < 0) {
Log.d(TAG, "now screen is facing down.");
}
}
} else {
if (mEventCountSinceGZChanged > 0) {
mGZ = gz;
mEventCountSinceGZChanged = 0;
}
}
}
}
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
}

关于Android-检测手机何时翻转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17774070/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com