-1

I'm working on a thing where I need to override the power button in a Fragment when pressed. I did this successfully, however, the screen turns off when the power button is pressed.

I did a quick search and came across a few posts like this, this, and this. Some say it's doable and others say it's not. My question is that is this even doable? If so, any idea how to go about this?

Community
  • 1
  • 1
  • You have to keep screen on using WindowManager flags, as power button key event will only be called when its long pressed. – Abdul Kawee Apr 27 '17 at 10:16
  • @AbdulKawee I tried it in a couple of ways (one of them is mentioned in my question). It didn't work. I suspect that it prevents screen from going to sleep on t's own, not override the power button functionality. –  Apr 27 '17 at 10:17
  • Check the answer i posted. – Abdul Kawee Apr 27 '17 at 10:22
  • @AbdulKawee Testing your code now. –  Apr 27 '17 at 10:23
  • For security reasons we cannot override the functionality of a power button short click i.e lock screen but we can detect the long click of button – Abdul Kawee Apr 27 '17 at 10:34
  • @AbdulKawee Is there a way to prevent screen lock/sleep from affecting my app(if it was just for only a few seconds)? –  Apr 27 '17 at 10:49
  • How does it effect your app?? Tell me , may be there is a solution for your problem – Abdul Kawee Apr 27 '17 at 10:53
  • @AbdulKawee I'm using it in a fragment and as far as I know (please correct me if I'm wrong) locking the screen would destroy or pause the fragment. If that's the case, I suspect that I need to specify somewhere to pause the fragment on screen lock. –  Apr 27 '17 at 11:01
  • No i don't think that fragment will be destroyed, lock screen will cause you activity to pause, just save states there and in onResume() restore those states it will work for you. – Abdul Kawee Apr 27 '17 at 11:10
  • Fragments will have problem when screen orientation is changed, thats because activity is destroyed and is recreated at that time – Abdul Kawee Apr 27 '17 at 11:11
  • @AbdulKawee Thanks! –  Apr 27 '17 at 11:15

3 Answers3

1

You can use this to prevent screen from locking

getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

While using in fragment

getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

Use a BroadcastReceiver and listen for the android.intent.action.SCREEN_OFF or Intent.ACTION_SCREEN_OFF which is sent when power button is pressed (and screen is on). What i think is that overriding the power button is not possible , because of security issues.

Abdul Kawee
  • 2,687
  • 1
  • 14
  • 26
0

What you are doing is not advisable. However you can write a broadcast receiver and then the screen gets locked, wake it up using AlarmManager. Below is the code.

    @Override
public void onReceive(Context context, Intent intent) {
if ((intent.getAction().equals(Intent.ACTION_SCREEN_OFF))) {

PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "TEST");
wakeLock.acquire();

AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent inten = new Intent(context,NewActivity.class);
PendingIntent pi = PendingIntent.getActivity(context, 0, inten, 0);
alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,  100, pi);
}
}   

// Finish your WakeLock HERE. call this method after U put the activity in front or when u exit from the new activity.
public void finishWakeLocker(){
if (wakeLock != null) 
wakeLock.release();  
}
Rahul Ahuja
  • 812
  • 10
  • 24
-2

You cannot override the power button functionality however you can prevent system from showing dialog on long press of a power button using following.

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if(!hasFocus) {
           Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
            sendBroadcast(closeDialog);
        }
    }
user1097940
  • 283
  • 3
  • 12