Wednesday, July 30, 2014

Connecting to android emulator running on another PC for debugging

If you have more than one computer, you might want to use one to run the ide and probably the other to run the android emulator. This is a good idea since the android emulator ,along with an ide like android studio, can take up reasonable amount of resources. Running them separately will ease the workload. You can use the steps below to connect to an android-emulator or an instance of bluestacks running on another machine.

1. In the machine where you want to run the emulator, download and install FreeSSHd from
 (Select No in the message box asking if you want to install it as a system service. )
2. Open up FreeSSHd and under the Server status tab check if the SSH server is running. If not,
      a. Select the SSH tab and change the port to something else (for eg: 27).
      b. Click Apply.
      c. Now go back to the Server status tab and start the ssh server.

3. Select Tunneling tab. Tick mark both Allow local port forwarding and Allow remote port forwarding and click Apply

4. Go under Users tab. Click Add
5. Enter a username
6. Select Password stored as  SHA1 hash in the Authorization drop down menu
7. Enter a password
8. Tick mark Shell, SFTP and Tunneling under the Users can use section.
9. Click OK and then Apply

10. Start the emulator or bluestacks

11. Now, on the machine you have installed the ide, download and install Putty from
12. Open up putty. Under Connection ->SSH->Tunnels, select Local and Auto
13. Against Source port enter 5555 and against Destination enter localhost:5555. Then click Add
14. Do the same with Source port 5554 and Destination localhost:5554

15. Go to Session, Select SSH and enter the ip address of the machine where you installed FreeSSHd and the port you set in step 2. (Read step 1 in to learn how to find out your internal ip address)
16. Under Saved sessions enter a name and click Save
17. Click Open

18. Enter the username and password you entered in steps 5 and 6 respectively.
19. Open up the command prompt.
20. Change the directory to the one where adb.exe is located
21. Enter adb kill-server followed by adb start-server
22. If you want to connect to Bluestacks, enter adb connect localhost
23. Enter adb devices and you will see that the available devices are listed

Start the ide and you will be able to debug against this device.

NOTE : Make sure that the firewall isn't blocking the connection attempts by putty and FreeSSHd.

Tuesday, July 15, 2014

Permanently saving game or application settings on an android device.

If your application contains a good amount of customizable preferences, you will have to integrate into it the capability to save these preferences permanently. There are 2 ways to do this. One is to create an SQLite database and store the settings into it and the other is to create a simple file and store these settings in your own format.

Dealing with an SQLite database isn't that straight forward and should be avoided unless you really need what it has to offer. A database is suited for saving information is in a standardized manner so that this information can easily be used by multiple applications. These applications maybe developed by different developers, but since the information in a standard structure, they can create apps. that can process the information contained in this database without much effort. An example would be a dictionary database which stores a large amount of words, their meanings, word class etc. A database like this can be easily migrated for use from one app to another.

Using an SQLite database to store some simple application settings would be an overkill. It is much faster and easier to store and retrieve these using a simple binary file. Let's see how this can be done.

Here I will create a class called GameSettings which will hold the settings data and a class GameDataManager which will be responsible for writing and retrieving data from the external storage.

Add the necessary permissions to the manifest first.

The GameSettings class contains 4 variables.

1. difficulty - Determines the game difficulty
2. controller - Whether to use touch or accelerometer.
3. soundEnabled - Whether sound is on or off.
4. defaultSettings - A static string variable which holds the default values of all the above variables.

GameSettings class:


Once you have these 2 classes, create a new GameDataManager instance. If it's the first time the game/app is running, this will create a new settings file with the default settings at the specified location.
Now you can call the GameDataManager.getData() function to get an instance of GameSettings. Call GameDataManager.writeData() after updating any settings value. Edit both these classes according to your need.

Below is a sample use of these classes.

Saturday, July 12, 2014

Google play store download not starting? Here's how to fix it.

Read onwards from the second step in the link below

Fixing persistent 'No internet connection' error on google play store.

