Android 如何在后台服务中运行传感器更改

Android 如何在后台服务中运行传感器更改,android,android-sensors,Android,Android Sensors,为了检查手机是否在应用程序未运行的情况下移动,我在互联网上搜索了解决方案,但没有太多选项,所以我最终创建了一个如下的服务,它可以正常工作,但我想知道是否有任何有效的方法可以做到这一点 申报服务 <service android:name=".CheckIdle"></service> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-feature andr

为了检查手机是否在应用程序未运行的情况下移动,我在互联网上搜索了解决方案,但没有太多选项,所以我最终创建了一个如下的服务,它可以正常工作,但我想知道是否有任何有效的方法可以做到这一点

申报服务

<service
    android:name=".CheckIdle"></service>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-feature android:name="android.hardware.sensor.accelerometer" />

我想,这项服务还可以。
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.IBinder;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;


public class CheckIdle extends Service implements SensorEventListener{
    WakeLock wakeLock;
    private SensorManager sensorManager;
    @Override
    public void onCreate() {

        super.onCreate();
        PowerManager pm = (PowerManager) getSystemService(this.POWER_SERVICE);

        wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "DoNotSleep");
        wakeLock.acquire();

        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        System.out.println("servcice created");

        sensorManager.registerListener(this,
                sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_NORMAL);

        Log.e("Google", "Service Created");
    }
    /**
     * Called when sensor values have changed.
     * <p>See {@link android.hardware.SensorManager SensorManager}
     * for details on possible sensor types.
     * <p>See also {@link android.hardware.SensorEvent SensorEvent}.
     * <p/>
     * <p><b>NOTE:</b> The application doesn't own the
     * {@link android.hardware.SensorEvent event}
     * object passed as a parameter and therefore cannot hold on to it.
     * The object may be part of an internal pool and may be reused by
     * the framework.
     *
     * @param event the {@link android.hardware.SensorEvent SensorEvent}.
     */
    @Override
    public void onSensorChanged(SensorEvent event) {
        System.out.println("sensor changing");
    }

    /**
     * Called when the accuracy of the registered sensor has changed.
     * <p/>
     * <p>See the SENSOR_STATUS_* constants in
     * {@link android.hardware.SensorManager SensorManager} for details.
     *
     * @param sensor
     * @param accuracy The new accuracy of this sensor, one of
     *                 {@code SensorManager.SENSOR_STATUS_*}
     */
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        System.out.println("sensor changing onAccuracyChanged");
    }

    /**
     * Return the communication channel to the service.  May return null if
     * clients can not bind to the service.  The returned
     * {@link android.os.IBinder} is usually for a complex interface
     * that has been <a href="{@docRoot}guide/components/aidl.html">described using
     * aidl</a>.
     * <p/>
     * <p><em>Note that unlike other application components, calls on to the
     * IBinder interface returned here may not happen on the main thread
     * of the process</em>.  More information about the main thread can be found in
     * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html">Processes and
     * Threads</a>.</p>
     *
     * @param intent The Intent that was used to bind to this service,
     *               as given to {@link android.content.Context#bindService
     *               Context.bindService}.  Note that any extras that were included with
     *               the Intent at that point will <em>not</em> be seen here.
     * @return Return an IBinder through which clients can call on to the
     * service.
     */
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


}
        //start the service
        /*
 * Creates a new Intent to start the CheckIdle
 * IntentService.
 */
        Intent mServiceIntent = new Intent(this, CheckIdle.class);
        // Starts the IntentService
        this.startService(mServiceIntent);