5

Tested the Significant Motion sample on android.googlesource.com to learn more on this Trigger sensor. Unfortunately the sensor does not trigger. What do I do wrong?

code snipes

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");
        }
    }

thanks for your help.

Frits Molenkamp
  • 175
  • 1
  • 10

1 Answers1

0

I was having same problem and I found

mSensorManager.getDefaultSensor(Sensor.TYPE_SIGNIFICANT_MOTION);

returned null. So maybe there is no Significant Motion Sensor in the device(or the virtual device). In following code from the sample

@Override
protected void onResume() {
    super.onResume();
    if (mSigMotion != null && mSensorManager.requestTriggerSensor(mListener, mSigMotion))
        mTextView.append(getString(R.string.sig_motion_enabled) + "\n");
 }

mSensorManager.requestTriggerSensor(mListener, mSigMotion)) is never executed.

Al117
  • 55
  • 1
  • 8
  • @AI117 thanks for your reply, In the code above, from android.googlesource.com, there should always be a comment in the mTextView, even if there is no Significant Motion Sensor. but I got no comment when running that code. – Frits Molenkamp Aug 19 '18 at 16:34