0

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

我有一个带有 onCreate 方法的HomeActivity.java

...
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 的实例,但没有跟踪点击。

4

1 回答 1

0

我最终跟踪了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();
            }
        }
    }
于 2015-02-11T00:14:01.317 回答