在android.googlesource.com上测试了“显著运动”示例以了解有关此触发器传感器的更多信息。不幸的是,该传感器未触发。 我做错了什么?
代码片段
class TriggerListener extends TriggerEventListener {
private Context mContext;
private TextView mTextView;
TriggerListener(Context context, TextView textView) {
mContext = context;
mTextView = textView;
}
@Override
public void onTrigger(TriggerEvent event) {
if (event.values[0] == 1) {
mTextView.append(mContext.getString(R.string.sig_motion) + "\n");
mTextView.append(mContext.getString(R.string.sig_motion_auto_disabled) + "\n");
}
}
}
public class MainActivity extends AppCompatActivity {
private SensorManager mSensorManager;
private Sensor mSigMotion;
private TriggerListener mListener;
private TextView mTextView;
@Override
protected void onPause() {
super.onPause();
if (mSigMotion != null) mSensorManager.cancelTriggerSensor(mListener, mSigMotion);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mSigMotion = mSensorManager.getDefaultSensor(Sensor.TYPE_SIGNIFICANT_MOTION);
mTextView = (TextView)findViewById(R.id.text);
mListener = new TriggerListener(this, mTextView);
if (mSigMotion == null) {
mTextView.append(getString(R.string.no_sig_motion) + "\n");
}
}
感谢您的帮助。