按两次电源按钮时举起一个祝酒词

时间:2014-01-22 08:03:27

标签: android android-broadcast android-toast

我要做的是,双击电源按钮将发出一个“发送消息”的祝酒词,无论屏幕是开还是关。我做的是,我已经记录了持续时间点击电源按钮&如果当前点击和之前点击持续时间的差异b / w小于1秒,那么它将提高吐司。但它的单击也提高了祝酒词。请帮帮我

1.MainActivity.java
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    BroadcastReceiver mReceiver = new CloseSystemDialogsIntentReceiver();
    registerReceiver(mReceiver, filter);
    }

@Override
    protected void onPause() {
        // when the screen is about to turn off
        if (CloseSystemDialogsIntentReceiver.wasScreenOn) {
            // this is the case when onPause() is called by the system due to a screen state change
            System.out.println("SCREEN TURNED OFF");

    } else {
        // this is when onPause() is called when the screen state has not changed
    }
    super.onPause();
}

@Override
protected void onResume() {
    // only when screen turns on
    if (!CloseSystemDialogsIntentReceiver.wasScreenOn) {
        // this is when onResume() is called due to a screen state change
        System.out.println("SCREEN TURNED ON");
    } else {
        // this is when onResume() is called when the screen state has not changed
    }
    super.onResume();
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_POWER) {
        // Do something here...
        Log.d("ONKEYDOWN", "ONKEYDOWN");
        event.startTracking(); // Needed to track long presses
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

@Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_POWER) {
        // Do something here...
        Log.d("onKeyLongPress", "ONKEYDOWN");
        return true;
    }
    return super.onKeyLongPress(keyCode, event);
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
        Log.d("dispatchKeyEvent", "ONKEYDOWN");

        return true;
    }

    return super.dispatchKeyEvent(event);
}
}

2.CloseSystemDialogsIntentReceiver.java

public class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {

public static boolean wasScreenOn = true;
static long prevTime=0;
static long currTime=0;
@Override
public void onReceive(Context context, Intent intent) {

    if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
        // do whatever you need to do here
        prevTime = System.currentTimeMillis();
        Log.d("CHECK IN RECIVER WHEN ON","CHECK IN RECIVER WHEN ON");
        wasScreenOn = false;
    } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
        // and do whatever you need to do here
        Log.d("CHECK IN RECIVER WHEN ON","CHECK IN RECIVER WHEN OFF");
        currTime = System.currentTimeMillis();
        wasScreenOn = true;
    }
    if ((currTime - prevTime) < 1000 && (currTime - prevTime)>-1000 ) {
        if ((currTime - prevTime) < 1000 ) {
            Toast.makeText(context, "double Clicked power button",
                    Toast.LENGTH_LONG).show();
            Log.e("eciver ", "double Clicked power button");
            currTime = 0;
            prevTime = 0;
        }
    }
   }
   }

2 个答案:

答案 0 :(得分:0)

您应该在onDestroy中取消注册广播接收器,其次应该在服务中处理这种系统事件,而不是在屏幕关闭时可以销毁的前台活动。您的活动可能会丢失所有状态,并在这些按下之间重新创建。

答案 1 :(得分:0)

像这样编辑你的代码:

public class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {

public static boolean wasScreenOn = true;
static long prevTime=0;
static long currTime=0;
@Override
public void onReceive(Context context, Intent intent) {

if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
    // do whatever you need to do here
    Log.d("CHECK IN RECIVER WHEN ON","CHECK IN RECIVER WHEN ON");
    wasScreenOn = false;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
    // and do whatever you need to do here
    Log.d("CHECK IN RECIVER WHEN ON","CHECK IN RECIVER WHEN OFF");
    wasScreenOn = true;
}

if (prevTime == 0) {
// power button first time pressed or after you double-pressed
    prevTime = System.currentTimeMillis();
} else if (((currTime = System.currentTimeMillis()) - prevTime) < 1000 ) {
// second press under 1s(double-pressed), reset prevTime
    Toast.makeText(context, "double Clicked power button",
        Toast.LENGTH_LONG).show();
    Log.e("eciver ", "double Clicked power button");
    prevTime = 0;
} else {
// second press over 1s, considered as first press for next checking
    prevTime = currTime;
}
}
}