What i am trying to do is ,double clicking the power button will raise a toast "Sending Message", doesn't matter if screen is On or Off.What i have done is , i have recorded the time duration of the clicks on power button & If the difference b/w the current click and previous click duration is less than a 1sec, then it will raise the toast. But its raising the toast on the single click also. Please Help me out
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;
}
}
}
}
You should unregister the broadcast receiver in onDestroy and secondly this kind of system event should be handled in a Service not a foreground activity that can be destroyed when the screen goes off. Your activity can lose all state and get recreated between these presses.
Edit your code like this:
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;
}
}
}
Related
I have 4 activity:
FirstActivity - it shows in the first time of app running
MainActivity
SeconActivity
ThirdActivity
I want to show FirstActivity when Power Button clicked and user unlocked his/her phone. but how can i handle it that after showing FirstActivity BackgroundActivity shows again.
how can i do it?
for example in MainActivity:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
Intent checkingIntent=new Intent(getApplicationContext(),FirstActivity.class);
checkingIntent.putExtra("checking",true);
startActivity(checkingIntent);
return true;
}
return super.onKeyDown(keyCode, event);
}
and in FirstActivity
public boolean checking() {
checking_FOR_bankInfo= getIntent().getExtras().getBoolean("checking");
if (checking_FOR_bankInfo){
...
}
return cdo_state;
}
Try below code in every activity's onPause() method:
#Override
protected void onPause() {
super.onPause();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
boolean screenOn;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
screenOn = pm.isInteractive();
} else {
screenOn = pm.isScreenOn();
}
if (!screenOn) { //Screen off by lock or power
Intent checkingIntent = new Intent(this, FirstActivity.class);
checkingIntent.putExtra("checking",true);
checkingIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(checkingIntent);
finish();
}
}
#Override
protected void onStop()
{
super.onStop();
Log.d(tag, "MYonStop is called");
// insert here your instructions
// you can call new activity here.
}
when you press the button HOME the onStop() method is called.
I am trying capture power click in my android app, I was looking for any solution and I just find one with good results, It is when I detect screenOn/screenOff like this:
public class Receiver extends BroadcastReceiver {
private boolean screenOff;
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
screenOff = true;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
screenOff = false;
}
Intent i = new Intent(context, PowerButtonService.class);
i.putExtra("screen_state", screenOff);
context.startService(i);
}
}
The problem in this method is that in my app I am looking for do it with screen Off, then it is not util for me.
I tryed this code also in my MainActivity but this doesn't work, debuging it, this the execution go inside when I push vol up or down but not when I push power key:
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
Log.i("Main", "Dispath event power");
Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
sendBroadcast(closeDialog);
Log.d("DISPATCH", "boton pulsado");
return true;
}
return super.dispatchKeyEvent(event);
}
I have the same problem in this one:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_POWER) {
Log.w("Test", "Power button down detected");
return true;
}
return false;
}
Could someone help me? thanks
Edit: I want to catch short key press
Sorry for my english
I want to track volume key down when screen was off. How can I put my code in on receive method?
I want to turn on flashlight when user pressed volume down key and when screen goes off.
MyReceiver.java :
public class MyReceiver extends BroadcastReceiver {
private boolean screenOff;
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
screenOff = true;
// Log.i("via Receiver","Normal ScreenOFF" );
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
screenOff = false;
} else if(intent.getAction().equals(Intent.ACTION_ANSWER)) {
}
Intent i = new Intent(context, UpdateService.class);
i.putExtra("screen_state", screenOff);
context.startService(i);
}
}
You don't need to set event listener inside onReceive(). Try like this
setOnKeyListener(new View.OnKeyListener() {
#Override public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (screenOff) {
// turn on torch
return true;
}
}
return false;
}
My Application needs to know if the screen went off due to timeout or the user clicked the power button.
I decided to check if the power button was pressed.
I read some Q&A here and came up with this :
public class MyActivity extends Activity {
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_POWER) {
Log.w("Test", "Power button down detected");
return true;
}
return false;
}
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_POWER) {
Log.w("Test", "Power button up detected");
}
return false;
}
}
I also added this:
<uses-permission android:name="android.permission.PREVENT_POWER_KEY"/>
This doesn't work, it doesn't print the log.
Try to yseing from BroadcastReceiver:
public class MyBroadCastReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
//DO HERE
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
//DO HERE
}
}
}
and your manifest:
<receiver android:name=".MyBroadCastReciever">
<intent-filter>
<action android:name="android.intent.action.SCREEN_OFF"/>
<action android:name="android.intent.action.SCREEN_ON"/>
</intent-filter>
The onKeyDown method, no longer detects the single Power Key click. It does however detect the long press. Here's a very simple trick to capture it, and to differentiate from other buttons and other user's activities that would bring the app to the background.
PwrKeyShortPress = true; // set the default value in your class
....
....
#Override
public void onUserLeaveHint(){
PwrKeyShortPress = false;
super.onUserLeaveHint();
}
#Override
public void onStop() {
if (PwrKeyShortPress) {
// Do something for single Power Key click.
}
PwrKeyShortPress = true;
super.onStop();
}
go this way
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
// Power Button pressed
return true;
}
return super.onKeyDown(keyCode, event);
}
EDIT
you can listen to the screen ON/OFF this way
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
//do something
}
else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
//do something else
}
i want to know in my onPause() and onStop() method if it was called because the screen went off. Therefore i wrote a broadcastReceiver:
public class ScreenReceiver extends BroadcastReceiver {
public static boolean screenOn = true;
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
screenOn = false;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
screenOn = true;
}
}
}
public class ExampleActivity extends Activity {
#Override
protected void onCreate() {
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
}
#Override
protected void onPause() {
if (ScreenReceiver.screenOn) {
System.out.println("SCREEN TURNED OFF");
} else {
}
super.onPause();
}
#Override
protected void onResume() {
if (!ScreenReceiver.screenOn) {
System.out.println("SCREEN TURNED ON");
} else {
}
super.onResume();
}
}
how is it possible if onPause() was called, because of switching the screen off ?
maybe with this ?
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_POWER) {
return true;
}
return super.onKeyDown(keyCode, event);
}
ok i got it i use now
pm =(PowerManager) getSystemService(Context.POWER_SERVICE);
if (pm.isScreenOn() == false) { ... }