If you are using an android smartphone, you might have come across the most annoying and unreasonable error when opening the play store app. Even if your smartphone has a working internet connection the play store app will keep lying to you that there is no internet connection. Or sometimes an app download will just not start. It will keep on showing 'Starting download' for ever. Here are the reasons why these occur and the ways to fix them.

1. Correct the date and time on your phone.
If you ran out of battery for a long time or if the battery got pulled out of its socket for some reason, the date and time on your smartphone will be incorrect. 
If the time on your android device isn't correct it will cause some 'https' protocol errors inside the play store app and it will resist to connect. Fix the date and time and everything will be back to normal. if it doesn't, read step 2.

2. Forward a port in your router for google play.
The most common reason for the error is that your router is blocking your phone's attempt to connect to play store.
What you need to do is to open the port 5228 in your router. The link below will show you how to port forward. Use 5228 as both external and internal ports.

3.Switch-off and then switch-on your phone
Maybe something inside your phone got bugged in the long run. Reboot your phone and this will fix such problems.

4.Clear the cache of google-play and google-play-services
Eventhough doing this step hasn't ever helped me, it might help you. 
Go to settings->application settings in your phone.
Look for google play app. Select it and press the Clear cache button there.
Now press back and find the google-play-services entry and do the same.

If none of the above works for you, it maybe that the google servers are busy or under maintanance. Wait for some time and retry, it will connect.

How to do port forwarding.

If you are connecting to the internet through WiFi or LAN, for some applications to connect to the internet properly you will have to open/forward some 'ports' inside your router. This is because the application you are using communicates to the internet through a port that isn't used commonly. Most routers do not allow communication through uncommon ports for security reasons. When you forward/open a port, this tells the router not to discard data-packets arriving at this port.

To perform port forwarding, first you will have to collect the following easy to get information

1.The ip address of your router's settings page and the local address of your PC

To find this ,open up control panel and at the right top of the page select small icons in the view by drop down menu.

Now click on Network and sharing center in that page.

In the window that appears, underneath the View your active networks section, click on the entry against Connections

This will open open up a new window and in that click on the Details button.

In the windows that opens up note down the values against. IPv4 address and IPv4 gateway

The IPv4 address here is the internal IP address of your PC and the IPv4 gateway is the address of your router.

NOTE:  If the application you want to port forward is in some other device, note down the IPv4 address of that device. If the device is a smartphone, search on the internet to find out how to find the internal IP address of that device. There are usually apps available for finding out such information.

2. The port your application uses
Applications that need to be port forwarded will have their on unique port number. You'll have to do a google search to find this out.

After having found out all that info., open up your web browser and enter the IPv4 gateway address you noted down earlier. This will open up the settings page of your router. 

It will be asking you for username and password now. Every router has a default username and password. Mostly it is admin for both username and password.

If those aren't the default ones, time for another google search. Search for 'default username and password' along with the manufacturer name of your router.

Once you find the username and password enter it in the password dialogue box and get rid of it.

Now the trick is to find out the 'port forwarding' page of your router. Since router settings pages differ from one model to the other, I can't tell you where exactly it is located.

Look for a page that says port forwarding. Or a page where you can enter the details like 'protocol name', 'internal port', 'internal IP','external port' and such.

NOTE: The IP-filter page will also contain fields like those. Make sure that it is not that one.

The port forwarding page in my router

The port forwarding page will have a ready made list of port forwarding rules for some applications. But chances are that your application is not in there and you will have to do it manually. In that case you will have to enter the following details.

1. Port forwarding rule name: Enter the name of the application for which you are port forwarding here.

2. Protocol/Type: Depends on the application. But most applications use TCP and UDP protocols.
If you can select only one protocol at a time, you will have to do the steps from 3 to 5 for all of the protocols. In this case, for TCP and UDP.

3. Internal IP address: This is the internal address you found out in step 1.

4. Internal Port Range: This is the port number mentioned in step 2. For both start and end of the range enter the same port number.

5. External Port Range: Depending on the type of application this can be the same as what you entered in    internal port range or 1 to 65535. If you are in doubt enter 1 as start and 65535 as end of the range.

Once you have entered those details click apply. You might have to restart the router for the settings to take effect.