2015-02-08 84 views
0

我的源代碼是在這裏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"); 
    } 
... 

我試圖找出如何我可以配合的條件,其中包觸發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; 
} 
} 

我曾嘗試創建MultiClickEvent的一個實例到HomeActivity但沒有跟蹤點擊次數。

+1

所有貴'HWReceiver'受到硬件點擊?還應避免使用'System.currentTimeMillis()'並使用'SystemClock.elapsedRealtime()',這樣即使設備處於休眠狀態,時間仍然會被計數。 – initramfs 2015-02-08 06:12:59

+0

是的,通過按實際電話上的電源按鈕觸發HWReceiver。我想在** Gear Fit **上按下應用標籤並將其綁定到相同的電源按鈕邏輯。目前,** HomeActivity **'bundle!= null'會在點擊標籤時觸發日誌。 – 2015-02-08 13:55:43

+0

我建議你在if語句中調用所有條件的狀態(在調用'registerClick()'之前的部分來查看它們中的任何一個是否不符合你的預期。 – initramfs 2015-02-08 13:59:10

回答

0

我結束了跟蹤上齒輪飛度應用的點擊次數,然後發送警報內的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(); 
      } 
     } 
    }