0

我只是想知道这是实现唤醒锁定以使我的设备在进入睡眠模式后重新启动的正确方法。设备重新启动后,它将从该点启动一个意图。真的,我只是想让屏幕显示足够长的时间以实现意图。如果不是指针会很棒,我这样做是否正确!

public class GMeter extends Service implements SensorEventListener {

private float mLastX, mLastY, mLastZ;
private boolean mInitialized;
private SensorManager mSensorManager;
private Sensor mAccelerometer;
private final float NOISE = (float) 2.0;
private PowerManager.WakeLock wl;

/** Called when the activity is first created. */
public void onCreate() {
    mInitialized = false;
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mAccelerometer = mSensorManager
            .getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    mSensorManager.registerListener(this, mAccelerometer,
            SensorManager.SENSOR_DELAY_NORMAL);
    Toast.makeText(this, "Starting Service", Toast.LENGTH_LONG).show();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return START_STICKY;
}

@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
    // TODO Auto-generated method stub

}

@Override
public void onSensorChanged(SensorEvent event) {
    float x = event.values[0];
    float y = event.values[1];
    float z = event.values[2];
    double a = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
    double currentG = (a / SensorManager.STANDARD_GRAVITY);
    if (!mInitialized && currentG > 0.5) {
        AlertDialog alert = new AlertDialog.Builder(GMeter.this).create();
        alert.setTitle("-=Warning=-");
        alert.setMessage("Incoming Game!");
        alert.show();
        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP + PowerManager.FULL_WAKE_LOCK, "myTag");
        wl.acquire();
        wl.release();
        mLastX = x;
        mLastY = y;
        mLastZ = z;
        mInitialized = true;
        // Start Second Activity
        Intent intent = new Intent(GMeter.this, nextClass.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    } else {
        float deltaX = Math.abs(mLastX - x);
        float deltaY = Math.abs(mLastY - y);
        float deltaZ = Math.abs(mLastZ - z);
        if (deltaX < NOISE)
            deltaX = (float) 0.0;
        if (deltaY < NOISE)
            deltaY = (float) 0.0;
        if (deltaZ < NOISE)
            deltaZ = (float) 0.0;
        mLastX = x;
        mLastY = y;
        mLastZ = z;
    }
}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

}

4

1 回答 1

0

不,唤醒锁用于防止设备休眠。我猜你应该使用 AlarmManager 功能来唤醒你的设备并发送一个意图(带有参数RTC_WAKEUP)。

编辑:如果您想要您onCreate()的服务方法中的功能,您应该获取 PARTIAL_WAKE_LOCK 并且您应该在onDestroy(). 因此,您的服务将收到通知。或者您可以每隔 n 秒唤醒您的设备并检查您的传感器。

于 2012-09-28T20:52:18.273 回答