I'm struggling a lot with keeping my screen on between activities when i have some calculations to do.
Here is the situation, I make an intent from activity A to B.
Intent intentCalculator = new Intent(this, CalculatorActivity.class);
intentCalculator.putExtra(CalculatorActivity.BANK,"20");
intentCalculator.putExtra(CalculatorActivity.DURATION,"365");
startActivity(intentCalculator);
Then on Activity B:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//tool bar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(getResources().getString(R.string.nav_calculator));
//get intent
Intent intent = getIntent();
bank = new BigDecimal(intent.getStringExtra(CalculatorActivity.BANK));
duration = Integer.parseInt(intent.getStringExtra(CalculatorActivity.DURATION));
//load data
loadData();
}
The layout "activity_calculator" is a coordinator layout that includes a relative layout. The method loadData is the one that takes long to fill a table from the relative layout and causes de screen to turn off.
I already put the FLAG_KEEP_SCREEN_ON on create and android:keepScreenOn="true" on both layouts and my screen keeps turning off.
I have no idea what I'm doing wrong, please I need some help.
Thanks for the attention, Best Regards
Try to use some more flags as:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
If problem persists and also try to use
attribute android:keepScreenOn="true"
to the root view of your activity's layout
Another method is to acquire and release a wakelock to keep screen on by:
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE,"");
mWakeLock.acquire();
permission grant:
<uses-permission android:name="android.permission.WAKE_LOCK" />
For more details visit: http://developer.android.com/training/scheduling/wakelock.html
Thanks