Thursday, June 4, 2015

Foredroid - Detecting when your Android app goes background / becomes foreground - Android

Detecting when your Android app goes background / becomes foreground,
is not so simple task as you might think...

I found this utility git for detecting and notifying this:

Please note, that it won't work correctly for all case, but it's still very useful.
For example a share intent, if the share dialog is open and your app is still visible in the background, it won't work.
But after selecting an app to share with, it will.

Anyway, It was perfect for what I needed.

Wednesday, June 3, 2015

Control Android's recent apps screenshot or disable screenshots in Android

Sometimes you have a situation when you need to control the screenshot that is shown in Android's task switcher / recents apps menu,

For example: you have an application with a lock screen, and when the user lock his session and the application moves to background, you don't want to accidentally show the last application activity in the recents apps menu.

Sooo, You have 2 options:
  1. Exclude your app from the recents apps menu
    You can do it by adding next code in the manifest.xml file under the <activity> tags:

  2. Disable screenshots of your app
    You can disable screenshots of your app, and as a side effect, 
    The android system itself, won't be able to take a screenshot of your app last session.
    You can do it by:

    public class MyActivity extends Activity 
      public void onCreate(Bundle savedInstanceState) 

    Please note that in this option, your app will still be in the recent apps menu, 
    but with a blank (white or black) screenshot thumbnail.

    In Addition, when you will try to get screenshot you will get this message:

Monday, September 16, 2013

Write and Read (Serialize) Object to memory

If you want to save object like HashMap, Array and so on to application memory,
use code below. Please note, all objects fields should be Serializable.

Types like String, int, HashMap... are already serializable, but if you create a class of your own, you should verify that that it implements Serializable, and so all of his content recursively.

Write to file:
     * Writes Serializable object into a file
    public static void writeObjectToFile(Context context, Object object, String filename)
        ObjectOutputStream objectOut = null;
            FileOutputStream fileOut = context.openFileOutput(filename,Activity.MODE_PRIVATE);
            objectOut = new ObjectOutputStream(fileOut);

        catch (IOException e)
            if (objectOut != null)
                catch (IOException e)
Read from file:
     * Reads a Serializable object from a file
    public static Object readObjectFromFile(Context context, String filename)
        ObjectInputStream objectIn = null;
        Object object = null;
            FileInputStream fileIn = context.getApplicationContext().openFileInput(filename);
            objectIn = new ObjectInputStream(fileIn);
            object = objectIn.readObject();

        catch (FileNotFoundException e)
            // Do nothing
        catch (IOException e)
        catch (ClassNotFoundException e)
            if (objectIn != null)
                catch (IOException e)

        return object;

Monday, September 9, 2013

How to use Google Maps V2 - Keystore orientation

1. Locate your keystore file:

    For debugging you should use the debug keystore file located in:

    For release version you should use the generated keystore
    (the same one that you use when you'll sign your applicatoin for distrebution)

2. Execute command (cmd) and extract SHA1 string:

    For debug:
keytool -v -list -alias androiddebugkey -keystore 
-keypass android

    For release:
keytool -v -list -alias YOUR_ALIAS_HERE -keystore 

3. Open

4. Go to services.

5. Enable google maps V2.

6. Go to API access menu item.

7. Press on "Create new Android key".

8. Paste your sha1 code with the application package:
(e.g. 40:DC:19:FF:11:33:B9:61:64:54:79:7A:84:5C:C8:F8:AF:CC:FC:1C;

9. Copy your API key from the Simple API Access    
    (should look like : AIzzSyACccKZeD-4lmBrtZI0JSrxBOAMnK1oEIk).

10. Paste your API key in your AndroidManifest.xml file:
<meta-data android:name=""

Monday, August 26, 2013

How Android developer should get resources

Hi Designers,
Few very important conventions

  1. Names should be meaningful.
    Bad: "logo".
    Good: "main_application_logo"
  2. No capital letters, spaces or dashes in names.
  3. Icons resources should starts with "ic_".
    For example: "ic_home_logo".
  4. Backgrounds resources (also ninepatches) should starts with "bg_".
    For example: "bg_enter_button".
  5. Resources should be separated to folders: xhpi, hdpi, mdpi (and maybe xxhdpi).
  6. Resource name should be consistent for all folders.
    Bad: "ic_home_logo_xhdpi",  "ic_home_logo_hdpi",  "ic_home_logo_mdpi".
    Good: "ic_home_logo" in different sizes in each folder.

Please please please follow these conventions.
You are driving me crazy!

Friday, July 26, 2013

Introduction to XMPP / Jabber

What is Xmpp?

The Extensible Messaging and Presence Protocol (XMPP - originally named Jabber) is an open technology for real-time communication, using the Extensible Markup Language (XML) as the base format for exchanging information. In essence, XMPP provides a way to send small pieces of XML from one entity to another in close to real time.

Xmpp and Jabber, what is the difference?

Xmpp protocol was originally named Jabber. That's all.


Configuration to use while establishing the connection to the server.
Here you can set the Host, Port and Service details:

Google Hangout (talk) configuration for example:

public static final String HOST = "";
public static final int PORT = 5222;
public static final String SERVICE = "";

ConnectionConfiguration connectionConfiguration = 
    new ConnectionConfiguration(HOST, PORT, SERVICE);


Creates a socket connection to a XMPP server.
Gets connectionConfiguration in the constructor.
XmppConnection xmppConnection =
    new XMPPConnection(connectionConfiguration);

Tuesday, July 23, 2013

MultiUserChat with XMPP - Android Tutorial

In this post I will show you how to build an application with multi user chat running on XMPP server.

For a small introduction read this post: Introduction to xmpp
Now let's go fucking coding!
You can download the Jar file from here

Step 1 - Define some parameters

    public static final String HOST = "";
    public static final int PORT = 5222;
    public static final String SERVICE = "";

    String mNickName;
    String mGroupChatName;
    private MultiUserChatmMultiUserChat;
    private XMPPConnectionmXmppConnection;
    private ConnectionConfiguration mConnectionConfiguration;

Step 2 - Create a connection

 mConnectionConfiguration = 
new ConnectionConfiguration(HOST, PORT, SERVICE);
mXmppConnection = new XMPPConnection(mConnectionConfiguration);
SASLAuthentication.supportSASLMechanism("PLAIN", 0);

Step 3 - Login and set status

mXmppConnection.login(USERNAME, PASSWORD, NICK_NAME);
// Set the status to available
Presence presence = new Presence(Presence.Type.available);

Step 4 - Set the Mutliuser Chat

// Create a MultiUserChat using a Connection for a room 
// (room name as the second parameter)
mMultiUserChat = new MultiUserChat(mXmppConnection, mGroupChatName);
mMultiUserChat.sendConfigurationForm(new Form(Form.TYPE_SUBMIT));

Step 5 - Received messages

// Add a packet listener to get messages sent to us
PacketFilter filter = new MessageTypeFilter(Message.Type.groupchat);
mXmppConnection.addPacketListener(new PacketListener()
    public void processPacket(Packet packet) 
        Message message = (Message) packet;
        if (message.getBody() != null)
            String from = message.getFrom();
            String Body = message.getBody();
            // Add incoming message to the list view or similar
}, filter);

Step 6 - Send message

// import org.jivesoftware.smack.packet.Message;
Message msg = new Message(to, Message.Type.groupchat);