服务是否可能/允许同时成为 SensorEventListener?
我想要做的是运行一个后台服务,当传感器值发生变化时它会做一些事情。我可以看到说明服务启动和停止的 Toast 消息,但是当我认为传感器值发生变化时,我没有收到说明传感器已激活的 Toast。
这就是我所拥有的:
public class MyService extends Service implements SensorEventListener
{
private SensorManager mSensorManager;
private Sensor mSensor;
public void onCreate()
{
Toast.makeText(this,"Service Created", Toast.LENGTH_SHORT).show();
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
}
public void onStart(Intent intent, int startId)
{
Toast.makeText(this, "Service started", Toast.LENGTH_SHORT).show();
}
public void onDestroy()
{
super.onDestroy();
Toast.makeText(this,"Service DESTROYED!!", Toast.LENGTH_SHORT).show();
}
public void onAccuracyChanged(Sensor sensor, int accuracy)
{
//do nothing
}
public void onSensorChanged(SensorEvent event)
{
Toast.makeText(this,"Sensor active", Toast.LENGTH_SHORT).show();
}
}