Keep the screen awake throughout my activity

As discussed in the Android tutorial Keep the Screen On, you can do this in a few ways. You can set the FLAG_KEEP_SCREEN_ON on the activity’s window: getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); An XML equivalent for that is to add the attribute android:keepScreenOn=”true” to the root view of your activity’s layout. The advantage of setting the flag programmatically is … Read more

How to avoid restarting activity when orientation changes on Android

There are various ways to do it, but as given here, using android:configChanges=”keyboardHidden|orientation|screenSize” allows you to listen for the config changes. You then respond to these changes by overriding onConfigurationChanged and calling setContentView. This is the way I’ve been doing it, but I’d be interested to know other people’s thoughts.

Detecting when screen is locked

There is a better way: KeyguardManager myKM = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE); if( myKM.inKeyguardRestrictedInputMode()) { //it is locked } else { //it is not locked } No need for broadcastRecievers, permissions or anything similar. Note: It doesn’t work when user has set his/her screen lock to none in settings–>security–>screenlock–>none

getLocationOnScreen() vs getLocationInWindow()

I don’t think this answer is correct. If I create a new project, and edit only the MainActivity by adding the following snippet: public boolean dispatchTouchEvent(MotionEvent ev) { View contentsView = findViewById(android.R.id.content); int test1[] = new int[2]; contentsView.getLocationInWindow(test1); int test2[] = new int[2]; contentsView.getLocationOnScreen(test2); System.out.println(test1[1] + ” ” + test2[1]); return super.dispatchTouchEvent(ev); } I will … Read more

Android accelerometer not working when screen is turned off

Basically, it is a problem with my phone. Other users have reported this also happens with their phones, from different brands but same Android version. Other persons have no problem at all – strongly indicating that this is not a problem with the stock version of android but from the implementations of each company for … Read more

Get and Set Screen Resolution

For retrieving the screen resolution, you’re going to want to use the System.Windows.Forms.Screen class. The Screen.AllScreens property can be used to access a collection of all of the displays on the system, or you can use the Screen.PrimaryScreen property to access the primary display. The Screen class has a property called Bounds, which you can … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)