Android 14 Java Wake Screen depreciated - Stack Overflow

admin2025-01-08  8

I have an App that can Wake the Screen, it works perfectly on earlier versions of Android 6 to 11. After upgrading to Android 14 it no longer works. I have modified my code to incorporate the suggested depreciation fix. However it just don't work. It still works on my old Android 6 phone but not on my new upgraded Android 14 phone, the same goes for Astudio emulation. This problem has bean addressed elsewhere but the fixes don't work for me. The below code is at the start of Main. When code running in a fragment completes an intent to return to main to called:

Intent intent = new Intent(getActivity(), MainActivity.class);

startActivity(intent);

Before depreciation when the program returned to main the screen would wake, already for the user to select the next action. Don't know what I am missing I have added to the manifest:

// Java code in MainActivity :

public class MainActivity extends AppCompatActivity {

private static final int MY_REQUEST_CODE_PERMISSION = 1000;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
   
      if (Build.VERSION.SDK_INT >= 27) {
    //     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
        setShowWhenLocked(true);
        setTurnScreenOn(true);
        KeyguardManager guardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
        guardManager.requestDismissKeyguard(this, null);
    } else {

    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
            WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
            WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
            WindowManager.LayoutParams.FLAG_FULLSCREEN |
            WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
            WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);


            Window window = this.getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
            window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
            window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

}

} }

//Manifest:

I have an App that can Wake the Screen, it works perfectly on earlier versions of Android 6 to 11. After upgrading to Android 14 it no longer works. I have modified my code to incorporate the suggested depreciation fix. However it just don't work. It still works on my old Android 6 phone but not on my new upgraded Android 14 phone, the same goes for Astudio emulation. This problem has bean addressed elsewhere but the fixes don't work for me. The below code is at the start of Main. When code running in a fragment completes an intent to return to main to called:

Intent intent = new Intent(getActivity(), MainActivity.class);

startActivity(intent);

Before depreciation when the program returned to main the screen would wake, already for the user to select the next action. Don't know what I am missing I have added to the manifest:

// Java code in MainActivity :

public class MainActivity extends AppCompatActivity {

private static final int MY_REQUEST_CODE_PERMISSION = 1000;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
   
      if (Build.VERSION.SDK_INT >= 27) {
    //     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
        setShowWhenLocked(true);
        setTurnScreenOn(true);
        KeyguardManager guardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
        guardManager.requestDismissKeyguard(this, null);
    } else {

    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
            WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
            WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
            WindowManager.LayoutParams.FLAG_FULLSCREEN |
            WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
            WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);


            Window window = this.getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
            window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
            window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

}

} }

//Manifest:

Share Improve this question edited Nov 23, 2024 at 7:18 Dave asked Nov 23, 2024 at 7:01 DaveDave 113 bronze badges 6
  • <uses-permission android:name="android.permission.WAKE_LOCK"/> – Dave Commented Nov 23, 2024 at 7:08
  • <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/> – Dave Commented Nov 23, 2024 at 7:13
  • 1 <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/> – Dave Commented Nov 23, 2024 at 7:13
  • <uses-permission android:name="android.permission.POST_NOTIFICATIONS"/> – Dave Commented Nov 23, 2024 at 7:14
  • <uses-permission android:name="android.permission.SUBSCRIBE_TO_KEYGUARD_LOCKED_STATE" /> – Dave Commented Nov 23, 2024 at 7:14
 |  Show 1 more comment

2 Answers 2

Reset to default 0

I found a better solution instead of opening a Wake Lock at the start of main, just call the Screen Wake method at the start of the Main and then again on completion of the code. I tried calling it just once I have to call it twice. Note this works for my VIVO Y55 5G phone and the application I am coding.

/////// Code at start of MaineActivity, ////

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); 

waky();

///////////////////////////////

Other Methods in MainActivity{

On compleation call: void waky();

}
}

///////////////////////////

//Method at bottm of MainActivity to wake screan

void waky() {
// Wake up code post AP I27 //
if (Build.VERSION.SDK_INT >= 27) {  //eather one
//if (Build.VERSION.SDK_INT >= 
Build.VERSION_CODES.O_MR1) {  //eather one 
setShowWhenLocked(true);
setTurnScreenOn(true);
KeyguardManager guardManager = (KeyguardManager) 
getSystemService(Context.KEYGUARD_SERVICE);

guardManager.requestDismissKeyguard(this, null);

//   Toast.makeText(this, "Waky Waky: Wake >= 27 ", Toast.LENGTH_LONG).show();

} else {

/////////////  Flags needed to be set for wakeup from sleep pre API 27   /////

this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |

 WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |

WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |

WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,

WindowManager.LayoutParams.FLAG_FULLSCREEN |

WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams

I Found a solution, the solution was to move all the app code from the fragment to the MainAcitivity, and create a wake-up method at the end of MainAcitivity

void waky(){ // wake up code }

then on completion of the app code, call the wake-up method:

void waky();

in my App at this point an intent is opened and it goes to a Class named EndCycle where another intent takes it back to MainActivity.

It seems the new wake-up fix for API's >= 27 only works in the MainActivity. On my actuarial hardware phone this only wakes the screen if the screen has bean asleep for less than 15 seconds, any longer and the screen will not wake. To overcome this problem I found that I needed to call the Screen wake method at the start of the MainAcitivity as well as on completion of the code. However it is likely the need to call the Screen Wake Method twice is hardware and application dependent, as it is not needed with emulated devices, I also had to tweak my settings. Any suggestions to improve this would be welcome and thanks for these snippets of code it is much appreciated.

/////// Code at start of MaineActivity, ////

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); 
// At start call Screen Wake Method:
waky();
//Toast.makeText(this, "Hello Main: Waky", Toast.LENGTH_LONG).show();

///////////////////////////////

Other Methods in MainActivity{

// On completion call Screen Wake Method: 
waky();

}

///////////////////////////

//Method at bottom of MainActivity to wake screen

void waky() {
// Wake up code post AP I27 //
if (Build.VERSION.SDK_INT >= 27) {    //eather one
//if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) { //eather one 
setShowWhenLocked(true);
setTurnScreenOn(true);
KeyguardManager guardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
guardManager.requestDismissKeyguard(this, null);

//   Toast.makeText(this, "Waky Waky: Wake >= 27 ", Toast.LENGTH_LONG).show();

    } else {

/////////////  Flags needed to be set for wakeup from sleep pre API 27   /////
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

  ////////// Wakeup code for pre API 27 ////////////
Window window = this.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
//    Toast.makeText(this, "Waky Waky: Wake 23 to 26", Toast.LENGTH_LONG).show();
    }
}


//////////////////////////////////////////

// Permisions in Manifest

<uses-permission android:name="android.permission.WAKE_LOCK"/> – 
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/> – 
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/> –  
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/> –  
<uses-permission android:name="android.permission.SUBSCRIBE_TO_KEYGUARD_LOCKED_STATE" />
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736270955a1473.html

最新回复(0)