java - Broadcast Receiver for power button (screen state) doesn't work -
I use this code to start an activity when the power button is pressed 2 or more times but it Does not work.
My code for
Import android.content.BroadcastReceiver; Import android.content.Context; Import android.content.Intent; Import android.util.log; Import android.widget.Toast; Public Class Mayrecewer Expands Broadcast Receiver {Private Static Int Count Poweroff = 0; Public MyReceiver () {} public void on reverse (reference references, intentions) {if (intent.getAction (.) Equals (Intent.ACTION_SCREEN_OFF)) {log. ("In receipt", "in law: ACTION_SCREEN_OFF"); CountPowerOff ++; } And if (intent.getAction (.) Equals (Intent.ACTION_SCREEN_ON)) {Log.e ("in receipt", "in law: ACTION_SCREEN_ON"); } And if (intent.getAction (.) Equals (Intent.ACTION_USER_PRESENT)) {Log.e ("in receipt", "in law: ACTION_USER_PRESENT"); If (countPowerOff> = 2) {countPowerOff = 0; Toast. Make text (context, "Calling main activity", Toast. LNGH_LOG). Show (); Intent I = new intent (reference, about. Square); I.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); Context.startActivity (i); }}}}
and manifest file
:
& lt; Receiver Android: name = "MyReceiver" Android: Enabled = "true" />
You have created the receiver but have not registered it.
Register in the creative method of your activity for the receiver
@Override protected void onCreate () {// Initial Receiver Final IntentFilter filter = new IntentFilter (Intent.ACTION_SCREEN_ON) ; Filter.addAction (Intent.ACTION_SCREEN_OFF); Last broadcast receiver MRECRE = new MyCareover (); Register receiver (MREC, filter); }
Anyway you do not need a broadcast receiver for the Power button press event.
You can use this code
int i = 0; Override public boolean at @KeyDown (intccode, key event event) {if (keycode == KeyEvent.KEYCODE_POWER) {i ++; If (i == 2) {// you want to do something}} super return Kendown (keycode, incident); }
Comments
Post a Comment