Android电源按钮双按

时间:2014-01-22 05:58:31

标签: 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

  3. public class CloseSystemDialogsIntentReceiver扩展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>0 && prevTime>0) {
            if ((currTime - prevTime) < 1000 && (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;
            }
        }
       }
       }
    
    1. Android Manifest File

      uses-permission android:name="android.permission.PREVENT_POWER_KEY" />

0 个答案:

没有答案