如何触发警报

How to trigger alert

我的源代码在这里https://github.com/jackygrahamez/MayDay

我有一个 HomeActivity.java 和一个 onCreate 方法

...
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.welcome_screen);

    Bundle bundle=getIntent().getExtras();
    boolean startedByCUP=false;
    if(bundle!=null) {
        Log.e(">>>>>>", "START_BY_CUP");
        startedByCUP = bundle.getBoolean("START_BY_CUP");
    }
...

我想弄清楚如何将 bundle 为 true 的条件与触发 multiClickEvent 联系起来,以便在点击 5 次后触发警报。硬件触发器内置于此

HardwareTriggerReceiver.java

   ...
   @Override
   public void onReceive(Context context, Intent intent) {
   Log.e(">>>>>>>", "in onReceive of HWReceiver");
   String action = intent.getAction();

   if (!isCallActive(context) && isScreenLocked(context) 
      && (action.equals(ACTION_SCREEN_OFF) ||           
      action.equals(ACTION_SCREEN_ON))) {
    multiClickEvent.registerClick(System.currentTimeMillis());
    if (multiClickEvent.isActivated()) {
        onActivation(context);
        resetEvent();
    }
}
} 
...

MultiClickEvent.java

package com.mayday.md.trigger;

import android.util.Log;

public class MultiClickEvent {
public static final int TIME_INTERVAL = 10000;
private static final int TOTAL_CLICKS = 5;

private Long firstEventTime;
private int clickCount = 0;

public void reset() {
    firstEventTime = null;
    clickCount = 0;
}

public void registerClick(Long eventTime) {
    if (isFirstClick() || notWithinLimit(eventTime)) {
        firstEventTime = eventTime;
        clickCount = 1;
        return;
    }
    clickCount++;
    Log.e(">>>>>>", "MultiClickEvent clickCount = " + clickCount);
}

private boolean notWithinLimit(long current) {
    return (current - firstEventTime) > TIME_INTERVAL;
}

private boolean isFirstClick() {
    return firstEventTime == null;
}

public boolean isActivated() {
    return clickCount >= TOTAL_CLICKS;
}
}

我已经尝试在 HomeActivity 中创建一个 MultiClickEvent 实例,但它没有跟踪点击。

我最终跟踪了 Gear Fit 应用程序上的点击,然后在 onCreate

中发送警报
        if(bundle!=null) {
        if (!hardwareTriggerReceiver.isCallActive(getApplicationContext())) {
            int c = mPref.getInt("numRun", 0);
            int TIME_INTERVAL = 10000;
            int TOTAL_CLICKS = 5;
            long delta = 0;
            Long eventTime = System.currentTimeMillis();
            mPref.edit().putLong("eventTime", eventTime).commit();
            Long firstEventTime = mPref.getLong("firstEventTime", 0);
            if (firstEventTime == 0) {
                firstEventTime = eventTime;
                mPref.edit().putLong("firstEventTime", firstEventTime).commit();
            }
            delta = eventTime - firstEventTime;
            Log.e(">>>>>>", "START_BY_CUP delta "+delta);
            if (delta < TIME_INTERVAL) {
                c++;
                mPref.edit().putInt("numRun",c).commit();
                Log.e(">>>>>>", "START_BY_CUP "+c);
                if (c >=TOTAL_CLICKS) {
                    hardwareTriggerReceiver.onActivation(getApplicationContext());
                    mPref.edit().putInt("numRun", 0).apply();
                    mPref.edit().putLong("firstEventTime", 0).apply();
                }
            } else {
                mPref.edit().putInt("numRun", 0).apply();
                mPref.edit().putLong("firstEventTime", 0).apply();
            }
        }
    }