即使应用程序未运行也要检查手机是否移动的目的,我搜索互联网寻求解决方案,但没有太多选择,所以我最终创建了一个服务如下,它的工作原理,但我想知道的是有没有有效的方法做同样的事情?
声明服务
<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);