Get rotation from its “natural” orientation on your android device

Up to API Level 7 you can use android.view.Display.getOrientation() to get the rotation of the screen from its default orientation. But this method was deprecated in API level 8.

But now you can use

The returned value is:

  • Surface.ROTATION_0 (no rotation)
  • Surface.ROTATION_90
  • Surface.ROTATION_180
  • Surface.ROTATION_270
  • If your device has a naturally portrait screen, and the user has turned it on its side to go into a landscape orientation, the value returned here may be either Surface.ROTATION_90 or Surface.ROTATION_270 depending on the direction it was turned.

    With this simple line of code you can get the information about the screen orientation related to the default. This could be helpful, if you want to deal with your Sensor.

    Android Activity without UI

    Is it possible to create a Android activity without UI?

    Yes it is. Android provides a theme for this requirement. Add the following line to your AndroidManifest.xml and call finish() at the end of the Activity’s onCreate() method.


    android:theme="@android:style/Theme.NoDisplay"

    Theme_NoDisplay

    public static final class android.R.style – Added in API level 3
    int Theme_NoDisplay
    Default theme for activities that don’t actually display a UI; that is, they finish themselves before being resumed.

    Constant Value: 16973909 (0x01030055)

    How to know if the app goes to Background

    With API Level 14 – Android 4.0, there is a Method in Application which is called onTrimMemory. The onTrimMemory is executed by the system with different levels ( TRIM_MEMORY_COMPLETE, TRIM_MEMORY_MODERATE, TRIM_MEMORY_BACKGROUND, TRIM_MEMORY_UI_HIDDEN, TRIM_MEMORY_RUNNING_CRITICAL, TRIM_MEMORY_RUNNING_LOW, or TRIM_MEMORY_RUNNING_MODERATE). The interesting level is “TRIM_MEMORY_UI_HIDDEN”. This level can be used to know that the app is going to background.

    The following example is a demonstration of the Application.onTrimMemory Method:


    public class MyApplication extends Application {
    // ...
    @Override
    public void onTrimMemory(int level) {
    super.onTrimMemory(level);
    if (level == TRIM_MEMORY_UI_HIDDEN) {
    notifyAppInBackground();
    }
    }
    // ...
    }

    With this simple piece of code, you can check if your app is being sent to the background!

    How to Format cells in Excel for Android

    Not a snippet, but a needed Information when using Microsoft Android Calc

    1449826012329
    How to Format cells in Excel for Android. (Sceenshot made on a german device)

    Some more informations are available in the video at https://support.office.com/en-us/article/Video-Getting-started-with-Excel-for-Android-tablet-c5d5c135-0bc1-4aea-9b54-95a518dd0c11. Excel for Android is very similar to Microsoft Excel for Windows. But some differences can confuse sometimes. The short video shows you how to get up and running with Microsoft Calc for Android.

    Recompiling objects in oracle

    A pl/sql script to recompile all invalid objects in oracle. Simple but powerfull.

    Source:
    http://www.orafaq.com/usenet/comp.databases.oracle.misc/2005/02/15/0382.htmy

    How To use landscape and reverseLandscape in AndroidManifest.xml

    If you develope a app or activity which is only available in landscape mode, you can enhance the app by adding the reverseLandscape mode (Landscape orientation in the opposite direction from normal landscape. Added in API level 9.) via editing the android:screenOrientation attribute of your activity in AndroidManifest.xml. Instead of using the “landscape” value, you can use “sensorLandscape” (Landscape orientation, but can be either normal or reverse landscape based on the device sensor. Added in API level 9.).

    This is a simple solution and your users will enjoy the possibility of using the device even if it is rotated by 180 degrees. The same solution is possible for portrait apps and activities. In this case you have to use sensorPortrait instead of sensorLandscape in AndroidManifest.xml.

    How to sort an ArrayList in Java

    If you want to sort any ArrayList in java, you need to make sure, your objects in the arrylist are comparable. This is done by using implements Comparable and implementing the abstract method compareTo (T cmp). For example if you want to sort some highscores you can do it like this.

    This is a simple, but powerfull solution to sort an ArrayList.

    How to execute a shell command from android app

    To execute a shell command from your android app you can grab the standard input of the su process just launched by Runtime.getRuntime().exec(“su”) and write down the command there, otherwise you are running the commands with the current UID.

    With the following code you can excute the screenrecord command to grab your screen to a mp4 video file (Works on Android 4.4 API level 19 and higher). There are many othe commands you can execute. You can find some of them at https://developer.android.com/tools/help/shell.html

    Source:
    http://stackoverflow.com/questions/20932102/execute-shell-command-from-android

    How to turn on/off (enable/disable) GPS in Android

    If you want to develop an Android app to turn on/off GPS you can use the following code snippets.

    Source:
    http://stackoverflow.com/questions/15426144/turning-on-and-off-gps-programmatically-in-android-4-0-and-above

    Simple standalone http server made with esp8266 and nodemcu

    The following code is a standalone http server made with esp8266 and nodemcu written in lua.
    The esp8266 is in wifi.SOFTAP mode (SoftAP is an abbreviated term for “software enabled access point.” This is software enabling a computer which hasn’t been specifically made to be a router into a wireless access point. It is often used interchangeably with the term “virtual router”.)

    You can connect connect your smarthone with the esp8266 direct without any router or other network infrasructure. Just load the following code as init.lua on you eso8266 flash ram and reset.

    The following circuit shows the min. configuration for your esp8266 to get the best stability.
    ESP8266_improved_stability

    If you ues a board with GPIO16 (Not ESP-01) you can connect the GPIO16 with REST to use the dsleep function. Otherwise your esp8266 will not wake up after execution of dsleep.